<jupyter_start><jupyter_text>Machine learning<jupyter_code>from sklearn.metrics import roc_auc_score
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

target_train, target_test = train_test_split(target,test_size = 0.5)

# param_grid = {  'classifier' : [LGBMClassifier()],
#                 'classifier__num_leaves': [5,10],
#                 'classifier__min_data_in_leaf': [1,3],
#                 'classifier__feature_fraction': [0.4],
#                 'classifier__bagging_fraction': [0.7],
#                 'classifier__bagging_freq': [8],
#                 'classifier__learning_rate': [0.05],
#                 'classifier__num_boost_round' : [400],
#                 'classifier__n_estimators' : [10],  
#              }

param_grid =  {"classifier" : [LogisticRegression()],
                   "classifier__C" : np.logspace(-3,3,5)}

param_grid = {"classifier" : [LogisticRegression()],
    'classifier__max_iter': np.logspace(-3,4,7),                      
    'classifier__solver': ['saga'],   
    'classifier__class_weight': ['balanced']                                    
}


target_clf, weighted_clf, scores = emm.metrics.classifier_metric(target_train.dropna(),
                                                         rwc,
                                                         param_grid,
                                                         return_models=True,
                                                         verbose=True, 
                                                         scoring=roc_auc_score,
                                                         metrics=[accuracy_score, roc_auc_score, confusion_matrix], test_size=0.8, cv=5)
import sklearn as sk
test = target_test.dropna()
X_test = test.drop(columns="Outcome")
X_test['weights'] = np.ones(len(X_test))/len(X_test)
cols = X_test.columns.tolist()
cols = cols[-1:] + cols[:-1]
X_test = X_test[cols]
X_test = X_test.values
y_test = test["Outcome"].values
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt

pred = target_clf.predict_proba(X_test)[:,1]
label = y_test

fpr, tpr, thresh_t = metrics.roc_curve(label, pred)
auc = metrics.roc_auc_score(label, pred)
plt.plot(fpr,tpr,label="RR, AUC={:.3f}".format(auc),linewidth=3)

pred = weighted_clf.predict_proba(X_test)[:,1]
print(metrics.confusion_matrix(np.where(pred>0.501,1,0),label,labels=[1,0]))
fpr, tpr, thresh = metrics.roc_curve(label, pred)
auc = metrics.roc_auc_score(label, pred)
plt.plot(fpr,tpr,label="SR, AUC={:.3f}".format(auc),linewidth=3)
plt.plot(np.linspace(0,1),np.linspace(0,1),'r--')
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.legend(loc=0)
plt.savefig('COVID_ROC')
plt.show()

# calculate the g-mean for each threshold
gmeans = np.sqrt(tpr * (1-fpr))
ix = np.argmax(gmeans)
print('Best Threshold=%f, G-Mean=%.3f' % (thresh[ix], gmeans[ix]))
print(metrics.confusion_matrix(np.where(pred>0.4949,1,0),label, labels=[1,0]))
38
scores
target['Outcome'].sum() * 0.2 
print((marg_tab*100).round(3).to_latex())
(100 - corpus.mean() * 100).round(1)
target['Outcome'].sum()
print(((marg_tab - rw_marg_tab)*100).round(4).to_latex())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1413108839446783, 'fraction_numerical': 0.024052916416115455, 'mean_word_length': 2.759322033898305, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '342736', 'license': 'MIT', 'n_tokens_mistral': 1149, 'n_tokens_neox': 1098, 'n_words': 171, 'path': 'notebooks/COVID_Example.ipynb', 'repo_name': 'LucasKIJ/EMM', 'size': '3326'}