随机森林特征选择
特征选择的意义:
这可以通过使用重要性分数来选择要删除(最低分数)或要保留的功能(最高分数)来实现。这是一种特征选择,可以简化正在建模的问题,加快建模过程(删除要素称为尺寸缩减),在某些情况下,可以提高模型的性能,并且提高模型的范化能力。
随机林特征重要性:
随机林是最受欢迎的机器学习方法之一,因为它们的精度、鲁棒性和易用性都比较好。它们还提供两种简单的特征选择方法:均值减少杂质和均值降低精度。
此处我们对回归树
和分类树
特征选择分别进行了代码展示:下面进行了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
感兴趣的可以查看此处
如果觉得有用,请点赞或者关注。
有问题请在下方评论!