ValueError: Found array with dim 3. Estimator expected 2

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
3 min

banner

# ValueError: Found array with dim 3. Estimator expected 2

The Python "ValueError: Found array with dim 3. Estimator expected <= 2" occurs when you pass a 3-dimensional array where a 2-dimensional array is expected, e.g. when calling fit().

To solve the error, use the numpy.reshape() method to reshape the array to 2-dimensional.

Here is an example of how the error occurs.

main.py
import numpy as np from sklearn.linear_model import LinearRegression # ๐Ÿ‘‡๏ธ 3-dimensional array x = np.array( [ [[19.42, 43.4]], [[19.22, 43.9]], [[19.68, 44.1]], [[19.67, 44.2]], [[19.67, 44.2]] ] ) print(x.shape) # ๐Ÿ‘‰๏ธ (5, 1, 2) y = np.array([[41.4], [42.9], [44], [45.1], [41.2]]) print(y.shape) # ๐Ÿ‘‰๏ธ (5, 1) model = LinearRegression() # ๐Ÿ‘‡๏ธ calling fit() with 3-dimensional array (2-dimensional is expected) # โ›”๏ธ ValueError: Found array with dim 3. LinearRegression expected <= 2. reg = model.fit(x, y) print(reg.score(x, y)) # ๐Ÿ‘‰๏ธ 0.08035714285714268

value error found array with dim 3 linearregression expected 2

The code for this article is available on GitHub

The x variable stores a 3-dimensional array, however, the fit() method takes a 2-dimensional array for the training dataset.

Notice that the shape of the x array is (5, 1, 2) - 5 subarrays with 1 subarray element and each subarray contains 2 elements.

# Reshape the 3-dimensional array to 2-dimensional

You can solve the error by using the numpy.reshape() method to reshape the array to 2-dimensional.

main.py
import numpy as np from sklearn.linear_model import LinearRegression x = np.array( [ [[19.42, 43.4]], [[19.22, 43.9]], [[19.68, 44.1]], [[19.67, 44.2]], [[19.67, 44.2]] ] ) # โœ… make the array 2-dimensional x = x.reshape(-1, 2) print(x.shape) # ๐Ÿ‘‰๏ธ (5, 2) y = np.array([[41.4], [42.9], [44], [45.1], [41.2]]) print(y.shape) # ๐Ÿ‘‰๏ธ (5, 1) model = LinearRegression() reg = model.fit(x, y) print(reg.score(x, y)) # ๐Ÿ‘‰๏ธ 0.08035714285714268

using reshape to solve the error

The code for this article is available on GitHub

We used the numpy.reshape() method to convert the 3-dimensional array to 2-dimensional.

The reshape() method gives a new shape to an array without changing its data.

main.py
# โœ… make the array 2-dimensional x = x.reshape(-1, 2)

The only parameter we passed to the method is the new shape.

The new shape should be compatible with the original shape.

When one shape dimension is set to -1, the value is inferred from the length of the array and the remaining dimensions.

We could've also set the shape explicitly.

main.py
x = x.reshape(5, 2)

The example reshapes the 5-element 3-dimensional array to 2-dimensional.

You can also use the x.reshape(number_samples, nx * ny) formula.

main.py
import numpy as np from sklearn.linear_model import LinearRegression x = np.array( [ [[19.42, 43.4]], [[19.22, 43.9]], [[19.68, 44.1]], [[19.67, 44.2]], [[19.67, 44.2]] ] ) print(x.shape) # ๐Ÿ‘‰๏ธ (5, 1, 2) x = x.reshape(5, 1 * 2) print(x.shape) # ๐Ÿ‘‰๏ธ (5, 2)
The code for this article is available on GitHub

The array has 5 samples where each subarray is a single nested array that has 2 elements.

You could also access the last element of the Shape object when calling reshape().

main.py
import numpy as np from sklearn.linear_model import LinearRegression x = np.array( [ [[19.42, 43.4]], [[19.22, 43.9]], [[19.68, 44.1]], [[19.67, 44.2]], [[19.67, 44.2]] ] ) print(x.shape) # ๐Ÿ‘‰๏ธ (5, 1, 2) # make the array 2-dimensional x = x.reshape(-1, x.shape[-1]) print(x.shape) # ๐Ÿ‘‰๏ธ (5, 2) y = np.array([[41.4], [42.9], [44], [45.1], [41.2]]) print(y.shape) # ๐Ÿ‘‰๏ธ (5, 1) model = LinearRegression() reg = model.fit(x, y) print(reg.score(x, y)) # ๐Ÿ‘‰๏ธ 0.24559652025113898

flatten array to 2 dimensional

The code for this article is available on GitHub

When one shape dimension is set to -1, the value is inferred from the length of the array and the remaining dimensions.

main.py
print(x.shape) # ๐Ÿ‘‰๏ธ (5, 1, 2) # make the array 2-dimensional x = x.reshape(-1, x.shape[-1])

We used the shape attribute to determine the second argument of the reshape() method.

# 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