Last updated: Apr 11, 2024
Reading timeยท4 min
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.
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)
We created two 1-dimensional arrays and instantiated the LinearRegression class.
The error occurs when calling the fit()
method.
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.
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
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.
You might also commonly get the error when using the reshape()
method.
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.
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.]
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.
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.]
reshape()
method to convert the 1-dimensional array to
2-dimensional.[]
when calling reshape()
.You can learn more about the related topics by checking out the following tutorials: