我用线性回归的简单预测不会执行
my simple prediction with linear regression wont execute
下面是我的试用代码:
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]
重新定义
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]
的稀疏矩阵
在您的例子中,每个示例都只有一个特征。
下面是我的试用代码:
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]
重新定义
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]
在您的例子中,每个示例都只有一个特征。