机器学习-保险花销预测笔记+代码

读取数据

复制代码
import numpy as np
import pandas as pd

data=pd.read_csv(r'D:\人工智能\python视频\机器学习\5--机器学习-线性回归\5--Lasso回归_Ridge回归_多项式回归\insurance.csv',sep=',')
data.head(n=6)

EDA 数据探索

复制代码
import matplotlib.pyplot as plt
%matplotlib inline

plt.hist(data['charges'])
复制代码
#上图出现右偏现象,要变成正态分布形式
plt.hist(np.log(data['charges']),bins=20)

特征工程

复制代码
data=pd.get_dummies(data)
data.head()
复制代码
x=data.drop('charges',axis=1)
x
复制代码
y=data['charges']

x.fillna(0,inplace=True)
y.fillna(0,inplace=True)

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)

from sklearn.preprocessing import StandardScaler
scaler=StandardScaler(with_mean=True,with_std=True).fit(x_train)


x_train_scaled=scaler.transform(x_train)
x_test_scaled=scaler.transform(x_test)
x_train_scaled
复制代码
from sklearn.preprocessing import PolynomialFeatures
poly_features=PolynomialFeatures(degree=2,include_bias=False)
x_train_scaled=poly_features.fit_transform(x_train_scaled)
x_test_scaled=poly_features.fit_transform(x_test_scaled)

模型训练

复制代码
from sklearn.linear_model import LinearRegression


reg=LinearRegression()

reg.fit(x_train_scaled,np.log1p(y_train))
y_predict=reg.predict(x_test_scaled)

#%%
from sklearn.linear_model import Ridge
ridge=Ridge(alpha=0.4)

ridge.fit(x_train_scaled,np.log1p(y_train))
y_predict_ridge=ridge.predict(x_test_scaled)
#%%
from sklearn.ensemble import GradientBoostingRegressor
booster=GradientBoostingRegressor()

booster.fit(x_train_scaled,np.log1p(y_train))
y_predict_booster=ridge.predict(x_test_scaled)

模型评估

复制代码
from sklearn.metrics import mean_squared_error

#log变换之后的
log_rmse_train=np.sqrt(mean_squared_error(y_true=np.log1p(y_train),y_pred=reg.predict(x_train_scaled))) 
log_rmse_test=np.sqrt(mean_squared_error(y_true=np.log1p(y_test),y_pred=y_predict))
#没有做log变换的
rmse_train=np.sqrt(mean_squared_error(y_true=y_train,y_pred=np.exp(reg.predict(x_train_scaled))))
rmse_test=np.sqrt(mean_squared_error(y_true=y_test,y_pred=np.exp(reg.predict(x_test_scaled))))

log_rmse_train,log_rmse_test,rmse_train,rmse_test
python 复制代码
#log变换之后的
log_rmse_train=np.sqrt(mean_squared_error(y_true=np.log1p(y_train),y_pred=ridge.predict(x_train_scaled))) 
log_rmse_test=np.sqrt(mean_squared_error(y_true=np.log1p(y_test),y_pred=y_predict_ridge))
#没有做log变换的
rmse_train=np.sqrt(mean_squared_error(y_true=y_train,y_pred=np.exp(ridge.predict(x_train_scaled))))
rmse_test=np.sqrt(mean_squared_error(y_true=y_test,y_pred=np.exp(ridge.predict(x_test_scaled))))

log_rmse_train,log_rmse_test,rmse_train,rmse_test
python 复制代码
#log变换之后的
log_rmse_train=np.sqrt(mean_squared_error(y_true=np.log1p(y_train),y_pred=booster.predict(x_train_scaled))) 
log_rmse_test=np.sqrt(mean_squared_error(y_true=np.log1p(y_test),y_pred=y_predict_booster))
#没有做log变换的
rmse_train=np.sqrt(mean_squared_error(y_true=y_train,y_pred=np.exp(booster.predict(x_train_scaled))))
rmse_test=np.sqrt(mean_squared_error(y_true=y_test,y_pred=np.exp(booster.predict(x_test_scaled))))

log_rmse_train,log_rmse_test,rmse_train,rmse_test
相关推荐
小小张说故事17 分钟前
Seaborn 入门指南:让统计数据可视化更优雅
python·机器学习
小小张说故事17 分钟前
Transformers 入门指南:用 Python 调用预训练大模型
python·机器学习
小小张说故事17 分钟前
FastAPI 入门指南:用 Python 极速构建 API
python·机器学习
小小张说故事19 分钟前
pytest 入门指南:Python 自动化测试的标配工具
python·机器学习
小小张说故事33 分钟前
XGBoost 入门指南:Python 梯度提升实战
python·机器学习
罗西的思考1 小时前
机器人 / 物理 Agent Harness 综合分析与对比:从「更强的模型」到「更好的系统」
人工智能·算法·机器学习
一隅论数智5 天前
给AI一张“业务概念地图“:本体如何从哲学走向企业智能
大数据·人工智能·经验分享·笔记·学习·学习方法·政务
XiHongShi20165 天前
STM32F407 RTC定时器例程,建议保存
stm32·单片机·学习
爱吃苹果的日记本5 天前
离散数学第六课
学习·离散数学