Search code examples
pythonpandasscikit-learnnumpy-ndarraysklearn-pandas

Fix ValueError: shapes (1,2) and (4,4) not aligned: 2 (dim 1) != 4 (dim 0) in python


I am using sklearn with pandas to create and fit a Linear Regression Classifier to continue a chart.

The code i am using to create the the arrays is:

sample_data = pd.read_csv("includes\\csv.csv")
sample_datat = pd.read_csv("includes\\csvt.csv")

X_train= np.array(sample_data["day"])
y_train= np.array(sample_data["balance"])

X_test= np.array(sample_datat["day"])
y_test= np.array(sample_datat["balance"])


X_train = X_train.reshape(1, -1)
y_train = y_train.reshape(1, -1)


X_test = X_test.reshape(1, -1)
y_test = y_test.reshape(1, -1)

#plt.plot(X_train, y_train)
#plt.show()
clf = LinearRegression()
clf.fit(X_train, y_train)
clf.score(X_test, y_test)

The csv files that are being opened are:

balance,day

242537,28.5
246362,29.5
246659,30.5
246844,31.5

And:

246987,1.6
247230,2.6

The error message is just like in the title:

Traceback (most recent call last):
  File "main.py", line 140, in <module>
    predict()
  File "main.py", line 59, in predict
    clf.score(X_test, y_test)
  File "C:\Users\simon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\base.py", line 408, in score
    y_pred = self.predict(X)
  File "C:\Users\simon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\linear_model\base.py", line 221, in predict
    return self._decision_function(X)
  File "C:\Users\simon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\linear_model\base.py", line 206, in _decision_function
    dense_output=True) + self.intercept_
  File "C:\Users\simon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sklearn\utils\extmath.py", line 142, in safe_sparse_dot
    return np.dot(a, b)
ValueError: shapes (1,2) and (4,4) not aligned: 2 (dim 1) != 4 (dim 0)

Solution

  • You should reshape it as (-1,1) instead of (1,-1)