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

读取数据

复制代码
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
相关推荐
future14122 分钟前
C#每日学习日记
java·学习·c#
近津薪荼19 分钟前
初学者关于数据在内存中的储存的笔记
笔记
碎叶城李白2 小时前
若依学习笔记1-validated
java·笔记·学习·validated
im_AMBER3 小时前
学习日志05 python
python·学习
acstdm3 小时前
DAY 48 CBAM注意力
人工智能·深度学习·机器学习
真的想上岸啊4 小时前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
摸爬滚打李上进4 小时前
重生学AI第十六集:线性层nn.Linear
人工智能·pytorch·python·神经网络·机器学习
HuashuiMu花水木4 小时前
PyTorch笔记1----------Tensor(张量):基本概念、创建、属性、算数运算
人工智能·pytorch·笔记
asyxchenchong8884 小时前
ChatGPT、DeepSeek等大语言模型助力高效办公、论文与项目撰写、数据分析、机器学习与深度学习建模
机器学习·语言模型·chatgpt
rui锐rui5 小时前
大数据学习2:HIve
大数据·hive·学习