需求
分析IT行业工龄和收入之间的规律,通常来说,收入都会随着工作年限的增长而增长,而在不同的行业中收入的 增长速度都会有所不同,本需求就是来通过一元线性回归模型来探寻工作年 限对收入的影响,也即搭建收入预测模型,同时比较多个行业的收入预测模 型来分析各个行业的特点。
数据
IT行业收入表.xlsx
工龄 | 薪水 |
0 | 10808 |
0.1 | 13611 |
0.2 | 12306 |
0.3 | 12151 |
0.3 | 13057 |
0.3 | 13883 |
0.4 | 12557 |
0.4 | 13013 |
0.4 | 13402 |
0.5 | 11502 |
0.5 | 13944 |
0.8 | 15944 |
0.8 | 12572 |
1 | 12939 |
1 | 13960 |
1 | 14827 |
1.1 | 13442 |
1.1 | 13798 |
1.2 | 14806 |
1.2 | 13966 |
1.2 | 13368 |
1.4 | 12261 |
1.4 | 12451 |
1.5 | 13170 |
1.5 | 14876 |
1.6 | 15750 |
1.6 | 16322 |
1.8 | 16686 |
1.8 | 13992 |
2.2 | 15325 |
2.2 | 15795 |
2.2 | 15410 |
2.3 | 15769 |
2.4 | 14425 |
2.4 | 15408 |
2.4 | 16236 |
2.5 | 15865 |
2.8 | 17160 |
2.9 | 16819 |
3 | 15860 |
3 | 18583 |
3.2 | 15209 |
3.3 | 15248 |
3.3 | 15838 |
3.5 | 18106 |
3.6 | 17057 |
3.6 | 18524 |
3.8 | 16424 |
代码
# 一元线性回归案例:分析IT行业工龄和收入之间的规律
# 【1】导入资源包
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
import pandas as pd
# 【2】加载样本数据,核心是给x,y赋值
df = pd.read_excel('IT行业收入表.xlsx')
print(df.head())
x = df[['工龄']]
y = df['薪水']
#【3】模型搭建
lr = LinearRegression()
#【4】模型训练
lr.fit(x,y)
#【5】模型预测
res = lr.predict([[3]])
print(res)
#拓展
#【6】显示模型参数
print('回归系数a:',str(lr.coef_))
print('截距b:',str(lr.intercept_))
print('模型函数: y = ',str(lr.coef_[0]),'*x +',str(lr.intercept_))
#【7】样本及模型可视化
plt.scatter(x,y)
plt.plot(x,lr.predict(x))
plt.show()
#【8】模型评估
import statsmodels.api as sma
res = sma.OLS(y,sma.add_constant(x)).fit()
print(res.summary())