Last updated: Apr 11, 2024
Reading timeยท3 min
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.
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
The x
variable stores a 3-dimensional array, however, the fit()
method takes
a 2-dimensional array for the training dataset.
x
array is (5, 1, 2)
- 5 subarrays with 1 subarray element and each subarray contains 2 elements.You can solve the error by using the numpy.reshape() method to reshape the array to 2-dimensional.
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
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.
# โ 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.
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.
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 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()
.
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
When one shape dimension is set to -1
, the value is inferred from the length
of the array and the remaining dimensions.
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.
You can learn more about the related topics by checking out the following tutorials: