sklearn 随机森林进行数据预测

本文介绍了如何利用RandomForestRegressor进行回归预测,并通过GridSearchCV优化参数。关键步骤包括数据集加载、特征选择(选取后四列重要性高者)、训练与测试,以及使用feature_importances_评估特征重要性。
摘要由CSDN通过智能技术生成

数据集:​​​​​​ https://download.csdn.net/download/SpecialRiot/85339262icon-default.png?t=M4ADhttps://download.csdn.net/download/SpecialRiot/85339262

根据上图的目录结构放置数据集后执行下面的代码。

from sklearn.ensemble import RandomForestRegressor
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score

df = pd.read_csv("./ETTm1.csv")
X = df.iloc[:, 0:7]
Y = df.iloc[:, 7]
X_features = pd.DataFrame(data=X ,columns=["MUFL","MULL","LUFL","LULL"])
X_train, X_test, y_train, y_test = train_test_split(
    X_features,Y, test_size=0.2, random_state=1)
forest = RandomForestRegressor(criterion='mse', max_depth=15, n_estimators=1000,n_jobs=-1)
forest.fit(X_train, y_train)
y_test_pred = forest.predict(X_test)
print('R^2 test: %.3f' % (r2_score(y_test, y_test_pred)))

RandomForestRegressor方法中的参数可以通过GridSearchCV去调优。原数据集有八个特征数据通过feature_importances_方法可以得出每一列的重要性,每一特征对应重要性如下["HUFL","HULL","MUFL","MULL","LUFL","LULL"]

[0.08494287 0.0578021  0.18068544 0.20789311 0.34723973 0.12143676]

选择后四列重要性比较高的作为特征。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值