2016-02-12 81 views
0

下面是我的审判代码:我用线性回归简单的预测不会执行

from sklearn import linear_model 

# plt.title("Time-independent variant student performance analysis") 

x_train = [5, 9, 33, 25, 4] 
y_train = [35, 2, 14 ,9, 7] 
x_test = [14, 2, 8, 1, 11] 

# create linear regression object 
linear = linear_model.LinearRegression() 

#train the model using the training sets and check score 
linear.fit(x_train, y_train) 
linear.score(x_train, y_train) 

# predict output 
predicted = linear.predict(x_test) 

运行时,这是输出:

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

回答

0

重新定义

x_train = [[5],[9],[33],[25],[4]] 
y_train = [35,2,14,9,7] 
x_test = [[14],[2],[8],[1],[11]] 

从DOC fit(X, y)X:numpy数组或形状的稀疏矩阵[n_samples,n_features]

就你而言,每个例子只有一个功能。

+0

我已经运行了简单回归,但没有显示输出。为什么?我究竟做错了什么? – user2979063

+0

打印'预测'。这是你的预测。@ user2979063 – Farseer

+0

import numpy as np x_train = np.array([5,9,33,25,4]) y_train = np.array([35,2,14,9,7]) x_test = np.array([14,...]) – user2979063