valueerror: expected 2d array, got 1d array instead – Code Example

Total
0
Shares

In this article we will look at Python code example to resolve valueerror: expected 2d array, got 1d array instead. Some of the functions in Python expects 2d array as input. One such function is predict() from SciKit. It performs classification on samples in input array.

Code Example

Error Code – Let’s first reproduce the error –

import numpy as np
from sklearn.ensemble import HistGradientBoostingClassifier

X = np.array([0, 1, 2, np.nan])
y = [0, 0, 1, 1]

gbdt = HistGradientBoostingClassifier(min_samples_leaf=1).fit(X, y)
print(gbdt.predict(X))

This code will throw error –

Traceback (most recent call last):
  File "HelloWorld.py", line 14, in <module>
    gbdt = HistGradientBoostingClassifier(min_samples_leaf=1).fit(X, y)
    raise ValueError(
ValueError: Expected 2D array, got 1D array instead:
array=[ 0.  1.  2. nan].
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.

As you can see from the output that a ValueError: Expected 2D array, got 1D array instead: is thrown. Also, compiler gave us the solution too. You need to reshape the data to solve this.

Solution

1. Reshape the data using array.reshape()

import numpy as np
from sklearn.ensemble import HistGradientBoostingClassifier

X = np.array([0, 1, 2, np.nan]).reshape(-1, 1)
y = [0, 0, 1, 1]

gbdt = HistGradientBoostingClassifier(min_samples_leaf=1).fit(X, y)
print(gbdt.predict(X))

Output –

[0 0 1 1]

2. Converting a 1D array to 2D using [[]]

import numpy as np
from sklearn.ensemble import HistGradientBoostingClassifier

X = np.array([0, 1, 2, np.nan]).reshape(-1, 1)
y = [0, 0, 1, 1]

gbdt = HistGradientBoostingClassifier(min_samples_leaf=1).fit(X, y)
print(gbdt.predict([[5]]))

Output –

[1]