Python机器学习:不同行业工龄与薪水的线性回归模型
1.读取数据
import pandas as pd
df1=pd.read_excel('IT行业收入表.xlsx ')
df1.head(5)
结果显示:
x=df1[['工龄']] #自变量
y=df1['薪水'] #因变量
需要注意的是:自变量需要写成二维结构,因为要符合多元回归的逻辑。
2.搭建模型
from sklearn.linear_model import LinearRegression
regr=LinearRegression()
regr.fit(x,y)
3.模型可视化
from matplotlib import pyplot as plt
plt.scatter(x,y)
plt.plot(x,regr.predict(x),color='red')
plt.xlabel('工龄')
plt.ylabel('薪水')
plt.show()
结果显示:
4.线性回归方程构造
通过coef_和intercept属性可以获得系数和截距
print('系数a'+str(regr_coef_[0]))
print('截距b'+str(regr.intercept_))
结果显示:
因此,拟合得到的一元线性回归方程是y=2497x+10143
5.模型评估
在这里插入代码片