Sklearn: ValueError: found input variables with inconsistent number of samples: [1, 6]

X = [ 1994.  1995.  1996.  1997.  1998.  1999.]
y = [1.2 2.3 3.4 4.5 5.6 6.7]
clf = LinearRegression()
clf.fit(X,y)

This gives the above error. Both X and y are numpy arrays

How to remove this error?

I tried the method given here and changed X and y with X.reshape((-1,1))and y.reshape((-1,1)). However, this did not work.

0
source share
1 answer

This works for me well. Before rebuilding, make sure the arrays are numpy arrays.

import numpy as np
from sklearn.linear_model import LinearRegression

X = np.asarray([ 1994.,  1995.,  1996.,  1997.,  1998.,  1999.])
y = np.asarray([1.2, 2.3, 3.4, 4.5, 5.6, 6.7])

clf = LinearRegression()
clf.fit(X.reshape(-1,1),y)


clf.predict([1997])
#Output: array([ 4.5])

clf.predict([2001])
#Output: array([ 8.9])
0
source

Source: https://habr.com/ru/post/1684586/


All Articles