【发布时间】:2020-08-18 06:43:36
【问题描述】:
我试图使用 scikit learn 和 matplotlib 在 python 中绘制线性回归模型。但是,当我使用 plt.scatter() 和 plt.plot() 绘制数据时,代码变得混乱
这是我使用 sklearn 对数据进行建模的代码:-
from sklearn import linear_model
regr = linear_model.LinearRegression()
train_x = np.asanyarray(train[['ENGINESIZE']])
train_y = np.asanyarray(train[['CO2EMISSIONS']])
regr.fit (train_x, train_y)
# The coefficients
print ('Coefficients: ', regr.coef_)
print ('Intercept: ',regr.intercept_)
这是我在图表上绘制线性回归模型的代码:-
plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS, color='blue')
plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-y')
plt.xlabel("Engine size")
plt.ylabel("Emission")
我不明白plt.scatter() 和plt.plot() 中传递的参数。我注意到当我删除方法plt.plot() 时,最佳拟合线没有绘制在图表上。
【问题讨论】:
-
...您查看过这些功能的文档吗?这通常是最好的查看位置...您没有显示您的导入,所以我只能猜测您作为
plt(或分配给它)导入的内容,否则我会自己链接到文档。跨度> -
请阅读the docs。
'-y'表示绘制一条连接所提供坐标的黄线。
标签: python matplotlib plot scikit-learn