简体   繁体   中英

Running sklearn linear regression, getting “arrays with inconsistent numbers of samples” error

I'm trying to run a simple linear regression of in sklearn. I have a Pandas dataframe with two columns, "Likes" and "Attendance" Both columns have 18 samples.

lr = LinearRegression()
lr.fit(Likes,Attendance)

I get the following error:

C:\\Anaconda3\\lib\\site-packages\\sklearn\\utils\\validation.py:386: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)

...

ValueError: Found arrays with inconsistent numbers of samples: [ 1 18]

Even after I reshaped the data using Likes.reshape(-1, 1), I got the same error.

Can anyone help?

You have a single feature in your data, therefore (as suggested in the error you posted) "Reshape your data ... using X.reshape(-1, 1)".

lr = LinearRegression()
lr.fit(X=Likes.reshape(-1, 1), y=Attendance)

I see that you tried this. Does my code above not work?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM