ValueError: Expected 2D array, got 1D array instead [Fixed]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# ValueError: Expected 2D array, got 1D array instead [Fixed]

The Python "ValueError: Expected 2D array, got 1D array instead" occurs when you pass a 1-dimensional array to a function that expects a 2-dimensional array.

To solve the error, reshape the numpy.reshape() method to make the array two-dimensional.

Here is an example of how the error occurs.

main.py
import numpy as np from sklearn.linear_model import LinearRegression # ๐Ÿ‘‡๏ธ both arrays are 1-dimensional x = np.array([1, 5, 3, 2, 1]) y = np.array([2, 4, 6, 8, 10]) model = LinearRegression() # โ›”๏ธ ValueError: Expected 2D array, got 1D array instead: # array=[1 5 3 2 1]. # Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. reg = model.fit(x, y)

expected 2d array got 1d array instead

We created two 1-dimensional arrays and instantiated the LinearRegression class.

The error occurs when calling the fit() method.

The method expects to get called with a 2-dimensional array for the input features (x) and a 1-dimensional array for the target values (y).

Calling the fit() method with two 1-dimensional arrays caused the error.

One way to solve the error is to use the reshape() method to reshape x into a 2-dimensional array.

main.py
import numpy as np from sklearn.linear_model import LinearRegression # both arrays are 1-dimensional x = np.array([1, 5, 3, 2, 1]) y = np.array([2, 4, 6, 8, 10]) x = x.reshape(-1, 1) # [[1] # [5] # [3] # [2] # [1]] print(x) 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 give a new shape to the x array without changing its data.

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

We used the method to construct a 2-dimensional array where each subarray has 1 element.

# Wrap the one-dimensional array in a set of square brackets

You might also commonly get the error when using the reshape() method.

main.py
import numpy as np from sklearn.linear_model import LinearRegression X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 reg = LinearRegression().fit(X, y) print(reg.score(X, y)) # โ›”๏ธ ValueError: Expected 2D array, got 1D array instead: # array=[3 5]. # Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. arr = np.array([3, 5]) print(reg.predict(arr))

Notice that we passed a one-dimensional array to the predict() method.

To solve the error, wrap the array in an extra set of square brackets to make it a two-dimensional array.

main.py
import numpy as np from sklearn.linear_model import LinearRegression X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 reg = LinearRegression().fit(X, y) print(reg.score(X, y)) arr = np.array([3, 5]) # ๐Ÿ‘‡๏ธ Wrap in square brackets [] print(reg.predict([arr])) # ๐Ÿ‘‰๏ธ [16.]

using extra set of square brackets to solve error

The code for this article is available on GitHub
Notice that we wrapped the one-dimensional array in an extra set of square brackets to make it a two-dimensional array.

Now the predict() method gets called with a two-dimensional array and everything works as expected.

This is necessary because the training data (X) is a two-dimensional array.

We have to use the predict() method on data that is of the same dimensionality as the training data.

You can also use the reshape() method to solve the error.

main.py
import numpy as np from sklearn.linear_model import LinearRegression X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]]) y = np.dot(X, np.array([1, 2])) + 3 reg = LinearRegression().fit(X, y) print(reg.score(X, y)) arr = np.array([3, 5]) arr = arr.reshape(1, -1) print(arr) # ๐Ÿ‘‰๏ธ [[3 5]] print(reg.predict(arr)) # ๐Ÿ‘‰๏ธ [16.]
The code for this article is available on GitHub
  1. We used the reshape() method to convert the 1-dimensional array to 2-dimensional.
  2. We removed the set of square brackets [] when calling reshape().

# 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