基于随机森林的特征选择python
时间: 2023-10-16 13:06:35 浏览: 29
可以使用sklearn库中的RandomForestClassifier来实现基于随机森林的特征选择。
首先,导入需要的库和数据集:
```python
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=0, random_state=42)
```
其中,make_classification函数用于生成分类数据集,n_samples表示样本数量,n_features表示特征数量,n_informative表示与标签相关的特征数量,n_redundant表示与标签无关的特征数量。
然后,使用RandomForestClassifier来训练模型并获取特征重要性:
```python
rfc = RandomForestClassifier(n_estimators=100, random_state=42)
rfc.fit(X, y)
feature_importances = rfc.feature_importances_
```
其中,n_estimators表示随机森林中树的数量,random_state用于控制随机性。
最后,根据特征重要性进行特征选择:
```python
selected_features = X[:, feature_importances.argsort()[::-1][:5]]
```
其中,argsort函数用于获取特征重要性从大到小的索引,[::-1]用于倒序排列,[:5]表示选择前5个特征。
完整代码如下:
```python
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_redundant=0, random_state=42)
rfc = RandomForestClassifier(n_estimators=100, random_state=42)
rfc.fit(X, y)
feature_importances = rfc.feature_importances_
selected_features = X[:, feature_importances.argsort()[::-1][:5]]
```