[英]How to handle lots of missing values in GradientBoostingClassifier in sklearn
[英]Generate code for sklearn's GradientBoostingClassifier
我想從訓練有素的梯度提升分類器(來自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。
對於一個估算器,它給出了與訓練模型上的predict
方法相同的預測。 然而,當我添加更多估算器時,預測開始偏離。 我應該加入步長( 這里描述)? 另外,我的F0是否正確? 我應該采取均值嗎? 我應該將日志概率轉換為其他東西嗎? 任何幫助是極大的贊賞!
在Gradient Boosting分類器的引擎下是回歸樹的總和。
您可以通過讀取estimators_
屬性從訓練的分類estimators_
獲取弱學習者決策樹。 從文檔中 ,它實際上是DecisionTreeRegressor的ndarray。
最后,要完全重現預測功能,您還需要訪問權重,如本答案中所述 。
或者,您可以導出決策樹的GraphViz表示(而不是其偽代碼)。 從scikit-learn.org中找到以下可視化示例:
作為最后的邊緣注釋/建議,您可能還想嘗試xgboost:除了其他功能外,它還具有內置的“轉儲模型”功能(用於顯示受過訓練的模型下的所有決策樹並將其保存到文本文件中)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.