我的Python线性回归有不完全lin

2024-06-28 11:12:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想画一个线性回归图。但这条线是不完整的。下面是我的Python代码:

import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score

features = [[2],[4],[8],[5]]
labels = [320, 610, 1190, 726]

plt.scatter(features,labels,color="black")
plt.xlabel("Number of room")
plt.ylabel('price')

clf = linear_model.LinearRegression()
clf=clf.fit(features,labels)
result = clf.predict([[11],[9]])

plt.plot([[11],[9]], result, color='blue', linewidth=3)

print(result)
plt.show()

图为: enter image description here

从图中可以看出这条线是不完整的。请帮我解决这个问题。使行与所有其他值一起完成


Tags: 代码fromimportlabelsmodelasplt线性