Home > database >  Expected 2D array, got 1D array instead error in sklearn regresion models
Expected 2D array, got 1D array instead error in sklearn regresion models

Time:03-02

I started machine learning recently and when I came across regression models I found out that to train models we use regressor.fit method which takes 2 argument observation and result but observation array are 2d and result array is 1D. Can anyone tell me why we don't use the same dimension array for the fit method? when I tried to fit the same dimension array it gave me an error as class expected 2d array but 1d was given

dataset = pd.read_csv("Position_Salaries.csv")
x= dataset.iloc[:,1:-1].values
y = dataset.iloc[:,-1].values
print(y)
print(x)
from sklearn.tree import DecisionTreeRegressor
reg = DecisionTreeRegressor(random_state=0)
reg.fit(x,y)
 Output - 
[  45000   50000   60000   80000  110000  150000  200000  300000  500000
 1000000]

[[ 1]
 [ 2]
 [ 3]
 [ 4]
 [ 5]
 [ 6]
 [ 7]
 [ 8]
 [ 9]
 [10]]

CodePudding user response:

To make the data conform to sklearn's preferred format, try the following:

reg.fit(x.reshape(-1,1),y.reshape(-1,1))
  • Related