ValueError: object too deep for desired array [Solved]

avatar
Borislav Hadzhiev

Last updated: Jun 15, 2023
3 min

banner

# ValueError: object too deep for desired array [Solved]

The NumPy "ValueError: object too deep for desired array" occurs when you pass a multidimensional array where a one-dimensional array is expected.

You can solve the error by removing the extra dimension or converting the array to one-dimensional.

Here is an example of how the error occurs.

main.py
import numpy as np x = np.array([1, 2, 3]) y = np.array([[0.5, 1, 0.3], [0.2, 0.7, 0.8]]) # โ›”๏ธ ValueError: object too deep for desired array arr = np.convolve(x, y, 'same')

valueerror object too deep for desired array

The numpy.convolve method takes two one-dimensional input arrays and returns the discrete, linear convolution of two one-dimensional sequences.

The error is raised because the second argument we passed to numpy.convolve is a two-dimensional array.

You can use the numpy.shape attribute to verify that the array is not one-dimensional.

main.py
import numpy as np x = np.array([1, 2, 3]) print(x.shape) # (3,) y = np.array([[0.5, 1, 0.3], [0.2, 0.7, 0.8]]) print(y.shape) # (2, 3)

The second array is two-dimensional and has 2 rows and 3 columns.

# Removing the extra dimension to solve the error

One way to solve the error is to remove the extra dimension by slicing the array.

main.py
import numpy as np x = np.array([1, 2, 3]) y = np.array([[0.5, 1, 0.3], [0.2, 0.7, 0.8]]) print(y[:, 0]) # ๐Ÿ‘‰๏ธ [0.5 0.2] arr = np.convolve(x, y[:, 0], 'same') print(arr) # ๐Ÿ‘‰๏ธ [0.5 1.2 1.9]

removing extra dimensions to solve the error

We used array slicing to select the first element of each subarray.

Now both arrays we passed to numpy.convolve are one-dimensional, so the error is resolved.

# Converting a multidimensional array to one-dimensional to solve the error

You can also solve the error by converting the multidimensional array to one-dimensional.

main.py
import numpy as np x = np.array([1, 2, 3]) y = np.array([[0.5, 1, 0.3], [0.2, 0.7, 0.8]]) # โœ… convert the array to one-dimensional y = np.reshape(y, y.size) print(y) # ๐Ÿ‘‰๏ธ [0.5 1. 0.3 0.2 0.7 0.8] arr = np.convolve(x, y, 'same') print(arr) # ๐Ÿ‘‰๏ธ [2. 3.8 3.8 2. 2.8 3.7]

We used the numpy.reshape method to convert the multidimensional array to one-dimensional.

The two arguments we passed to numpy.reshape are:

  1. The array we want to reshape.
  2. The new shape of the array. If an integer is supplied, then the result is a one-dimensional array of that length.

# Flattening the array to solve the error

You can also flatten the array to solve the error.

main.py
import numpy as np x = np.array([1, 2, 3]) y = np.array([[0.5, 1, 0.3], [0.2, 0.7, 0.8]]) y = y.flatten() print(y) # ๐Ÿ‘‰๏ธ [0.5 1. 0.3 0.2 0.7 0.8] arr = np.convolve(x, y, 'same') print(arr) # ๐Ÿ‘‰๏ธ [2. 3.8 3.8 2. 2.8 3.7]

We used the numpy.ndarray.flatten method to get a copy of the array collapsed into one dimension.

The flatten() method doesn't flatten the array in place, so make sure to assign the result of calling flatten() to a variable.

# Using the numpy.ravel() method to solve the error

You can also use the numpy.ravel method to flatten the array and solve the error.

main.py
import numpy as np x = np.array([1, 2, 3]) y = np.array([[0.5, 1, 0.3], [0.2, 0.7, 0.8]]) y = np.ravel(y) print(y) # ๐Ÿ‘‰๏ธ [0.5 1. 0.3 0.2 0.7 0.8] arr = np.convolve(x, y, 'same') print(arr) # ๐Ÿ‘‰๏ธ [2. 3.8 3.8 2. 2.8 3.7]

The numpy.ravel() method returns a 1-D array containing the elements of the input.

The method makes a copy of the array only if needed.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev