Trying to make linear regression model fro machine learning, keep getting error

Question:

I recently posted here saying that I kept getting an error with my input, in which people told me to use numpy’s reshape command. However I keep getting this error now from this code:

X_train= X_train.reshape(-1, 1)
X_test = X_test.reshape(-1, 1)
y_train = y.reshape(-1, 1)
myModel = LinearRegression() 

myModel.fit(X_train,y_train)

Error:
‘Series’ object has no attribute ‘reshape’
this comes up when running the first line.

Asked By: acidic231

||

Answers:

X_train= X_train.reshape(-1, 1)
X_test = X_test.reshape(-1, 1)

myModel = LinearRegression()

myModel.fit(X_train,y_train)

Answered By: Firas Farjallah

Instead of using:
X_train.reshape(-1,1)

Try using:
X_train.values.reshape(-1,1)

Overall code:

X_train = X_train.values.reshape(-1, 1)
X_test = X_test.values.reshape(-1, 1)
y_train = y_train.values.reshape(-1, 1)
myModel = LinearRegression() 

myModel.fit(X_train, y_train)
Answered By: cheekysim

Try using

X_train= X_train.values.reshape(-1, 1)
X_test = X_test.values.reshape(-1, 1)
y_train = y.values.reshape(-1, 1)
myModel = LinearRegression() 

myModel.fit(X_train,y_train)

It will work

Answered By: sumit mandal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.