代码:
- import numpy as np
- from sklearn.ensemble import RandomForestClassifier
-
- data = np.random.random((100,10))
- lanel = np.zeros((100))
- lanel[25:50] = 1
- lanel[50:75] = 2
- lanel[75:] = 3
-
- model = RandomForestClassifier(max_depth=None,n_estimators=1000,
- criterion='gini',class_weight="balanced")
- model.fit(data,lanel)
- train_score = model.score(data,lanel)
- print('train_score: ', train_score)
-
- # 特征重要性
- feature_impotant = model.feature_importances_
- print(feature_impotant)
-
- train_score: 1.0
- 0.9999999999999998
'运行
更直观的演示:
- from sklearn.datasets import load_iris
- from sklearn.ensemble import RandomForestClassifier
-
- iris = load_iris()
- X, y = iris.data, iris.target
-
- rfc = RandomForestClassifier(n_estimators=100, random_state=42)
- rfc.fit(X, y)
-
- importances = rfc.feature_importances_
-
- indices = sorted(range(len(importances)), key=lambda i: importances[i], reverse=True)
-
- print("Feature ranking:")
- for f in range(X.shape[1]):
- print(f"{f+1}. {iris.feature_names[indices[f]]}: {importances[indices[f]]}")
- Feature ranking:
- 1. petal length (cm): 0.4361295069034437
- 2. petal width (cm): 0.43606478004168353
- 3. sepal length (cm): 0.10612761987750428
- 4. sepal width (cm): 0.02167809317736852