TVP

# 使用 Scikit-learn 理解随机森林

fromtreeinterpreterimporttreeinterpreterasti

fromsklearn.treeimportDecisionTreeRegressor

fromsklearn.ensembleimportRandomForestRegressor

importnumpyasnp

rf = RandomForestRegressor()

rf.fit(boston.data[:300], boston.target[:300])

instances = boston.data[[300,309]]

print"Instance 0 prediction:", rf.predict(instances[])

print"Instance 1 prediction:", rf.predict(instances[1])

prediction, bias, contributions = ti.predict(rf, instances)

fori inrange(len(instances)):

print"Instance", i

print"Bias (trainset mean)", biases[i]

print"Feature contributions:"

forc, feature in sorted(zip(contributions[i],

boston.feature_names),

key=lambdax: -abs(x[])):

printfeature,round(c,2)

print"-"*20

printprediction

printbiases + np.sum(contributions, axis=1)

[30.7622.41]

[30.7622.41]

ds1 = boston.data[300:400]

ds2 = boston.data[400:]

printnp.mean(rf.predict(ds1))

printnp.mean(rf.predict(ds2))

prediction1, bias1, contributions1 = ti.predict(rf, ds1)

prediction2, bias2, contributions2 = ti.predict(rf, ds2)

totalc1= np.mean(contributions1, axis=)

totalc2= np.mean(contributions2, axis=)

printnp.sum(totalc1-totalc2)

printnp.mean(prediction1)-np.mean(prediction2)

3.71384150943

3.71384150943

forc, feature in sorted(zip(totalc1 - totalc2,

boston.feature_names),reverse=True):

printfeature,round(c,2)

instance = iris.data[idx][100:101]

print rf.predict_proba(instance)

prediction, bias, contributions = ti.predict(rf, instance)

print"Prediction", prediction

print"Bias (trainset prior)", bias

print"Feature contributions:"

forc, featureinzip(contributions[0],

iris.feature_names):

printfeature, c

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180803A091WL00?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20

2024-07-20