多项式回归(特定值)的预测不准确?

时间:2019-04-09 15:49:33

标签: python-3.x numpy machine-learning

我建立了一个简单的线性和多项式回归模型,以从绘图中获取特定值(或进行预测,但由于无法获得特定值,所以我该如何预测;-;)

在spyder ver 3.3的python 3.x中。我试图重塑可用于线性回归并给出准确结果的数组,但是对于多项式回归,我要么得到一个错误,要么得到一个偏离预期结果的结果

#importing of libs import numpy as np import matplotlib.pyplot as plt import pandas as pd

# Retrieving dataset dataset = pd.read_csv('Position_salaries.csv')

# Seperating the indpendant variable colum from dependant column x = dataset.iloc[:, 1:-1].values y = dataset.iloc[:, 2].values

# Fitting in linear regression from sklearn.linear_model import LinearRegression lin_reg = LinearRegression() lin_reg.fit(x,y)

# Fitting of a poly model from sklearn.preprocessing import PolynomialFeatures poly = PolynomialFeatures(3) x_poly = poly.fit_transform(x) # fit poly regression lin_reg2 = LinearRegression() lin_reg2.fit(x_poly, y)

# Visualization of linear regression plt.scatter(x,y, color = 'red') plt.plot(x, lin_reg.predict(x)) plt.show()

# Visualisation of poly regression x_grid = np.arange(min(x), max(x), 0.1) x_grid = np.reshape(x_grid, (len(x_grid),1)) plt.scatter(x,y, color = 'red') plt.plot(x_grid, lin_reg2.predict(poly.fit_transform(x_grid))) plt.show()

# Predicting with linear regression model value_needed = np.array(6.5).reshape(-1,1) lin_reg.predict(np.array(6.5).reshape(-1,1))

#predicting with polynomial regression model lin_reg2.predict(poly.fit_transform(value_needed))

对于线性模型,这是准确的结果

value_needed = np.array(6.5).reshape(-1,1) lin_reg.predict(np.array(6.5).reshape(-1,1)) Out[17]: array([330378.78787879]) 而对于多项式,我得到-

lin_reg2.predict(poly.fit_transform(value_needed)) Out[18]: array([133259.46969697]) 这与实际结果相去甚远(158862.45265)

My code and Dataset

0 个答案:

没有答案