Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jay-trivedi/8c349328e3d492dfef5525a652673c2e to your computer and use it in GitHub Desktop.
Save jay-trivedi/8c349328e3d492dfef5525a652673c2e to your computer and use it in GitHub Desktop.
acc_vs_depth_result = {"depth": [],\
"train_acc": [],
"valid_acc": [],
"top_feature": [],
"second_feature": [],
"third_feature": []}
seed = 1
depth_range = range(1, 30,1)
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, validation_frame=valid)
predict_valid = model.predict(valid[x])
predict_train = model.predict(train[x])
t = predict_train["predict"].cbind(train["SalePrice"]).as_data_frame()
v = predict_valid["predict"].cbind(valid["SalePrice"]).as_data_frame()
acc_vs_depth_result["depth"].append(depth)
acc_vs_depth_result["valid_acc"].append(mean_squared_error(y_true = v.SalePrice, y_pred = v.predict))
acc_vs_depth_result["train_acc"].append(mean_squared_error(y_true = t.SalePrice, y_pred = t.predict))
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 MSE")
plt.plot(acc_vs_depth_result_df.depth, acc_vs_depth_result_df.valid_acc, label="validation MSE")
plt.legend(loc='upper left', frameon=False)
plt.xlabel('Tree Depth')
plt.ylabel('MSE')
plt.savefig("figures/House_pricing_DT.png")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment