Theo de Quillacq
Theo de Quillacq

Reputation: 13

Linear regression on a dataframe -reshape error

I would like to create a linear regression on my data.

I have tried to reshape my dataframe into an array. I have been through the following questions:

pandas crashes on repeated DataFrame.reset_index()

sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

AttributeError: 'Series' object has no attribute 'reshape'

Invalid character in identifier

import numpy as np
from sklearn.linear_model import LinearRegression


X = df_ind["ppentq"]
y = df_ind["xsgay"]
z = df_ind['revtq']

reg = LinearRegression().fit(x, y)
reg.score(X, y)

I get the error: ValueError: Expected 2D array, got 1D array instead: array=[4.1835757 4.15731936 4.19720195 ... 7.46244352 7.51525252 7.58601316]. 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.

I want to reshape, but when I do:

AttributeError: 'Series' object has no attribute 'reshape'

I'm a total novice/self-taught. I appreciate any and all help.

Upvotes: 0

Views: 1126

Answers (2)

Rutwik Deshmukh
Rutwik Deshmukh

Reputation: 43

Actually the fit method requires your X-array to be 2D or higher. You can simply reshape the variable X using:


    X_matrix = X.values.reshape(-1,1)

That's it!!!!!

Upvotes: 0

Theo de Quillacq
Theo de Quillacq

Reputation: 13

Just use tensorflow. Also, use df.to_numpy() to transform into an array

Upvotes: 0

Related Questions