【问题标题】:What do the arguments passed inside the plt.plot() method mean?plt.plot() 方法内部传递的参数是什么意思?
【发布时间】: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


【解决方案1】:

plt 库绘制数据。第一个条目是 x 数据,第二个条目是 y 数据。其他输入可用于添加颜色、线宽或标记类型,如 the documentation 所示。

plt.scatter 添加数据的散点图。根据我怀疑的变量名称猜测:

plt.scatter(train.ENGINESIZE, train.CO2EMISSIONS,  color='blue')

使用蓝色标记绘制所有发动机尺寸及其对应的二氧化碳排放量的散点图。

plt.plot 画了一条线。通过我怀疑的变量名称来猜测

plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-y')

将以黄色绘制训练数据的线性回归。 train_x 是 x 数据,regr.coef_[0][0]*train_x + regr.intercept_[0] 是 y 数据(它遵循公式 y = a*x + b)。

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 2016-10-17
    • 2021-12-07
    • 1970-01-01
    • 2016-03-20
    • 2013-01-24
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多