Skip to content

Instantly share code, notes, and snippets.

View kbfreder's full-sized avatar

Kendra kbfreder

View GitHub Profile
# import function, or patch it:
# Note: may need to install mlxtend
try:
from sklearn.inspection import permutation_importance
except ImportError:
print("Problem importing permutation_importance -- patching")
from mlxtend.evaluate import feature_importance_permutation
def permutation_importance(estimator, X, y, scoring='r2', n_repeats=5):
"""
@kbfreder
kbfreder / grid_cv.py
Created February 11, 2020 22:40
Using sklearn's GridSearchCV
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import make_scorer
# import Random Forest
param_grid = {'n_estimators': [50, 100, 200],
'max_depth':[None, 10, 15, 20],
'criterion': ['gini', 'entropy'],
'min_impurity_decrease': [0, 1e7, 1e5]}
scorer = make_scorer(accuracy_score)
@kbfreder
kbfreder / tree_depths.py
Created February 11, 2020 22:15
Retrieve tree depths
# import Random Forest
base_rf = RandomForestClassifier(n_estimators=100, max_depth=None)
base_rf.fit(train_X, train_y)
depths = [est.tree_.max_depth for est in base_rf.estimators_]
@kbfreder
kbfreder / evaluating_classifiers.py
Created February 11, 2020 19:54
Comparing classifiers
# this is a custom module
import assess_clf_models as acm
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier, BaggingClassifier
from catboost import CatBoostClassifier
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder, FunctionTransformer