Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jay-trivedi/bbd5a27e25789a6e82f0e7f13c0862af to your computer and use it in GitHub Desktop.
Save jay-trivedi/bbd5a27e25789a6e82f0e7f13c0862af to your computer and use it in GitHub Desktop.
seed = 1
depth_range = range(1, 30,1)
acc_vs_depth_result = {"depth": [],\
"train_acc": [],
"valid_acc": [],
"top_feature": [],
"second_feature": [],
"third_feature": []}
for depth in depth_range:
model = H2ORandomForestEstimator(model_id="model", \
sample_rate=1, \
ntrees=1, \
max_depth=depth, \
seed=seed)
model.train(x=x, y=y, training_frame=train)
predict_valid = model.predict(valid[x])
predict_train = model.predict(train[x])
acc_vs_depth_result["depth"].append(depth)
acc_vs_depth_result["valid_acc"].append((predict_valid["predict"] == valid["Survived"]).mean()[0])
acc_vs_depth_result["train_acc"].append((predict_train["predict"] == train["Survived"]).mean()[0])
acc_vs_depth_result["top_feature"].append(model.varimp()[0][0])
acc_vs_depth_result["second_feature"].append(model.varimp()[1][0])
acc_vs_depth_result["third_feature"].append(model.varimp()[2][0])
acc_vs_depth_result_df = pd.DataFrame(acc_vs_depth_result)
cols = ["depth", "train_acc", "valid_acc", "top_feature", "second_feature", "third_feature"]
acc_vs_depth_result_df = acc_vs_depth_result_df[cols]
fig = plt.figure(figsize=(10, 7))
plt.plot(acc_vs_depth_result_df.depth, acc_vs_depth_result_df.train_acc, label="train accuracy")
plt.plot(acc_vs_depth_result_df.depth, acc_vs_depth_result_df.valid_acc, label="validation accuracy")
plt.legend(loc='upper left', frameon=False)
plt.xlabel('Tree Depth')
plt.ylabel('Accuracy')
plt.savefig("figures/Titanic_DT")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment