使用 matplotlib 绘制 scikit learn 线性回归结果
Posted
技术标签:
【中文标题】使用 matplotlib 绘制 scikit learn 线性回归结果【英文标题】:Use matplotlib to plot scikit learn linear regression results 【发布时间】:2019-03-08 17:28:06 【问题描述】:如何在分析后绘制来自scikit learn
的线性回归结果,以在程序结束时查看“测试”数据(实际值与预测值)?下面的代码很接近,但我认为它缺少比例因子。
输入:
import pandas as pd
import numpy as np
import datetime
pd.core.common.is_list_like = pd.api.types.is_list_like # temp fix
import fix_yahoo_finance as yf
from pandas_datareader import data, wb
from datetime import date
from sklearn.linear_model import LinearRegression
from sklearn import preprocessing, cross_validation, svm
import matplotlib.pyplot as plt
df = yf.download('MMM', start = date (2012, 1, 1), end = date (2018, 1, 1) , progress = False)
df_low = df[['Low']] # create a new df with only the low column
forecast_out = int(5) # predicting some days into future
df_low['low_prediction'] = df_low[['Low']].shift(-forecast_out) # create a new column based on the existing col but shifted some days
X_low = np.array(df_low.drop(['low_prediction'], 1))
X_low = preprocessing.scale(X_low) # scaling the input values
X_low_forecast = X_low[-forecast_out:] # set X_forecast equal to last 5 days
X_low = X_low[:-forecast_out] # remove last 5 days from X
y_low = np.array(df_low['low_prediction'])
y_low = y_low[:-forecast_out]
X_low_train, X_low_test, y_low_train, y_low_test = cross_validation.train_test_split(X_low, y_low, test_size = 0.2)
clf_low = LinearRegression() # classifier
clf_low.fit(X_low_train, y_low_train) # training
confidence_low = clf_low.score(X_low_test, y_low_test) # testing
print("confidence for lows: ", confidence_low)
forecast_prediction_low = clf_low.predict(X_low_forecast)
print(forecast_prediction_low)
plt.figure(figsize = (17,9))
plt.grid(True)
plt.plot(X_low_test, color = "red")
plt.plot(y_low_test, color = "green")
plt.show()
图片:
【问题讨论】:
我认为您刚刚分别绘制了测试数据 x (X_low_test
) 和 y (y_low_test
)。您要做的是根据y_low_test
预测新的 y 值,例如:X_low_predicted = clf_low.predict(y_low_test)
。之后,在plt.plot(X_low_test, y_low_test, label='real'); plt.plot(X_low_predicted, y_low_test, label='predicted'); plt.legend()
测试集的预测值上绘制测试集
【参考方案1】:
如果你想比较目标和预测,你应该绘制y_test
和X_test
,而你应该绘制y_test
和clf_low.predict(X_test)
。
顺便说一句,您的代码中的clf_low
不是分类器,而是回归器。最好使用别名model
而不是clf
。
【讨论】:
以上是关于使用 matplotlib 绘制 scikit learn 线性回归结果的主要内容,如果未能解决你的问题,请参考以下文章
如何在用 scikit-learn / matplotlib 绘制的混淆矩阵中格式化 xticklabels?
Scikit Learn Xticks Matplotlib [重复]
绘制阈值(precision_recall 曲线)matplotlib/sklearn.metrics