import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
#载入数据
data = np.genfromtxt("job.csv",delimiter=",")
x_data = data[1:, 1, np.newaxis]
y_data = data[1:, 2, np.newaxis]
model = LinearRegression()
model.fit(x_data, y_data)
# 定义多项式回归,degree的值可以调节多项式的特征
poly_reg = PolynomialFeatures(degree=3)
#特征处理
x_poly = poly_reg.fit_transform(x_data)
# 定义回归模型
lin_reg = LinearRegression()
#训练模型
lin_reg.fit(x_poly,y_data)
# 画图
plt.plot(x_data,y_data, 'b.' )
plt.plot(x_data, lin_reg.predict(poly_reg.fit_transform(x_data)), c='r')
plt.title("Truth of Bluff (Polynomian Regression)")
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()