<jupyter_start><jupyter_text>Template LR<jupyter_code>def lasso(X,y):
    from sklearn.linear_model import Lasso
    from sklearn.model_selection import GridSearchCV
    model = Lasso()
    param_grid = [
        {'alpha' : np.logspace(-4, 4, 20)}
        ]
    clf = GridSearchCV(model, param_grid, cv = 5, verbose = True, n_jobs = 1)
    best_clf = clf.fit(X, y)
    return best_clf
def scores(X_train,y_train, best_clf):
    from sklearn.metrics import accuracy_score
    from sklearn.metrics import f1_score
    from sklearn.metrics import fbeta_score
    from sklearn.metrics import roc_auc_score 
    from sklearn.metrics import log_loss
    import numpy as np
    pred = np.round(best_clf.predict(X_train))
    actual = y_train
    prob = best_clf.predict(X_train)
    print(accuracy_score(actual,pred))
    print(f1_score(actual,pred, average = 'macro'))
    print(fbeta_score(actual,pred, average = 'macro', beta = 2))
    print(roc_auc_score(actual, prob))
    print(log_loss(actual,prob))
def cross_val(X,y):
    from sklearn.model_selection import KFold
    from sklearn.model_selection import cross_validate
    from sklearn.metrics import log_loss
    from sklearn.metrics import roc_auc_score
    from sklearn.metrics import fbeta_score
    import sklearn
    import numpy as np
    cv = KFold(n_splits=5, random_state=1, shuffle=True)
    log_loss = [] 
    auc = [] 
    accuracy = []
    f1 = []
    f2 = [] 
    for train_index, test_index in cv.split(X):
        X_train, X_test, y_train, y_test = X.iloc[train_index], X.iloc[test_index], y.iloc[train_index], y.iloc[test_index]
        model = lasso(X_train, y_train)
        prob = model.predict(X_test) # prob is a vector of probabilities 
        pred = np.round(model.predict(X_test)) # pred is the rounded predictions 
        log_loss.append(sklearn.metrics.log_loss(y_test, prob))
        auc.append(sklearn.metrics.roc_auc_score(y_test, prob))
        accuracy.append(sklearn.metrics.accuracy_score(y_test, pred))
        f1.append(sklearn.metrics.f1_score(y_test, pred, average = 'macro'))
        f2.append(fbeta_score(y_test,pred, average = 'macro', beta = 2))
    print(np.mean(accuracy))
    print(np.mean(f1))
    print(np.mean(f2))
    print(np.mean(auc))
    print(np.mean(log_loss))<jupyter_output><empty_output><jupyter_text>General Population<jupyter_code>from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state = 42)
co_train_gpop_sm,out_train_death_gpop_sm = sm.fit_resample(co_train_gpop,out_train_death_gpop)

best_clf = lasso(co_train_gpop_sm, out_train_death_gpop_sm)

cross_val(co_train_gpop_sm, out_train_death_gpop_sm)

print()

scores(co_train_gpop, out_train_death_gpop, best_clf)

print()

scores(co_validation_gpop, out_validation_death_gpop, best_clf)

#comb = [] 
#for i in range(len(predictor_variable_claims)):
    #comb.append(predictor_variable_claims[i] + str(best_clf.best_estimator_.coef_[:,i:i+1]))
#comb<jupyter_output>Fitting 5 folds for each of 20 candidates, totalling 100 fits<jupyter_text>High Continuity<jupyter_code>from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state = 42)
co_train_high_sm,out_train_death_high_sm = sm.fit_resample(co_train_high, out_train_death_high)

best_clf = lasso(co_train_high_sm, out_train_death_high_sm)

cross_val(co_train_high_sm, out_train_death_high_sm)

print()

scores(co_train_high, out_train_death_high, best_clf)

print()

scores(co_validation_high, out_validation_death_high, best_clf)

#comb = [] 
#for i in range(len(predictor_variable_claims)):
    #comb.append(predictor_variable_claims[i] + str(best_clf.best_estimator_.coef_[:,i:i+1]))
#comb<jupyter_output>Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits<jupyter_text>Low Continuity<jupyter_code>from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state = 42)
co_train_low_sm,out_train_death_low_sm = sm.fit_resample(co_train_low,out_train_death_low)

best_clf = lasso(co_train_low_sm, out_train_death_low_sm)

cross_val(co_train_low_sm, out_train_death_low_sm)

print()

scores(co_train_low, out_train_death_low, best_clf)

print()

scores(co_validation_low, out_validation_death_low, best_clf)

#comb = [] 
#for i in range(len(predictor_variable_claims)):
    #comb.append(predictor_variable_claims[i] + str(best_clf.best_estimator_.coef_[:,i:i+1]))
#comb<jupyter_output>Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits
Fitting 5 folds for each of 20 candidates, totalling 100 fits
0.7754689223979404
0.7754165504612924
0.7754359801843529
0.8508869492109585
0.6347038745337287

0.7701365077311251
0.6486935454987156
0.6960906194289609
0.8412130257340931
0.729983338237978

0.7424126741744608
0.6345209735011402
0.6775516646090722
0.8142679642181803
0.8065143731277347
{'fraction_non_alphanumeric': 0.09066305818673884, 'fraction_numerical': 0.06727237579740963, 'mean_word_length': 5.37192118226601, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '4681', 'license': 'MIT', 'n_tokens_mistral': 2194, 'n_tokens_neox': 1918, 'n_words': 389, 'path': 'EHR_Claims/Lasso/EHR_C_Death_SMOTE.ipynb', 'repo_name': 'shreyaskar123/EHR-Discontinuity', 'size': '5173'}