<jupyter_start><jupyter_text>Predicting 30-Day Readmission for Patients by: <NAME> Data set from UCI Machine Learning Repository  Submitted on behalf of the Center for Clinical and Translational Research  Virginia Commonwealth University  Importing the neccessary packages<jupyter_code>import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris

#plotting
import matplotlib.pyplot as plt
import seaborn as sns

#linear algebra
import numpy as np

#data processing
import pandas as pd

#splitting data
from sklearn.model_selection import train_test_split

#scaling
from sklearn.preprocessing import StandardScaler

#tuning
from sklearn.model_selection import RandomizedSearchCV

#model evaluation
from sklearn import metrics<jupyter_output><empty_output><jupyter_text>EDA of the raw data set<jupyter_code>#importing data
file = r"C:\Users\chris\OneDrive\Desktop\550 analysis\DiabetesProject\MODdiabetesSMOTE100k.csv"
df = pd.read_csv(file)
df.head(5)
df.info()
### Visualizing the data
df.plot()
df['age'].hist()
df['num_medications'].hist()
df['number_diagnoses'].hist(bins=8)<jupyter_output><empty_output><jupyter_text>Splitting data for train/test<jupyter_code>X = df.drop(['readmitted'], axis = 1)
y = df.pop('readmitted')
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)
## Scaling data
sc = StandardScaler()

X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)<jupyter_output><empty_output><jupyter_text>Predictive Modeling<jupyter_code>from sklearn.discriminant_analysis import LinearDiscriminantAnalysis


LDA = LinearDiscriminantAnalysis(solver='svd')

# fit the model
LDA.fit(X_train, y_train)

y_predict_LDA = LDA.predict(X_test)

# model score
predict_train_LDA = LDA.predict(X_train)
predict_test_LDA = LDA.predict(X_test)

# accuracy score
LDA_train_score = LDA.score(X_train,y_train)
LDA_test_score = LDA.score(X_test,y_test)

# f1-score
LDA_f1_score = metrics.f1_score(y_test,predict_test_LDA)
LDA_recall = metrics.recall_score(y_test, predict_test_LDA)

print('Accuracy on Train set',LDA_train_score)
print('Accuracy on Test set',LDA_test_score)
print('F1-score on Test set:',LDA_f1_score)
print(metrics.classification_report(y_test, predict_test_LDA))
from sklearn.naive_bayes import GaussianNB

NB_model = GaussianNB()

# fit the model
NB_model.fit(X_train, y_train)

y_predict_NB = NB_model.predict(X_test)

# model score
predict_train_NB = NB_model.predict(X_train)
predict_test_NB = NB_model.predict(X_test)

# accuracy score
NB_train_score = NB_model.score(X_train,y_train)
NB_test_score = NB_model.score(X_test,y_test)

# f1-score
NB_f1_score = metrics.f1_score(y_test,predict_test_NB)
NB_recall = metrics.recall_score(y_test, predict_test_NB)

print('Accuracy on Train set',NB_train_score)
print('Accuracy on Test set',NB_test_score)
print('F1-score on Test set:',NB_f1_score)
print(metrics.classification_report(y_test, predict_test_NB))
from sklearn.linear_model import LogisticRegression

LR_model = LogisticRegression(max_iter=2000)

# fit the model
LR_model.fit(X_train, y_train)

y_predict_LR = LR_model.predict(X_test)

# model score
predict_train_LR = LR_model.predict(X_train)
predict_test_LR = LR_model.predict(X_test)

# accuracy score
LR_train_score = LR_model.score(X_train,y_train)
LR_test_score = LR_model.score(X_test,y_test)

# f1-score
LR_f1_score = metrics.f1_score(y_test,predict_test_LR)
LR_recall = metrics.recall_score(y_test,predict_test_LR)

print('Accuracy on Train set',LR_train_score)
print('Accuracy on Test set',LR_test_score)
print('F1-score on Test set:',LR_f1_score)
print(metrics.classification_report(y_test, predict_test_LR))
from sklearn.tree import DecisionTreeClassifier

DT_model= DecisionTreeClassifier()

# fit the model
DT_model.fit(X_train,y_train)

# model score
predict_train_DT = DT_model.predict(X_train)
predict_test_DT = DT_model.predict(X_test)

# accuracy score
DT_train_score = DT_model.score(X_train,y_train)
DT_test_score = DT_model.score(X_test,y_test)

# f1-score
DT_f1_score = metrics.f1_score(y_test, predict_test_DT)

print('Accuracy on Train set',DT_train_score)
print('Accuracy on Test set',DT_test_score)
print('F1-score on Test set:',DT_f1_score)
print('\n')
print(metrics.classification_report(y_test, predict_test_DT))
from sklearn.ensemble import RandomForestClassifier

# create object model
RF_model = RandomForestClassifier()

# fit the model
RF_model.fit(X_train,y_train)

# model score
predict_train_RF = RF_model.predict(X_train)
predict_test_RF = RF_model.predict(X_test)

# accuracy score
RF_train_score = RF_model.score(X_train,y_train)
RF_test_score = RF_model.score(X_test,y_test)

# f1-score
RF_f1_score = metrics.f1_score(y_test,predict_test_RF)

print('Accuracy on Train set',RF_train_score)
print('Accuracy on Test set',RF_test_score)
print('F1-score on Test set:',RF_f1_score)
print(metrics.classification_report(y_test,predict_test_RF))
feature_score = pd.DataFrame(RF_model.feature_importances_, index =df.columns,  columns=['Feature_importance']).sort_values('Feature_importance', ascending=False)

plt.rcParams.update({'font.size': 22})
plt.style.use('fivethirtyeight')
feature_score.plot(kind='bar',figsize=(10,6));
plt.xticks(rotation=70);
plt.title('Feature Importance Random forest');
from scipy.stats import pearsonr
num_medications = df['num_medications']
age = df['age']
corr, _ = pearsonr(num_medications, age)
print('Pearsons correlation: %.3f' % corr)
from scipy.stats import pearsonr
num_medications = df['number_diagnoses']
age = df['num_medications']
corr, _ = pearsonr(num_medications, age)
print('Pearsons correlation: %.3f' % corr)
import scipy.stats as stats

stats.f_oneway(df['readmitted'][df['diabetes_DRGs'] == 1],
               df['readmitted'][df['diabetes_DRGs'] == 0])
stats.f_oneway(df['readmitted'][df['num_medications'] == 1],
               df['readmitted'][df['diabetes_DRGs'] == 0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10715479772651287, 'fraction_numerical': 0.009528585757271816, 'mean_word_length': 7.916542473919523, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, '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': '299403', 'license': 'MIT', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2189, 'n_words': 407, 'path': 'MachineLearningReadmission.ipynb', 'repo_name': 'chrisvidetto1/Exploratory_Data_Analysis', 'size': '5989'}