多项式回归没有给出正确的假设

时间:2018-05-31 03:59:08

标签: machine-learning scikit-learn artificial-intelligence regression

PFB代码,这里X是职位等级的数量(即等级1,等级2 ..),y是工资范围。多项式回归没有给出最佳拟合线,我怎么能算出X = 20。 请建议:)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

X = [[ 1],[ 2],[ 3],[ 4],[ 5],[ 6],[ 7],[ 8],[ 9],[10]]
y = [45000,50000,60000,80000,110000,150000,200000,300000,500000,1000000]

from sklearn.linear_model import LinearRegression


from sklearn.preprocessing import PolynomialFeatures

poly_reg = PolynomialFeatures(degree = 2)
X_poly = poly_reg.fit_transform(X)


linearReg_2 = LinearRegression()
linearReg_2.fit(X_poly,y)

# plot polynomial linear regression
plt.scatter(X,y, color = "red")
plt.plot(X,linearReg_2.predict(X_poly), color = "blue")
plt.show()

1 个答案:

答案 0 :(得分:1)

您可以尝试将度数增加到3或4,以获得更合适的匹配。

poly_reg = PolynomialFeatures(degree = 3)

enter image description here

poly_reg = PolynomialFeatures(degree = 4)

enter image description here

要预测新值,您需要将其传递到同一个poly_reg对象,然后使用linearReg_2.predict()

这样的事情:

X_new = [[20]]

# Observe that I used transform() and not fit_transform()
X_new_poly = poly_reg.transform(X_new)

y_new = linearReg_2.predict(X_new_poly)