在Python中拟合此线性回归的问题

问题描述 投票:-3回答:1

我试图使用两个数组来练习要在哪里绘制它们-首先查看它们,然后创建一个线性回归模型,然后拟合数据并对其进行预测。

最后,我想绘制将它们分开的线性回归线,但我不能-它不能绘制。这是我的代码:

k_true = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
k_pred = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,21]
plt.scatter(k_true,k_pred)
model = LinearRegression()
model.fit([k_true],[k_pred])
predictor = model.predict([k_pred])
plt.scatter(k_true,k_pred)
plt.plot([k_true],predictor,color="red")

This is the result

python matplotlib machine-learning linear-regression
1个回答
2
投票

您正在绘制错误的自变量,并且predictor也需要用predictor[0]替换,因为它包含列表中的列表。您需要将k_true用作x。

plt.scatter(k_true, k_pred)
plt.plot(k_pred, predictor[0], '-k');

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.