机器学习-RF预测

本文使用机器学习RF对数据进行预测。仅供参考

1、数据

1.1 训练数据集:

medol.xlsx文件示例

otv
3015-1.915362209
3018-1.963409776
3021-1.762028408
3024-1.789477583

1.2 预测数据集

test.xlsx文件示例

ot
3516
3519

2、模型训练

train.py

import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
import joblib
import numpy as np
import matplotlib.pyplot as plt

# 载入数据集
df = pd.read_excel("model.xlsx")

# 定义特征列
feature_columns = ['o', 't']

# 初始化标准缩放器
scaler = StandardScaler()

# 对数据集进行标准化
X_scaled = scaler.fit_transform(df[feature_columns])

# 目标列
target_column = 'v'

# 将数据分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_scaled, df[target_column], test_size=0.1)

# 初始化随机森林回归模型
rf = RandomForestRegressor(random_state=42)

# 定义用于网格搜索的超参数网格
param_grid = {
    'n_estimators': [50, 100, 150],
    'max_features': [ 10, 20, 30, 40, 50, 60, 70]
}

# 使用GridSearchCV进行超参数调优
grid_search = GridSearchCV(rf, param_grid, cv=5, scoring='neg_mean_squared_error', verbose=1)
grid_search.fit(X_train, y_train)

# 获取网格搜索中的最佳模型
best_rf_model = grid_search.best_estimator_

# 打印最佳超参数
print("最佳超参数:", grid_search.best_params_)

# 使用最佳模型进行训练集和测试集的预测
train_predictions = best_rf_model.predict(X_train)
test_predictions = best_rf_model.predict(X_test)

# 打印训练集指标
print("\n[训练集指标]")
print("平均绝对误差: {}".format(mean_absolute_error(y_train, train_predictions)))
print("均方误差: {}".format(mean_squared_error(y_train, train_predictions)))
print("均方根误差: {}".format(np.sqrt(mean_squared_error(y_train, train_predictions))))
print("R2: {}".format(r2_score(y_train, train_predictions)))

# 打印测试集指标
print("\n[测试集指标]")
print("平均绝对误差: {}".format(mean_absolute_error(y_test, test_predictions)))
print("均方误差: {}".format(mean_squared_error(y_test, test_predictions)))
print("均方根误差: {}".format(np.sqrt(mean_squared_error(y_test, test_predictions))))
print("R2: {}".format(r2_score(y_test, test_predictions)))

# 绘制训练集误差随树数量的变化图
train_errors = -1 * grid_search.cv_results_['mean_test_score'].reshape(len(param_grid['max_features']), -1)
plt.figure(figsize=(8, 6))
for i, value in enumerate(param_grid['max_features']):
    plt.plot(param_grid['n_estimators'], train_errors[i], label=f'max_features={value}')

plt.xlabel('Number of Trees')
plt.ylabel('Mean Squared Error')
plt.title('Training Set Error Over Number of Trees (GridSearchCV Results)')
plt.legend()
plt.show()


# 绘制测试集的实际值和预测值的对比图
plt.scatter(y_test, test_predictions, alpha=0.5)
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], '--', color='red', label='Identity Line')
plt.xlabel('Actual Values')
plt.ylabel('Predicted Values')
plt.title('Actual vs Predicted Values on Test Set')
plt.legend()
plt.show()

# 保存训练好的模型
model_filename = 'rf_model'
joblib.dump(best_rf_model, model_filename)

# 保存标准缩放器
joblib.dump(scaler, 'scaler_model')

3、预测模型

test.py

import pandas as pd
import joblib
from sklearn.preprocessing import StandardScaler

# 加载训练好的模型并在新数据上进行预测 (test.xlsx)
test_df = pd.read_excel("test.xlsx")

# 初始化 StandardScaler
scaler = joblib.load('scaler_model')

predicted_columns = []


model_filename = f'rf_model'

# 加载模型
loaded_model = joblib.load(model_filename)

# 在新数据上进行预测
predictions = loaded_model.predict(scaler.transform(test_df[['o', 't']]))

# 将预测结果存储在列表中
predicted_columns.append(predictions)

# 使用 pd.concat(axis=1) 将所有列一次性连接到 DataFrame 中
predicted_df = pd.concat([test_df] + [pd.Series(predictions, name=f'v') for i, predictions in enumerate(predicted_columns, start=1)], axis=1)

# 保存更新后的 test_df
predicted_df.to_excel("predicted.xlsx", index=False)

4、结果

最佳超参数: {'max_features': 10, 'n_estimators': 100}

[训练集指标]
平均绝对误差: 0.06338937108380824
均方误差: 0.006458315810360046
均方根误差: 0.08036364731867292
R2: 0.9972901801394782

[测试集指标]
平均绝对误差: 0.17862688242033742
均方误差: 0.04211310733703667
均方根误差: 0.20521478342711247
R2: 0.97073350775831

若有问题,欢迎讨论

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小张er

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

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

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

打赏作者

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

抵扣说明:

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

余额充值