0

I am trying to perform a simple linear regression as follows

from sklearn.linear_model import LinearRegression
import numpy as np
x=np.arange(1,769)
data=np.genfromtxt('pima-indians-diabetes.data.csv',delimiter=',')
y=np.sort(data[:,4])
reg=LinearRegression()
reg.fit(x,y)

I am getting the following ValueError

ValueError:Found input variables with inconsistent numbers of samples: [1, 768] 

I even tried reshaping my X as

X.reshape(-1,1)

But it doesn't help.Where am I wrong?

  • can you print shape of `x` and `y` before passing them to the fit? – Mischa Lisovyi Jun 19 '18 at 18:07
  • its (768,) for both x and y – Annavaram Nishanth Jun 19 '18 at 18:13
  • then this seems to be a duplicate of https://stackoverflow.com/questions/30813044/sklearn-found-arrays-with-inconsistent-numbers-of-samples-when-calling-linearre – Mischa Lisovyi Jun 19 '18 at 18:15
  • But the accepted solution doesn't work for me..I already looked it up – Annavaram Nishanth Jun 19 '18 at 18:18
  • Did you print out the shapes after reshaping to make sure they are what you expect? – Mischa Lisovyi Jun 19 '18 at 18:20
  • `x` must be a matrix, not a vector. Try passing `x[:,np.newaxis]` instead of `x` to the function. `x.reshape(-1,1)` must have helped, too. How exactly "it doesn't help"? – DYZ Jun 19 '18 at 23:53
  • I've used x.reshape(-1,1) and then printed out the shape..it did not change, but when I printed out x it looked exactly how I wanted it to be.But then I tried x.shape=((768,1)),it worked for me.Why did this happen? – Annavaram Nishanth Jun 20 '18 at 01:43
  • Possible duplicate of [sklearn: Found arrays with inconsistent numbers of samples when calling LinearRegression.fit()](https://stackoverflow.com/questions/30813044/sklearn-found-arrays-with-inconsistent-numbers-of-samples-when-calling-linearre) – desertnaut Jun 20 '18 at 07:31
  • 1
    I just found out that x.reshape(-1,1) is returning a copy, without modifying x.It works though if I used x=x.reshape(-1,1) instead – Annavaram Nishanth Jun 20 '18 at 11:52

0 Answers0