【问题标题】:my simple prediction with linear regression wont execute我的线性回归简单预测不会执行
【发布时间】:2016-05-24 05:55:09
【问题描述】:

下面是我的试用代码:

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]

【问题讨论】:

    标签: python scikit-learn regression linear-regression prediction


    【解决方案1】:

    重新定义

    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] 的稀疏矩阵

    在你的例子中,每个例子只有一个特征。

    【讨论】:

    • 我运行了简单回归,但没有显示输出。为什么?我做错了什么?
    • 打印predicted 。这是你的预测。@user2979063
    • 将 numpy 导入为 np x_train = np.array([5, 9, 33, 25, 4]) y_train = np.array([35, 2, 14 ,9, 7]) x_test = np.array([14,...])
    猜你喜欢
    • 2015-06-19
    • 2018-04-02
    • 2021-02-27
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多