我用线性回归的简单预测不会执行

时间:2016-02-12 22:46:18

标签: python scikit-learn regression prediction linear

以下是我的试用代码:

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:找到样本数不一致的数组:[1 5]

1 个答案:

答案 0 :(得分:0)

重新定义

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

来自fit(X, y)的文档:X:numpy数组或形状稀疏矩阵[n_samples,n_features]

在您的情况下,每个示例只有一个功能。