ValueError: Found array with dim 3. Estimator expected 2

Borislav Hadzhiev

Last updated: Jul 1, 2023

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.

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`

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

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

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.

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

- ValueError: cannot reshape array of size X into shape Y
- lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT
- How to draw empty circles on a Scatter Plot in Matplotlib
- TypeError: Image data cannot be converted to float [Solved]
- OverflowError: Python int too large to convert to C long
- How to use numpy.argsort in Descending order in Python
- TypeError: cannot pickle '_thread.lock' object [Solved]
- ValueError: DataFrame constructor not properly called [Fix]
- ufunc 'add' did not contain loop with signature matching types
- TypeError: Field elements must be 2- or 3-tuples, got 1
- ValueError: Expected 2D array, got 1D array instead [Fixed]
- Input contains infinity or value too large for dtype(float64)
- TypeError: ufunc 'isnan' not supported for the input types
- ValueError: columns overlap but no suffix specified [Solved]
- Drop Unnamed: 0 columns from a Pandas DataFrame in Python
- AttributeError: Can only use .dt accessor with datetimelike values
- Usecols do not match columns, columns expected but not found
- Using pandas.read_csv() with multiple delimiters in Python
- Convert Epoch to Datetime in a Pandas DataFrame