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

Last updated: Apr 11, 2024
3 min

## #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
```Copied!```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
```Copied!```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
```Copied!```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.

## #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
```Copied!```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
```Copied!```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
```Copied!```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.