<jupyter_start><jupyter_text>Note* build model based on different months_bin<jupyter_code>df_demo = pd.read_csv('T_demo.csv')
df_stage = pd.read_csv('T_stage.csv')
df_agg = pd.read_csv('df_aggregated.csv')
# Change the unknown in df_demo race to the mode
df_demo['race'].value_counts()
df_demo.loc[df_demo['race'] == 'Unknown','race'] = 'White'
df_demo['race'].value_counts()
df_stage.info()
# Change state to 0, 1
df_stage['Stage_Progress'].value_counts()
df_stage['Stage_Progress'] = np.where(df_stage['Stage_Progress'] == True, 1, 0)
df_stage['Stage_Progress'].value_counts()
df_agg.head()<jupyter_output><empty_output><jupyter_text>6 months data<jupyter_code>df_bin1 = df_agg[df_agg['months_bin']==1]
# Check if all 300 subjects are included
len(df_bin1['id'].unique())
# Check missing values
df_bin1.info()
# ALl missing values are at the med, so can just fill with 0
df_bin1 = df_bin1.fillna(0)
# Attach demographic and target
df_bin1 = df_bin1.merge(df_demo, on='id', how='outer')
df_bin1 = df_bin1.merge(df_stage, on='id', how='outer')
# Drop useless columns
df_bin1.drop(['id', 'months_bin'], axis=1, inplace=True)
df_bin1.columns
X = df_bin1[['sbp', 'dbp', 'creatinine', 'glucose', 'ldl', 'hgb',
       'atenolol', 'atorvastatin', 'bisoprolol', 'canagliflozin', 'carvedilol',
       'dapagliflozin', 'irbesartan', 'labetalol', 'losartan', 'lovastatin',
       'metformin', 'metoprolol', 'nebivolol', 'olmesartan', 'pitavastatin',
       'pravastatin', 'propranolol', 'rosuvastatin', 'simvastatin',
       'telmisartan', 'valsartan', 'race', 'gender', 'age']]

y = df_bin1['Stage_Progress']
# Split train-test data
from sklearn.model_selection import train_test_split

X_train, X_valid, y_train, y_valid = train_test_split(X, y, random_state=0) # default 75-25 split

# Select categorical columns
categorical_cols = ['race', 'gender']

# Select numerical columns
numerical_cols = ['sbp', 'dbp', 'creatinine', 'glucose', 'ldl', 'hgb',
       'atenolol', 'atorvastatin', 'bisoprolol', 'canagliflozin', 'carvedilol',
       'dapagliflozin', 'irbesartan', 'labetalol', 'losartan', 'lovastatin',
       'metformin', 'metoprolol', 'nebivolol', 'olmesartan', 'pitavastatin',
       'pravastatin', 'propranolol', 'rosuvastatin', 'simvastatin',
       'telmisartan', 'valsartan', 'age']<jupyter_output><empty_output><jupyter_text>Pipeline<jupyter_code>from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegressionCV
from sklearn.metrics import *

# Preprocess data

# Preprocessing for numerical data
numerical_transformer = StandardScaler()

# Preprocessing for categorical data
categorical_transformer = OneHotEncoder(handle_unknown='ignore')

# Bundle preprocessing for numerical and categorical data
preprocessor = ColumnTransformer(
    transformers=[
        ('num', numerical_transformer, numerical_cols),
        ('cat', categorical_transformer, categorical_cols)
    ])

# Define model
# Set weight to prevent bias, 4 to CKD (1), 1 to non-CKD (0)
model = LogisticRegressionCV(cv=5, random_state=0, max_iter=10000, class_weight={0: 1, 1: 4})

# Bundle preprocessing and modeling code in a pipeline
clf = Pipeline(steps=[('preprocessor', preprocessor),
                      ('model', model)
                     ])<jupyter_output><empty_output><jupyter_text>Logistic regression CV<jupyter_code># from sklearn.compose import ColumnTransformer
# from sklearn.pipeline import Pipeline
# from sklearn.preprocessing import OneHotEncoder, StandardScaler
# from sklearn.linear_model import LogisticRegressionCV
# from sklearn.metrics import *

# metrics_1 = []

# # Preprocess data

# # Preprocessing for numerical data
# numerical_transformer = StandardScaler()

# # Preprocessing for categorical data
# categorical_transformer = OneHotEncoder(handle_unknown='ignore')

# # Bundle preprocessing for numerical and categorical data
# preprocessor = ColumnTransformer(
#     transformers=[
#         ('num', numerical_transformer, numerical_cols),
#         ('cat', categorical_transformer, categorical_cols)
#     ])

# # Define model
# # Set weight to prevent bias, 4 to CKD (1), 1 to non-CKD (0)
# model = LogisticRegressionCV(cv=5, random_state=0, max_iter=10000, class_weight={0: 1, 1: 4})

# # Bundle preprocessing and modeling code in a pipeline
# clf = Pipeline(steps=[('preprocessor', preprocessor),
#                       ('model', model)
#                      ])

# # Preprocessing of training data, fit model 
# clf.fit(X_train, y_train)

# # Preprocessing of validation data, get predictions
# y_pred = clf.predict(X_valid)
# y_pred_proba = clf.predict_proba(X_valid)[:,1] # only get the probability of 1

# acc = accuracy_score(y_valid, y_pred)
# f1 = f1_score(y_valid, y_pred)
# prec = precision_score(y_valid, y_pred)
# rec = recall_score(y_valid, y_pred)
# spec = specificity(y_valid, y_pred)
# roc = roc_auc_score(y_valid, y_pred_proba)

# metric_output = [acc, f1, prec, rec, spec, roc]
# metrics_1.append(metric_output)
# # C selected
# clf.steps[1][1].C_

# # Coefficient
# clf.steps[1][1].coef_
cols = ['Accuracy', 'F1', 'Precision', 'Recall', 'Specificiy', 'ROC_AUC']
pd.DataFrame(metrics_1, columns = cols)<jupyter_output><empty_output><jupyter_text>GridSearchCV - logistic * Same C and results as the LogisticRegressionCV<jupyter_code>def specificity(y_valid, y_pred):
    tn, fp, fn, tp = confusion_matrix(y_valid, y_pred).ravel()
    
    specificity = tn / (tn+fp)
    return specificity
scores = ['accuracy', 'f1', 'precision', 'recall', 'Specificiy']
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.linear_model import LogisticRegression


for score in scores:
    
    print("# Tuning hyper-parameters for %s" % score)
    print()
    
    # Set weight to prevent bias, 4 to CKD (1), 1 to non-CKD (0)
    model = LogisticRegression(random_state=0, max_iter=10000, class_weight={0: 1, 1: 4})
#     model = LogisticRegression(random_state=0, max_iter=10000,class_weight='balanced')
    
    param_grid = {
    'model__C': np.logspace(-5, 5, 10)
    }
    
    pipe = Pipeline(steps=[('preprocessor', preprocessor),
                      ('model', model)])
    
    search = GridSearchCV(pipe, param_grid, n_jobs=-1)
    search.fit(X_train, y_train)
    print("Best parameter (CV score=%0.3f):" % search.best_score_)
    print()
    print("Best parameters set found on development set:")
    print()
    print(search.best_params_)
    print()
    print("Grid scores on development set:")
    print()
    means = search.cv_results_['mean_test_score']
    stds = search.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, search.cv_results_['params']):
        print("%0.3f (+/-%0.03f) for %r"
              % (mean, std * 2, params))
    print()

    print("Detailed classification report:")
    print()
    print("The model is trained on the full development set.")
    print("The scores are computed on the full evaluation set.")
    print()
    y_true, y_pred = y_valid, search.predict(X_valid)
    print(classification_report(y_true, y_pred))
    print()
    
search.best_estimator_
import joblib
joblib.dump(search.best_estimator_, 'models/6m/LogisticRegression.joblib')<jupyter_output><empty_output><jupyter_text>Decision tree<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.tree import DecisionTreeClassifier

for score in scores:
    
    print("# Tuning hyper-parameters for %s" % score)
    print()
    
    # Set weight to prevent bias, 4 to CKD (1), 1 to non-CKD (0)
    model = DecisionTreeClassifier(random_state=0, class_weight={0: 1, 1: 4}, min_samples_leaf = 30)
    
    param_grid = {
    'model__max_depth': np.linspace(10, 100, 10),
                
    }
    
    pipe = Pipeline(steps=[('preprocessor', preprocessor),
                      ('model', model)])
    
    search = GridSearchCV(pipe, param_grid, n_jobs=-1)
    search.fit(X_train, y_train)
    print("Best parameter (CV score=%0.3f):" % search.best_score_)
    print()
    print("Best parameters set found on development set:")
    print()
    print(search.best_params_)
    print()
    print("Grid scores on development set:")
    print()
    means = search.cv_results_['mean_test_score']
    stds = search.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, search.cv_results_['params']):
        print("%0.3f (+/-%0.03f) for %r"
              % (mean, std * 2, params))
    print()

    print("Detailed classification report:")
    print()
    print("The model is trained on the full development set.")
    print("The scores are computed on the full evaluation set.")
    print()
    y_true, y_pred = y_valid, search.predict(X_valid)
    print(classification_report(y_true, y_pred))
    print()
    
joblib.dump(search.best_estimator_, 'models/6m/DecisionTreeClassifier.joblib')<jupyter_output><empty_output><jupyter_text>lightgbm<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from lightgbm import LGBMClassifier

for score in scores:
    
    print("# Tuning hyper-parameters for %s" % score)
    print()
    
    # Set weight to prevent bias, 4 to CKD (1), 1 to non-CKD (0)
    model = LGBMClassifier(n_estimators=1000, objective='binary',scale_pos_weight=4)
    
    param_grid = {
    'model__learning_rate': [1e-05, 1e-04, 1e-03, 1e-02, 1e-01, 1, 10, 100]
                
    }
    
    pipe = Pipeline(steps=[('preprocessor', preprocessor),
                      ('model', model)])
    
    search = GridSearchCV(pipe, param_grid, n_jobs=-1)
    search.fit(X_train, y_train)
    print("Best parameter (CV score=%0.3f):" % search.best_score_)
    print()
    print("Best parameters set found on development set:")
    print()
    print(search.best_params_)
    print()
    print("Grid scores on development set:")
    print()
    means = search.cv_results_['mean_test_score']
    stds = search.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, search.cv_results_['params']):
        print("%0.3f (+/-%0.03f) for %r"
              % (mean, std * 2, params))
    print()

    print("Detailed classification report:")
    print()
    print("The model is trained on the full development set.")
    print("The scores are computed on the full evaluation set.")
    print()
    y_true, y_pred = y_valid, search.predict(X_valid)
    print(classification_report(y_true, y_pred))
    print()
    
joblib.dump(search.best_estimator_, 'models/6m/LGBMClassifier.joblib')<jupyter_output><empty_output><jupyter_text>random forest<jupyter_code>from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier

for score in scores:
    
    print("# Tuning hyper-parameters for %s" % score)
    print()
    
    # Set weight to prevent bias, 4 to CKD (1), 1 to non-CKD (0)
    model = RandomForestClassifier(class_weight={0: 1, 1: 4}, n_estimators=800, n_jobs=-1, verbose=1)
    
    param_grid = {
    'model__max_depth': list(range(1,31))
                
    }
    
    pipe = Pipeline(steps=[('preprocessor', preprocessor),
                      ('model', model)])
    
    search = GridSearchCV(pipe, param_grid, n_jobs=-1)
    search.fit(X_train, y_train)
    print("Best parameter (CV score=%0.3f):" % search.best_score_)
    print()
    print("Best parameters set found on development set:")
    print()
    print(search.best_params_)
    print()
    print("Grid scores on development set:")
    print()
    means = search.cv_results_['mean_test_score']
    stds = search.cv_results_['std_test_score']
    for mean, std, params in zip(means, stds, search.cv_results_['params']):
        print("%0.3f (+/-%0.03f) for %r"
              % (mean, std * 2, params))
    print()

    print("Detailed classification report:")
    print()
    print("The model is trained on the full development set.")
    print("The scores are computed on the full evaluation set.")
    print()
    y_true, y_pred = y_valid, search.predict(X_valid)
    print(classification_report(y_true, y_pred))
    print()<jupyter_output># Tuning hyper-parameters for accuracy<jupyter_text>Compare models<jupyter_code>metrics_1 = []

clf = search.best_estimator_

# Preprocessing of training data, fit model 
clf.fit(X_train, y_train)

# Preprocessing of validation data, get predictions
y_pred = clf.predict(X_valid)
y_pred_proba = clf.predict_proba(X_valid)[:,1] # only get the probability of 1

acc = accuracy_score(y_valid, y_pred)
f1 = f1_score(y_valid, y_pred)
prec = precision_score(y_valid, y_pred)
rec = recall_score(y_valid, y_pred)
spec = specificity(y_valid, y_pred)
roc = roc_auc_score(y_valid, y_pred_proba)

metric_output = [acc, f1, prec, rec, spec, roc]
metrics_1.append(metric_output)
cols = ['Accuracy', 'F1', 'Precision', 'Recall', 'Specificiy', 'ROC_AUC']
pd.DataFrame(metrics_1, columns = cols)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1266092785861202, 'fraction_numerical': 0.015997562276224574, 'mean_word_length': 4.268057784911718, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '657198', 'license': 'Apache-2.0', 'n_tokens_mistral': 4571, 'n_tokens_neox': 4348, 'n_words': 1052, 'path': '.ipynb_checkpoints/Modelling-checkpoint.ipynb', 'repo_name': 'singchenyeo/CKD', 'size': '13127'}