<jupyter_start><jupyter_text>Natural Language<jupyter_code>import string
import nltk
from nltk import word_tokenize
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords
def clean_text(texts):
    texts = texts.fillna(' ')
    texts = texts.str.replace('\n',' ')
    texts = texts.str.replace('\r',' ')

    table = str.maketrans('', '', string.punctuation + '0123456789')
    texts = [text.lower().translate(table) for text in texts]

    return texts
adm_notes['TEXT'] = clean_text(adm_notes['TEXT'])
stop_words = stopwords.words('english')
stop_words = stop_words + ['patient', 'date', 'admission', 'discharge', 'lastname', 'firstname', 'sex']
porter = PorterStemmer()
def tokenize_stem(text):
    words = word_tokenize(text)
    words = [word for word in words if word not in stop_words]
    words = [porter.stem(word) for word in words]
    return words<jupyter_output><empty_output><jupyter_text>Model Words, Train and Test<jupyter_code>from sklearn import metrics
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression, Ridge, Lasso, ElasticNet
from sklearn.model_selection import GridSearchCV, train_test_split<jupyter_output><empty_output><jupyter_text>Repartition data<jupyter_code>df_train, df_test = train_test_split(adm_notes, test_size=0.2)<jupyter_output><empty_output><jupyter_text>Subsample non-readmitted patients to match size of readmitted ones<jupyter_code>rows_pos = df_train['READM_WITHIN_30'] == 1
df_train_pos = df_train.loc[rows_pos]
df_train_neg = df_train.loc[~rows_pos]

df_train = pd.concat([df_train_pos, df_train_neg.sample(n = len(df_train_pos))], axis = 0)
df_train = df_train.sample(n = len(df_train)).reset_index(drop = True)
rows_pos = df_test['READM_WITHIN_30'] == 1
df_test_pos = df_test.loc[rows_pos]
df_test_neg = df_test.loc[~rows_pos]

df_test = pd.concat([df_test_pos, df_test_neg.sample(n = len(df_test_pos))], axis = 0)
df_test = df_test.sample(n = len(df_test)).reset_index(drop = True)<jupyter_output><empty_output><jupyter_text>Sparse Matrix with word count<jupyter_code>vect = CountVectorizer(lowercase = True, max_features = 3000, tokenizer = tokenize_stem)
vect.fit(df_train['TEXT'].values)

X_train_tf = vect.transform(df_train['TEXT'].values)
X_test_tf = vect.transform(df_test['TEXT'].values)

y_train = df_train['READM_WITHIN_30']
y_test = df_test['READM_WITHIN_30']<jupyter_output><empty_output><jupyter_text>Proportion of 1 in test sample<jupyter_code>sum(y_test == 1) / sum(y_test == 0)<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>grid = {'C': [1, 0.1, 0.01, 0.001, 0.0005, 0.0001]}

model_log = GridSearchCV(LogisticRegression(penalty='l2'), param_grid=grid)
model_log.fit(X_train_tf, y_train)
model_log.best_params_
y_train_prob = model_log.predict_proba(X_train_tf)[:,1]
y_test_prob = model_log.predict_proba(X_test_tf)[:,1]

y_train_preds = np.round(y_train_prob)
y_test_preds = np.round(y_test_prob)
fpr, tpr, thresholds = metrics.roc_curve(y_train, y_train_prob)
fprt, tprt, thresholds = metrics.roc_curve(y_test, y_test_prob)

auc_train = metrics.auc(fpr, tpr)
auc_test = metrics.auc(fprt, tprt)

acc_train = metrics.accuracy_score(y_train, y_train_preds)
acc_test = metrics.accuracy_score(y_test, y_test_preds)

prec_train = metrics.precision_score(y_train, y_train_preds)
prec_test = metrics.precision_score(y_test, y_test_preds)

rec_train = metrics.recall_score(y_train, y_train_preds)
rec_test = metrics.recall_score(y_test, y_test_preds)
print('Set\t\t', 'Train\t\t', '   Test')
print('AUC\t\t', auc_train, auc_test)
print('Accuracy\t', acc_train, acc_test)
print('Precission\t', prec_train, prec_test)
print('Recall\t\t', rec_train, rec_test)
sns.set('talk', 'whitegrid', 'dark', font_scale=0.7,
        rc={"lines.linewidth": 1, 'grid.linestyle': '--'})

plt.plot(fpr, tpr, label='Train')
plt.plot(fprt, tprt, label='Test')
plt.title("ROC Curve")
plt.legend(loc="lower right")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")

plt.savefig('logistic_auroc.pgf')<jupyter_output><empty_output><jupyter_text>Ridge Regression<jupyter_code>grid={'alpha': np.arange(10000, 20000, 1000)}
model_ridge = GridSearchCV(Ridge(), param_grid=grid)
model_ridge.fit(X_train_tf, y_train)
model_ridge.best_params_
y_train_reg = model_ridge.predict(X_train_tf)
y_test_reg = model_ridge.predict(X_test_tf)

y_train_preds = np.round(y_train_reg.clip(0, 1))
y_test_preds = np.round(y_test_reg.clip(0, 1))
fpr, tpr, thresholds = metrics.roc_curve(y_train, y_train_reg)
fprt, tprt, thresholds = metrics.roc_curve(y_test, y_test_reg)

auc_train = metrics.auc(fpr, tpr)
auc_test = metrics.auc(fprt, tprt)

acc_train = metrics.accuracy_score(y_train, y_train_preds)
acc_test = metrics.accuracy_score(y_test, y_test_preds)

prec_train = metrics.precision_score(y_train, y_train_preds)
prec_test = metrics.precision_score(y_test, y_test_preds)

rec_train = metrics.recall_score(y_train, y_train_preds)
rec_test = metrics.recall_score(y_test, y_test_preds)
print('Set\t\t', 'Train\t\t', '   Test')
print('AUC\t\t', auc_train, auc_test)
print('Accuracy\t', acc_train, acc_test)
print('Precission\t', prec_train, prec_test)
print('Recall\t\t', rec_train, rec_test)
plt.plot(fpr, tpr, label='Train')
plt.plot(fprt, tprt, label='Test')
plt.title("ROC Curve")
plt.legend(loc="lower right")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")

plt.savefig('ridge_auroc.pgf')<jupyter_output><empty_output><jupyter_text>Lasso Regression<jupyter_code>grid = {'alpha':[0.1,0.01,0.001,0.0001]}
model_lasso = GridSearchCV(Lasso(), param_grid=grid)
model_lasso.fit(X_train_tf, y_train)
model_lasso.best_params_
y_train_reg = model_lasso.predict(X_train_tf)
y_test_reg = model_lasso.predict(X_test_tf)

y_train_preds = np.round(y_train_reg.clip(0, 1))
y_test_preds = np.round(y_test_reg.clip(0, 1))
fpr, tpr, thresholds = metrics.roc_curve(y_train, y_train_prob)
fprt, tprt, thresholds = metrics.roc_curve(y_test, y_test_prob)

auc_train = metrics.auc(fpr, tpr)
auc_test = metrics.auc(fprt, tprt)

acc_train = metrics.accuracy_score(y_train, y_train_preds)
acc_test = metrics.accuracy_score(y_test, y_test_preds)

prec_train = metrics.precision_score(y_train, y_train_preds)
prec_test = metrics.precision_score(y_test, y_test_preds)

rec_train = metrics.recall_score(y_train, y_train_preds)
rec_test = metrics.recall_score(y_test, y_test_preds)
print('Set\t\t', 'Train\t\t', '   Test')
print('AUC\t\t', auc_train, auc_test)
print('Accuracy\t', acc_train, acc_test)
print('Precission\t', prec_train, prec_test)
print('Recall\t\t', rec_train, rec_test)
plt.plot(fpr, tpr, label='Train')
plt.plot(fprt, tprt, label='Test')
plt.title("ROC Curve")
plt.legend(loc="lower right")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")

plt.savefig('lasso_auroc.pgf')<jupyter_output><empty_output><jupyter_text>ElasticNet<jupyter_code>np.arange(0.1, 1.0, 0.1)
grid = {
    #"max_iter": [1, 2, 3, 4, 5],
    "alpha": [0.01, 0.1, 1, 10, 100],
    "l1_ratio": np.arange(0.1, 1.0, 0.1)
}
model_enet = GridSearchCV(ElasticNet(), param_grid=grid)
model_enet.fit(X_train_tf, y_train)
model_enet.best_params_
y_train_prob = model_enet.predict(X_train_tf)
y_test_prob = model_enet.predict(X_test_tf)

y_train_preds = np.round(y_train_prob)
y_test_preds = np.round(y_test_prob)
fpr, tpr, thresholds = metrics.roc_curve(y_train, y_train_prob)
fprt, tprt, thresholds = metrics.roc_curve(y_test, y_test_prob)

auc_train = metrics.auc(fpr, tpr)
auc_test = metrics.auc(fprt, tprt)

acc_train = metrics.accuracy_score(y_train, y_train_preds)
acc_test = metrics.accuracy_score(y_test, y_test_preds)

prec_train = metrics.precision_score(y_train, y_train_preds)
prec_test = metrics.precision_score(y_test, y_test_preds)

rec_train = metrics.recall_score(y_train, y_train_preds)
rec_test = metrics.recall_score(y_test, y_test_preds)
print('Set\t\t', 'Train\t\t', '   Test')
print('AUC\t\t', auc_train, auc_test)
print('Accuracy\t', acc_train, acc_test)
print('Precission\t', prec_train, prec_test)
print('Recall\t\t', rec_train, rec_test)
plt.plot(fpr, tpr, label='Train')
plt.plot(fprt, tprt, label='Test')
plt.title("ROC Curve")
plt.legend(loc="lower right")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")

plt.savefig('elastic_auroc.pgf')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1407185628742515, 'fraction_numerical': 0.014610778443113773, 'mean_word_length': 7.689906347554631, 'pattern_counts': {'":': 4, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '451544', 'license': 'CC0-1.0', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3390, 'n_words': 534, 'path': 'Code/4_linear_models.ipynb', 'repo_name': 'javiermzll/NLP-Prediction-Unplanned-Readmissions', 'size': '8350'}