Skip to content

Instantly share code, notes, and snippets.

@evanfeinberg
Created February 24, 2016 22:58
Show Gist options
  • Save evanfeinberg/df45077cc80a178efa1d to your computer and use it in GitHub Desktop.
Save evanfeinberg/df45077cc80a178efa1d to your computer and use it in GitHub Desktop.
Random Forests for interpreting tICs
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
import numpy as np
def compute_random_forests(features, projected_tica_coords, rf_dir, n_trees=500,
n_tica_components=25, start_tIC=0):
features = np.concatenate(features)
tics = np.concatenate(projected_tica_coords)
for j in range(start_tIC, np.shape(tics)[1]):
#os.system("rm %s/tIC.%d_rfr.pkl" %(rf_dir, j))
rfr = RandomForestRegressor(n_estimators=n_trees, max_features="sqrt",
n_jobs=-1, verbose=1)
print(("Fitting tree for tIC %d" %(j+1)))
rfr.fit(features, tics[:,j])
print("Done fitting RF model. Saving now...")
with open(os.path.join(rf_dir, "tIC.%d_rfr.pkl" %j), "wb") as f:
pickle.dump(rfr.feature_importances_, f)
print("Saved random forest feature importances")
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment