# How To Solve Error “ValueError: Object Too Deep For Desired Array”

## The cause of the “ValueError: object too deep for desired array” error

The main reason for the “ValueError: object too deep for desired array” error is when we use the convolve() function, this function does not see array as a 1D array.

The convolve() function is often seen in signal processing, where it models the effect of a linear time-invariant system on a signal.

Example:

import numpy as np

array1D = [2, 1, 1, 1, 1]
myArray = [[2], [2], [5], [2], [2]]

# Here is the cause of this error
newArray = np.convolve(array1D, myArray, "valid")
print(newArray)

Output:

The error will occur if you try to run this code.

Traceback (most recent call last):
File "<string>", line 4, in <module>
File "<__array_function__ internals>", line 180, in convolve
File "/usr/local/lib/python3.8/dist-packages/numpy/core/numeric.py", line 844, in convolve
return multiarray.correlate(a, v[::-1], mode)
ValueError: object too deep for desired array

## How to solve this error?

The best solutions to solve the “ValueError: object too deep for the desired array” error is to convert to the 1D array by using the hstack() method, using the flatten() method or using the ravel() method and then using the involve() method.

### Using the hstack() method

The hstack() method allows for rearranging the array in horizontal order.

Syntax:

numpy.hstack(tup)

Parameter:

• tup: is the 2D array or more.

Return value: The return value is a 1D array.

Example:

In this example, we will use hstack() method to convert an array into a 1D array. We can easily to use convolve() function with ‘valid’ argument to calculate two arrays with other.

import numpy as np

array1D = [2, 1, 1, 1, 1]
myArray = [[2], [2], [5], [2], [2]]
myArray = np.hstack(myArray)
print("Convert myArray to a 1D array:", myArray)

# Ex: 2*2 + 1*2 + 5*1 + 2*1 + 2+1 = 15
newArray = np.convolve(array1D, myArray, "valid")
print(newArray) 

Output:

Convert myArray to a 1D array: [2 2 5 2 2]
[15]

### Using the flatten() method

Syntax:

numpy.array(ar).flatten()

Parameter:

• ar: is an array you want to convert to the 1D array.

Return value: The return value is a 1D array copy.

Example:

In this example, we will use flatten() method to convert an array into a 1D array. We can easily to use convolve() function with ‘valid’ argument. It will calculate two arrays with other.

import numpy as np

array1D = [2, 1, 1, 1, 1]
myArray = [[2], [2], [5], [2], [2]]
myArray = np.array(myArray).flatten() # The myArray will return a 1D array copy
print("Convert myArray to a 1D array:", myArray)

newArray = np.convolve(array1D, myArray, "valid") # Expected output is 15
print(newArray) 

Output:

Convert array to a 1D array: [2 2 5 2 2]
[15]

### Using the ravel() method

Syntax:

numpy.array(ar).ravel()

Parameter:

• ar: is an array you want to convert to a 1D array.

Return value: The return value is a 1D view over the array.

Example:

import numpy as np

array1D = [2, 1, 1, 1, 1]
myArray = [[2], [2], [5], [2], [2]]
myArray = np.array(myArray).ravel() # The return value is a 1D view over the array.
print("Convert myArray to a 1D array:", myArray)

newArray = np.convolve(array1D, myArray, "valid") # Expected output is 15
print(newArray) 

Output:

Convert array to a 1D array: [2 2 5 2 2]
[15]

## Summary

In this article, we’ve already explained to you how to solve the “ValueError: object too deep for desired array” error with three solutions using the hstack() method, using the flatten() method and using the ravel() method. We always hope this information will be of some help to you. If you have any questions, don’t hesitate and leave your comment below. Thanks for your reading.

Maybe you are interested: