RF特征选择

随机森林特征选择

特征选择的意义:

这可以通过使用重要性分数来选择要删除(最低分数)或要保留的功能(最高分数)来实现。这是一种特征选择,可以简化正在建模的问题,加快建模过程(删除要素称为尺寸缩减),在某些情况下,可以提高模型的性能,并且提高模型的范化能力。

随机林特征重要性:

随机林是最受欢迎的机器学习方法之一,因为它们的精度、鲁棒性和易用性都比较好。它们还提供两种简单的特征选择方法:均值减少杂质和均值降低精度。

此处我们对回归树分类树特征选择分别进行了代码展示:下面进行了code展示,希望能对爱好者有所启发!

分类特征选择
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# define dataset

X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, 
                           n_redundant=5, random_state=1)
# define the model
model = RandomForestClassifier()
# fit the model
model.fit(X, y)
# get importance
fet_ind  = np.argsort(model.feature_importances_)[::-1]
fet_imp = model.feature_importances_[fet_ind]

fig, ax = plt.subplots(1,1,figsize=(8, 3))
pd.Series(fet_imp).plot("bar", ax=ax)
ax.set_title('Features importance')
plt.show()

在这里插入图片描述

回归特征选择
mport numpy as np
import sklearn as sk
import sklearn.datasets as skd
import sklearn.ensemble as ske
import matplotlib.pyplot as plt
import pandas as pd

data = skd.load_boston()

reg = ske.RandomForestRegressor()
X = data['data']
y = data['target']

reg.fit(X, y)

fet_ind = np.argsort(reg.feature_importances_)[::-1]
fet_imp = reg.feature_importances_[fet_ind]

fig, ax = plt.subplots(1, 1, figsize=(8, 3))
labels = data['feature_names'][fet_ind]
pd.Series(fet_imp, index=labels).plot('bar', ax=ax)
ax.set_title('Features importance')
plt.show()

在这里插入图片描述
R 感兴趣的可以查看此处

如果觉得有用,请点赞或者关注。
有问题请在下方评论!

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值