#!/usr/bin/env python
# coding: utf-8

# In[1]:


# 多项式回归

# 导入包
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


# In[2]:


# 导入数据集
dataset = pd.read_csv('职位工资.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values


# In[3]:


# 将线性回归代入数据集
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)


# In[4]:


# 将多项式回归代入数据集
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
poly_reg.fit(X_poly, y)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, y)


# In[5]:


# 将线性回归的结果可视化
plt.scatter(X, y, color = 'red')
plt.plot(X, lin_reg.predict(X), color = 'blue')
plt.title('Linear Regression')
plt.xlabel('level')
plt.ylabel('Salary')
plt.show()


# In[6]:


# 将多项式结果可视化
plt.scatter(X, y, color = 'red')
plt.plot(X, lin_reg_2.predict(poly_reg.fit_transform(X)), color = 'blue')
plt.title('Polynomial Regression')
plt.xlabel('level')
plt.ylabel('Salary')
plt.show()


# In[7]:


# 使用线性回归预测新值
lin_reg.predict([[6.5]])


# In[8]:


# 使用多项式回归预测新值
lin_reg_2.predict(poly_reg.fit_transform([[6.5]]))


# In[ ]:





