回归线绘图

我正在尝试在散点图上根据我的预测数据绘制一条回归线。

问题是我应该得到一条线,但我的图表上却有很多连接所有点的线(见图)https://i.sstatic.net/VF483.png

在根据其他数据预测二氧化碳排放后,我绘制了测试引擎尺寸与测试的实际数据(co2emissions)的关系图,并且我试图在引擎尺寸与测试的预测数据上绘制线,但做不到。

这是代码:

#import the datasetdf = pd.read_csv('FuelConsumptionCo2.csv')cols = ['ENGINESIZE','CYLINDERS','FUELTYPE','FUELCONSUMPTION_CITY','FUELCONSUMPTION_HWY','FUELCONSUMPTION_COMB','CO2EMISSIONS']#create new dataset with colums neeededcdf = df[cols]#dummies for the categorigal column fueltypecdf = pd.get_dummies(cdf,'FUELTYPE')#the features without the target columnselFeatures = list(cdf.columns.values)del selFeatures[5]#split the dataset for fittingX_train, X_test, Y_train, Y_test = train_test_split(cdf[selFeatures], cdf['CO2EMISSIONS'], test_size=0.5)#regression modelclfregr = linear_model.LinearRegression()#train the modelclfregr.fit(X_train, Y_train)#predict the valuestrain_pred = clfregr.predict(X_train)test_pred = clfregr.predict(X_test)#regression line for the predicted in testplt.scatter(X_test.ENGINESIZE,Y_test,  color='gray')plt.plot(X_test.ENGINESIZE, test_pred, color='red', linewidth=1)plt.show()

回答:

数据中有9个独立变量。因此,如果只根据其中一个变量绘图,你会得到每个ENGINESIZE值的重复。这并不会形成可绘制的函数。当你试图绘制一条线时,它会在这些多个垂直点之间 zigzag 变化。

enter image description here

请注意,当我们对预测值进行scatterplot绘图时,我们会在一条垂直线上有很多点——这些点对应于你正在x-axis上绘图的变量之外的其他八个独立变量的不同值:

 plt.scatter(X_test.ENGINESIZE, test_pred, color='yello') # , linewidth=1)

enter image description here

我得说,sklearnLinearRegression类使用起来相当困难。我改用了statsmodels

plt.scatter(X_test.ENGINESIZE,Y_test,  color='gray')import statsmodels.formula.api  as smfy = Y_trainX = X_traindf = pd.DataFrame({'x' : X.ENGINESIZE, 'y': y})smod = smf.ols(formula ='y~ x', data=df)result = smod.fit()plt.plot(df['x'], result.predict(df['x']), color='red', linewidth=1)plt.show()

enter image description here

然后为了额外的分数

print(result.summary())

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注