ランダム森林アルゴリズム(RandomForest)はMNIST手書きデジタル識別を実現する


一、準備:
サードパーティ製ライブラリsklearn
二、コード:
# -*- coding: utf-8 -*-
# @Time    : 2018/8/21 9:35
# @Author  : Barry
# @File    : mnist.py
# @Software: PyCharm Community Edition

from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import tensorflow.examples.tutorials.mnist.input_data as input_data

data_dir = 'MNIST_data/'
mnist = input_data.read_data_sets(data_dir,one_hot=False)
batch_size = 50000
batch_x,batch_y = mnist.train.next_batch(batch_size)
test_x = mnist.test.images[:10000]
test_y = mnist.test.labels[:10000]

print("start random forest")
for i in range(10,200,10):
    clf_rf = RandomForestClassifier(n_estimators=i)
    clf_rf.fit(batch_x,batch_y)

    y_pred_rf = clf_rf.predict(test_x)
    acc_rf = accuracy_score(test_y,y_pred_rf)
    print("n_estimators = %d, random forest accuracy:%f" %(i,acc_rf))

実行結果:
start random forest
n_estimators = 10, random forest accuracy:0.947300
n_estimators = 20, random forest accuracy:0.957600
n_estimators = 30, random forest accuracy:0.962900
n_estimators = 40, random forest accuracy:0.965400
n_estimators = 50, random forest accuracy:0.966500
n_estimators = 60, random forest accuracy:0.965500
n_estimators = 70, random forest accuracy:0.968000
n_estimators = 80, random forest accuracy:0.967900
n_estimators = 90, random forest accuracy:0.967300
n_estimators = 100, random forest accuracy:0.968900
n_estimators = 110, random forest accuracy:0.968600
n_estimators = 120, random forest accuracy:0.969800
n_estimators = 130, random forest accuracy:0.967100
n_estimators = 140, random forest accuracy:0.968200
n_estimators = 150, random forest accuracy:0.969200
n_estimators = 160, random forest accuracy:0.969200
n_estimators = 170, random forest accuracy:0.969300
n_estimators = 180, random forest accuracy:0.969300
n_estimators = 190, random forest accuracy:0.968500

三、アルゴリズムの浅い分析:
ランダムフォレスト(random forest)は2001年に提案されたデータの回帰と分類予測アルゴリズムを同時にサポートし、ランダムフォレストアルゴリズムを具体的に理解する前に、まず決定ツリーアルゴリズム(Decision Tree)の決定ツリーアルゴリズムを見て、絶えず分岐条件をフィルタリングし、最終的に予測分類を決定し、簡単な例を挙げて、あなたは仕事を探して、相手はあなたにofferをあげました.次は、offerを最終的に受け入れるか拒否するかを決定する一連の条件が内部ノード(矩形)の最終的な決定が外部ノード(葉-楕円)であることを決定した後、あなた自身が上記の条件に基づいてofferを受け入れるかどうかを決定する可能性がありますが、時にはまだ確定していない場合があります.周りの何人かの友达にランダムに聞いてみましょう.彼らもあなたの状況と把握した情報に基づいて一連の決定をして、イメージの比喩をして、彼らは1本の単独で存在する決定木で、最終的にあなたはこれらの結果に基づいてofferを受け入れるか拒否するかを決定して、前の状況はあなたが自分で受け入れるかofferを拒否するかを決定木のアルゴリズムと呼んで、後ろの状況、あなたは一人で考えが定まらないで、また、ランダムにあなたの周りの何人かの友达に一緒に参謀に聞いて、最終的にofferを受け入れるか拒否するかの決定方法をして、あなたの友达も1本の単独で存在する決定木で、彼らは一緒に決定して、これはランダムな森と呼ばれています.ランダムな森を使って決定するとき、分岐条件が多すぎることがあります.決定要因ではない分岐条件もあります.offerを受け入れるか拒否するかを決定するとき、会社にプログラマー奨励師がいるかどうかを考えないかもしれません.この場合,このような小さな分岐をノイズと見なし,剪断アルゴリズム処理を行い決定木を生成し,最終的にランダム森林を得る必要がある.同時にランダム森林の規模が大きいほど(決定木が多いほど)、その決定精度も高くなる.ランダム森林アルゴリズムは金融風制御分析、株式取引データ分析、電子商取引などの分野で応用されている.
参考資料:
Opencv微信公衆番号プッシュ文章