4

トレーニング済みの勾配ブースト分類器 (sklearn から) からコード (今のところ Python ですが、最終的には C) を生成したいと考えています。私が理解している限り、モデルは最初の予測子を取り、次に、順次トレーニングされた回帰ツリー (学習係数によってスケーリング) からの予測を追加します。選択されたクラスは、最高の出力値を持つクラスです。

これは私がこれまでに持っているコードです:

def recursep_gbm(left, right, threshold, features, node, depth, value, out_name, scale):
    # Functions for spacing
    tabs = lambda n: (' ' * n * 4)[:-1]
    def print_depth():
        if depth: print tabs(depth),
    def print_depth_b():
        if depth: 
            print tabs(depth), 
            if (depth-1): print tabs(depth-1),

    if (threshold[node] != -2):
        print_depth()
        print "if " + features[node] + " <= " + str(threshold[node]) + ":"
        if left[node] != -1:
            recursep_gbm(left, right, threshold, features, left[node], depth+1, value, out_name, scale)
        print_depth()
        print "else:"
        if right[node] != -1:
            recursep_gbm(left, right, threshold, features, right[node], depth+1, value, out_name, scale)
    else:
        # This is an end node, add results
        print_depth()
        print out_name + " += " + str(scale) + " * " + str(value[node][0, 0])

def print_GBM_python(gbm_model, feature_names, X_data, l_rate):
    print "PYTHON CODE"

    # Get trees
    trees = gbm_model.estimators_

    # F0
    f0_probs = np.mean(clf.predict_log_proba(X_data), axis=0)
    probs    = ", ".join([str(prob) for prob in f0_probs])
    print "# Initial probabilities (F0)"
    print "scores = np.array([%s])" % probs
    print 

    print "# Update scores for each estimator"
    for j, tree_group in enumerate(trees):
        for k, tree in enumerate(tree_group):
            left      = tree.tree_.children_left
            right     = tree.tree_.children_right
            threshold = tree.tree_.threshold
            features  = [feature_names[i] for i in tree.tree_.feature]
            value = tree.tree_.value

            recursep_gbm(left, right, threshold, features, 0, 0, value, "scores[%i]" % k, l_rate)
        print

    print "# Get class with max score"
    print "return np.argmax(scores)"

この質問からツリー生成コードを変更しました。

これが生成するものの例です (3 つのクラス、2 つの推定器、1 つの最大深度、0.1 の学習率)。

# Initial probabilities (F0)
scores = np.array([-0.964890, -1.238279, -1.170222])

# Update scores for each estimator
if X1 <= 57.5:
    scores[0] += 0.1 * 1.60943587225
else:
    scores[0] += 0.1 * -0.908433703247
if X2 <= 0.000394500006223:
    scores[1] += 0.1 * -0.900203054177
else:
    scores[1] += 0.1 * 0.221484425933
if X2 <= 0.0340005010366:
    scores[2] += 0.1 * -0.848148803219
else:
    scores[2] += 0.1 * 1.98100820717

if X1 <= 57.5:
    scores[0] += 0.1 * 1.38506104792
else:
    scores[0] += 0.1 * -0.855930587354
if X1 <= 43.5:
    scores[1] += 0.1 * -0.810729087535
else:
    scores[1] += 0.1 * 0.237980820334
if X2 <= 0.027434501797:
    scores[2] += 0.1 * -0.815242297324
else:
    scores[2] += 0.1 * 1.69970863021

# Get class with max score
return np.argmax(scores)

これに基づいて、対数確率を F0 として使用しまし

predict1 つの推定量については、トレーニング済みモデルのメソッドと同じ予測が得られます。ただし、さらに推定量を追加すると、予測が逸脱し始めます。ステップの長さを組み込むことになっていますか (ここで説明されています)? また、私のF0は正しいですか?私は平均を取るべきですか?そして、対数確率を別のものに変換する必要がありますか? どんな助けでも大歓迎です!

4

1 に答える 1

2

勾配ブースティング分類器の内部には、回帰木の合計があります。

estimators_属性を読み取ることで、トレーニング済みの分類器から弱学習器の決定木を取得できます。ドキュメントから、実際には DecisionTreeRegressor の ndarray です。

最後に、予測関数を完全に再現するには、この回答で説明されているように、重みにもアクセスする必要があります。

または、 (疑似コードの代わりに) 決定木のGraphViz表現をエクスポートすることもできます。scikit-learn.org から以下の視覚的な例を見つけてください。 ここに画像の説明を入力

最後の補足的なメモ/提案として、xgboost も試してみることをお勧めします。他の機能に加えて、組み込みの「モデルのダンプ」機能があります (トレーニング済みモデルの下にあるすべての決定木を表示し、それらをテキスト ファイルに保存します)。 )。

于 2016-05-28T21:23:20.047 に答える