将我的测试样品与numpy对齐

时间:2016-09-07 04:48:07

标签: python numpy machine-learning linear-regression

当我尝试运行简单的线性回归样本时,我收到此错误:

ValueError: shapes (1,8) and (1,) not aligned: 8 (dim 1) != 1 (dim 0).

我的代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing, cross_validation, svm


df = pd.read_csv('table.csv') 
print (df.head())



df = df[['Price', 'Asset']]
x = np.array(df.Price)
y = np.array(df.Asset)



x_train, x_test, y_train, y_test = cross_validation.train_test_split(
x, y, test_size=0.33)

x_train = np.pad(x, [(0,0)], mode='constant')
x_train.reshape((23,1))

y_train = np.pad(y, [(0,0)], mode ='constant')
y_train.reshape((23,1))

x_train = np.arange(23).reshape((23, 1))
x_train = x.reshape((23, 1))
c = x.T
np.all(x_train == c)


clf = LinearRegression()
clf.fit(x_train, y_train)
print(clf.coef_)
print(clf.predict(x_test - y_test) **2)
print(clf.score(x_test, y_test))

plt.scatter(x_test, y_test, color='black')
plt.plot(x_test, clf.predict(x_test), color='blue')

plt.xlabel('Price')
plt.ylabel('Asset')

plt.show()

我搜索过类似的问题,但没有找到任何帮助。我的测试大小是8个样本,占我23个人口的33%(下面的追溯有我的人口片段)。这是我的错误消息的完整回溯:

        Price    Asset
0    87.585859    191
1    87.839996    232
2    87.309998    245
3    88.629997    445
4    88.379997    393
[ 29.67925381]
C:\Users\HP\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)
Traceback (most recent call last):

  File "<ipython-input-10-030ffa933525>", line 1, in <module>
    runfile('C:/Users/HP/Documents/linear.py', wdir='C:/Users/HP/Documents')

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
    execfile(filename, namespace)

  File "C:\Users\HP\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/HP/Documents/linear.py", line 44, in <module>
    print(clf.predict(x_test - y_test) **2)

  File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 200, in predict
    return self._decision_function(X)

  File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 185, in _decision_function
    dense_output=True) + self.intercept_

  File "C:\Users\HP\Anaconda3\lib\site-packages\sklearn\utils\extmath.py", line 184, in safe_sparse_dot
    return fast_dot(a, b)

ValueError: shapes (1,8) and (1,) not aligned: 8 (dim 1) != 1 (dim 0)

谢谢!

0 个答案:

没有答案