2021-10-27

# K折交叉验证
from sklearn.model_selection import KFold
​
kf = KFold(n_splits=10) # 10折
​
rmse_scores = [] # 保存10折运行的结果
​
​
for train_indices, test_indices in kf.split(X): # 分割元数据,生成索引
    X_train, X_test = X[train_indices], X[test_indices] # 训练集和验证集
    y_train, y_test = y[train_indices], y[test_indices] # 训练标签集和验证标签集
    # 初始化线性回归模型对象
    LR = LinearRegression(normalize=True)
    LR.fit(X_train, y_train) # 训练
    y_pred = LR.predict(X_test) # 预测
    rmse = np.sqrt(mean_absolute_error(np.log(y_test), np.log(abs(y_pred)))) # 评估
    rmse_scores.append(rmse) # 累计每一轮的验证结果
   
​
print("rmse scores : ", rmse_scores)
print(f'average rmse score : {np.mean(rmse_scores)}')
# 2 随机森林(回归)
from sklearn.ensemble import RandomForestRegressor​# K折交叉验证kf = KFold(n_splits=10)​rmse_scores = []for train_indices, test_indices in kf.split(X):    X_train, X_test = X[train_indices], X[test_indices]    y_train, y_test = y[train_indices], y[test_indices]    # 初始化模型    RFR = RandomForestRegressor() # 基模型    # 训练/fit拟合    RFR.fit(X_train, y_train)    # 预测    y_pred = RFR.predict(X_test)    # 评估    rmse = mean_absolute_error(y_test, y_pred)    # 累计结果    rmse_scores.append(rmse)print("rmse scores : ", rmse_scores)print(f'average rmse scores : {np.mean(rmse_scores)}')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南师大蒜阿熏呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值