简体   繁体   English

多项式回归绘图问题

[英]Polynomial Regression Plotting issue

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error, r2_score
import seaborn as sns
from sklearn import metrics
from matplotlib.pylab import rcParams
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression

df = pd.read_csv('FB.csv')
datapol = pd.DataFrame(df, columns=['Date', 'Close'])

ptrain, ptest = train_test_split(datapol, test_size=0.20, random_state=42)
px_train = np.array(ptrain.index).reshape(-1, 1)
py_train = ptrain['Close']

poly= PolynomialFeatures(degree=4)
X_train_poly= poly.fit_transform(px_train)
pol_train_reg = LinearRegression()
pol_train_reg.fit(X_train_poly, py_train)
py_train_pred = pol_train_reg.predict(X_train_poly)

plt.figure(1, figsize=(16, 10))
plt.title('Linear Regression | Price vs Time')
plt.scatter(px_train, py_train, edgecolor='w', label='Actual Price')
plt.plot(px_train, pol_train_reg.predict(poly.fit_transform(px_train)), color='r', label='Polynomial')
plt.xlabel('Integer Date')
plt.ylabel('Stock Price')
plt.legend()
plt.show()`

This is the method of polynomial regression .The problem is plotting the graph.这是多项式回归的方法。问题是绘制图形。 Here is the graph.这是图表。 Can anyone tell me how can i fix this ?谁能告诉我如何解决这个问题?

https://i.stack.imgur.com/gefcY.png https://i.stack.imgur.com/gefcY.png

I don't have access to your dataset so I had to create one from another tutorial Basically I think you need to sort your values otherwise it creates the "block" you've seen.我无权访问您的数据集,所以我必须从另一个教程中创建一个基本上我认为您需要对您的值进行排序,否则它会创建您所看到的“块”。

So, foremost, I had to add these lines:所以,最重要的是,我必须添加这些行:

sort_axis = operator.itemgetter(0)
sorted_zip = sorted(zip(x,y_poly_pred), key=sort_axis)
x, y_poly_pred = zip(*sorted_zip)

Full code:完整代码:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error, r2_score
import seaborn as sns
from sklearn import metrics
from matplotlib.pylab import rcParams
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LinearRegression

np.random.seed(0)
x = 2 - 3 * np.random.normal(0, 1, 20)
y = x - 2 * (x ** 2) + 0.5 * (x ** 3) + np.random.normal(-3, 3, 20)

# transforming the data to include another axis
px_train = x[:, np.newaxis]
y = y[:, np.newaxis]

poly= PolynomialFeatures(degree=4)
x_train_poly = polynomial_features.fit_transform(px_train)

X_train_poly= poly.fit_transform(px_train)
pol_train_reg = LinearRegression()
pol_train_reg.fit(X_train_poly, y)
py_train_pred = pol_train_reg.predict(X_train_poly)

plt.figure(1, figsize=(16, 10))
plt.title('Linear Regression | Price vs Time')
plt.scatter(px_train, y, edgecolor='w', label='Actual Price')

sort_axis = operator.itemgetter(0)
sorted_zip = sorted(zip(x,y_poly_pred), key=sort_axis)
x, y_poly_pred = zip(*sorted_zip)

plt.plot(x, y_poly_pred, color='m')
plt.xlabel('Integer Date')
plt.ylabel('Stock Price')
plt.legend()
plt.show()

This will generate a proper graph.这将生成一个合适的图形。 Admittedly the regression needs tuning, but that was not the point of the question:诚然,回归需要调整,但这不是问题的重点:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM