Skip to content

Instantly share code, notes, and snippets.

@shaan-shah
Last active August 18, 2020 14:04
Show Gist options
  • Save shaan-shah/2904e5ddd8b07583949fdb5aae723790 to your computer and use it in GitHub Desktop.
Save shaan-shah/2904e5ddd8b07583949fdb5aae723790 to your computer and use it in GitHub Desktop.
This gist was made to demonstrate code on medium.
''' from here we are doing the feature engineering'''
print(min_leaf_a)
reset_rf_samples()
z=RandomForestRegressor(n_jobs=-1,min_samples_leaf= min_leaf_a,max_features= max_feature_a,oob_score=False,n_estimators=40)
z.fit(X_train,y_train)
fi=rf_feat_importance(z,df)
score=0
final_feature_importance_value=0
feature_importance_value_list=[0,0.001,0.002,0.0025,0.003,0.0035]
for feature_importance_value in feature_importance_value_list:
to_keep = fi[fi.imp>=feature_importance_value].cols
df_keep = df[to_keep].copy()
X_train, X_valid = split_vals(df_keep, n_trn)
feature_tuner=RandomForestRegressor(n_jobs=-1,min_samples_leaf= min_leaf_a,max_features= max_feature_a,oob_score=False,n_estimators=40)
feature_tuner.fit(X_train,y_train)
if fx(feature_tuner,X_valid=X_valid,y_valid=y_valid)>=score:
score=fx(feature_tuner,X_valid=X_valid,y_valid=y_valid)
final_feature_importance_value=feature_importance_value
'''Here we will be doing the final model training that is for now, with time i will add more tuning'''
to_keep = fi[fi.imp>=final_feature_importance_value].cols
df_keep = df[to_keep].copy()
X_train, X_valid = split_vals(df_keep, n_trn)
z=RandomForestRegressor(n_jobs=-1,min_samples_leaf= min_leaf_a,max_features= max_feature_a,oob_score=False,n_estimators=40)
z.fit(X_train,y_train)
return[min_leaf_a,max_feature_a,to_keep]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment