Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to do a linear regression with variables from a dataframe [Python]

I have two variables A and B in a dataframe. When i try this:

x=df.['A']
y=df.['B']

M = LinearRegression()
M.fit(A,B)

I get the following error

Expected 2D array, got 1D array instead:
array=[86.  0. 86. ...  0.  0.  0.].
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.

Although when i try to apply the reshape as suggested, i got a "’Series’ object has no attribute ‘reshape’"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I get around this?

>Solution :

You can try to change x to DataFrame rather than Series.

x = df[['A']]
y = df['B']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading