Skip to content

Instantly share code, notes, and snippets.

@erykml
Created February 11, 2019 22:24
Show Gist options
  • Select an option

  • Save erykml/ffc69f6a6166f2ce25e56fdd11bf091a to your computer and use it in GitHub Desktop.

Select an option

Save erykml/ffc69f6a6166f2ce25e56fdd11bf091a to your computer and use it in GitHub Desktop.
from treeinterpreter import treeinterpreter as ti, utils
selected_rows = [31, 85]
selected_df = X_train.iloc[selected_rows,:].values
prediction, bias, contributions = ti.predict(rf, selected_df)
for i in range(len(selected_rows)):
print("Row", selected_rows[i])
print("Prediction:", prediction[i][0], 'Actual Value:', y_train[selected_rows[i]])
print("Bias (trainset mean)", bias[i])
print("Feature contributions:")
for c, feature in sorted(zip(contributions[i],
X_train.columns),
key=lambda x: -abs(x[0])):
print(feature, round(c, 2))
print("-"*20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment