RandomForestClassifier(随机森林检测每个特征的重要性及每个样例属于哪个类的概率)...
生活随笔
收集整理的這篇文章主要介紹了
RandomForestClassifier(随机森林检测每个特征的重要性及每个样例属于哪个类的概率)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#In the next recipe, we'll look at how to tune the random forest classifier.
#Let's start by importing datasets:from sklearn import datasets
X, y = datasets.make_classification(1000)# X(1000,20)
#y(1000) 取值范圍【0,1】from sklearn.ensemble import RandomForestClassifier
rf = RandomForestClassifier()
rf.n_jobs=-1rf.fit(X, y)
print ("Accuracy:\t", (y == rf.predict(X)).mean())
print ("Total Correct:\t", (y == rf.predict(X)).sum())#每個例子屬于哪個類的概率
probs = rf.predict_proba(X)
import pandas as pd
probs_df = pd.DataFrame(probs, columns=['0', '1'])
probs_df['was_correct'] = rf.predict(X) == y
import matplotlib.pyplot as plt
f, ax = plt.subplots(figsize=(7, 5))
probs_df.groupby('0').was_correct.mean().plot(kind='bar', ax=ax)
ax.set_title("Accuracy at 0 class probability")
ax.set_ylabel("% Correct")
ax.set_xlabel("% trees for 0")
f.show()#檢測重要特征
rf = RandomForestClassifier()
rf.fit(X, y)
f, ax = plt.subplots(figsize=(7, 5))
ax.bar(range(len(rf.feature_importances_)),rf.feature_importances_)
ax.set_title("Feature Importances")
f.show()
轉載于:https://www.cnblogs.com/qqhfeng/p/5341840.html
總結
以上是生活随笔為你收集整理的RandomForestClassifier(随机森林检测每个特征的重要性及每个样例属于哪个类的概率)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZOJ2930 The Worst Sc
- 下一篇: NGUI之输入文本框的使用