【发布时间】:2020-02-01 06:48:17
【问题描述】:
我的情节有一条很粗的线,这是我没想到的,也无法自己解决。我不知道如何显示图像。
在 Kaggle 的 Craigslist Auto 数据集上进行 EDA。我想显示,然后比较和对比每个独特车辆制造商和型号(即福特 F150)的线性和多项式回归拟合,关联价格和型号年份。
如何使用更正常的线条绘制以下图,线条宽度不会改变任何内容。
df_f150=df[df['Make and Model']=='ford F-150']
#plotting a linear regression line for each dataframe
fig = plt.figure(figsize=(10,7))
sns.regplot(x=df_f150.year, y=df_f150.price, color='b')
'#Here is where I try to do one of the polynomial regressions'
# Legend, title and labels.
#plt.legend(labels=x)
plt.title('Relationship Between Model Year and Price', size=24)
plt.xlabel('Year', size=18)
plt.ylabel('Price', size=18)
plt.xlim(1990,2020)
plt.ylim(1000,100000)
from sklearn.preprocessing import PolynomialFeatures
X = df_f150['year'].values.reshape(-1,1)
y = df_f150['price'].values.reshape(-1,1)
poly = PolynomialFeatures(degree = 8)
poly.fit_transform(X)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
regressor = LinearRegression()
regressor.fit(X_train, y_train) #training the algorithm
#To retrieve the intercept:
print(regressor.intercept_)
#For retrieving the slope:
print(regressor.coef_)
y_pred = regressor.predict(X_test)
dfres = pd.DataFrame({'Actual': y_test.flatten(), 'Predicted': y_pred.flatten()})
dfres
plt.scatter(X_test, y_test, color='gray')
plt.plot(X_test, y_pred, color='red', linewidth=2)
plt.show()
【问题讨论】:
-
图表的图像也会有所帮助。你可以阅读this 并添加图片吗?
-
感谢您的链接。我添加了图像,标签是错误的,但这就是它的样子。
-
我发现您的问题不清楚。您的代码似乎与您显示的图片不匹配,因此很难理解您到底在哪里遇到了困难。是非常繁琐的数据框创建列表,
regplot()的长列表还是回归的输出?请仅提供代码的相关部分。见How to ask和Minimal, Complete, and Verifiable example -
@DizietAsahi 感谢您的反馈,进行了一些额外的编辑,希望能够澄清。主要是想解决绘制回归输出的问题。
-
除非您实际生成Minimal, Complete, and Verifiable example,否则我无法重现该问题,这是一些可以复制和粘贴的代码,会产生有问题的结果。
标签: python pandas matplotlib scikit-learn seaborn