ValueError: object too deep for desired array [Solved]

Borislav Hadzhiev

Last updated: Jun 15, 2023

Reading timeยท3 min

**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')`

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.

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]`

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.

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:

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

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.

`numpy.ravel()`

method to solve the errorYou 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.

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

- Convert a NumPy array to 0 or 1 based on threshold in Python
- How to get the length of a 2D Array in Python
- TypeError: 'numpy.ndarray' object is not callable in Python
- TypeError: Object of type ndarray is not JSON serializable
- IndexError: too many indices for array in Python [Solved]
- How to filter a JSON array in Python
- AttributeError module 'numpy' has no attribute array or int
- How to check if a NumPy Array is multidimensional or 1D
- Numpy: How to extract a Submatrix from an array