<jupyter_start><jupyter_text>Coronary Heart Disease Prediction<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

# Sklearn
from sklearn.preprocessing import normalize 
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer 
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.linear_model import SGDClassifier, LogisticRegression
from sklearn import tree
from sklearn.naive_bayes import MultinomialNB,GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import VotingClassifier
from sklearn import svm
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, StandardScaler


from sklearn.calibration import CalibratedClassifierCV

# Evaluation Metrics
from sklearn.metrics import roc_curve, auc, roc_auc_score
from sklearn.metrics import mean_squared_error as mse
from sklearn.metrics import r2_score
from sklearn.metrics import mean_absolute_error as mae
from math import sqrt
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix, precision_recall_fscore_support,classification_report
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.linear_model import Ridge
from sklearn.svm import SVR

df=pd.read_csv('framingham.csv')

df.head()
columns = df.columns
columns
df.dtypes

df.hist(figsize=(12,12))

df.describe()<jupyter_output><empty_output><jupyter_text>Filling Null Values (NaN - Not a Number) with Attribute Mean<jupyter_code>for i in columns:
    df[i] = df[i].fillna(df[i].mean())
df.describe()<jupyter_output><empty_output><jupyter_text>Analysing Class Distribution TenYearCHD attribute gives whether an individual is at risk of suffering from CHD in next 10 years0 - Not at Risk1 - At Risk<jupyter_code>target_count = df['TenYearCHD'].value_counts()
print('Class 0:', target_count[0])
print('Class 1:', target_count[1])
print('Proportion:', round(target_count[0] / target_count[1], 2), ': 1')

target_count.plot(kind='bar', title='Count (target)');
# Class count
count_class_0, count_class_1 = df['TenYearCHD'].value_counts()

# Divide by class
df_class_0 = df[df['TenYearCHD'] == 0]
df_class_1 = df[df['TenYearCHD'] == 1]
df_class_0
df_class_1<jupyter_output><empty_output><jupyter_text>Normaizing Class Distribution<jupyter_code>df_class_1_over = df_class_1.sample(count_class_0, replace=True)
df_test_over = pd.concat([df_class_0, df_class_1_over], axis=0)

print('Random over-sampling:')
print(df_test_over['TenYearCHD'].value_counts())

df_test_over['TenYearCHD'].value_counts().plot(kind='bar', title='Count (target)');

df = df_test_over
df.head()
df.shape<jupyter_output><empty_output><jupyter_text>Z-score Normalization on selected numerical attributes<jupyter_code>from scipy.stats import zscore
df['cigsPerDay'] = df[['cigsPerDay']].apply(zscore)
df['totChol'] = df[['totChol']].apply(zscore)
df['sysBP'] = df[['sysBP']].apply(zscore)
df['diaBP'] = df[['diaBP']].apply(zscore)
df['BMI'] = df[['BMI']].apply(zscore)
df['heartRate'] = df[['heartRate']].apply(zscore)
df['glucose'] = df[['glucose']].apply(zscore)
df
X = df.drop(['TenYearCHD','education'],axis = 1)
Y = df['TenYearCHD']
X.head()
Y.head()
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,test_size=0.25,random_state=0)

print(X_train.shape)
print(X_test.shape)
#Y_train = np.asarray(Y_train).reshape(-1,1)
#Y_test = np.asarray(Y_test).reshape(-1,1)
print(Y_train.shape)
print(Y_test.shape)<jupyter_output>(5394, 14)
(1798, 14)
(5394,)
(1798,)<jupyter_text>KNN<jupyter_code>import time
start = time.time()

knn =  KNeighborsClassifier(n_neighbors=2).fit(X_train,Y_train)
y_pred=knn.predict(X_test)

end = time.time()

print("Accuracy:",accuracy_score(Y_test, y_pred))

print(end-start)

cm = confusion_matrix(Y_test,y_pred)
print(cm)

TP =cm[0][0]
FN =cm[0][1]
TN = cm[1][1]
FP = cm[1][0]

print("TP: ", TP)
print("TN", TN)
print("FP", FP)
print("FN", FN)

Precision = (TP / (TP+FP))
Recall = (TP / (TP+FN))

print("Accuracy: ", ((TP+TN) / (TP+FP+FN+TN)))
print("Precision", (TP / (TP+FP)))
print("Recall", (TP / (TP+FN)))
print("Specificity", (TN / (TN+FP)) )
print("F1 Score",  ((2 * Precision * Recall) / (Precision + Recall)))

print(classification_report(Y_test, y_pred))



df.groupby('TenYearCHD').count()
error = []
for i in range(1, 40):  
    knn = KNeighborsClassifier(n_neighbors=i)
    knn.fit(X_train, Y_train)
    pred_i = knn.predict(X_test)
    error.append(np.mean(pred_i != Y_test))
    
plt.figure(figsize=(12, 6))  
plt.plot(range(1, 40), error, color='red', linestyle='dashed', marker='o',  
         markerfacecolor='blue', markersize=10)
plt.title('Error Rate K Value')  
plt.xlabel('K Value')  
plt.ylabel('Mean Error')  
from sklearn.model_selection import cross_val_score
knn =  KNeighborsClassifier(n_neighbors=2).fit(X_train,Y_train)
cv_scores = cross_val_score(knn, X, Y, cv=10)

print(cv_scores)
print("\nCross Validation Scores mean:{}".format(np.mean(cv_scores)))<jupyter_output>[ 0.9125      0.89861111  0.91805556  0.92777778  0.92777778  0.91111111
  0.92618384  0.91922006  0.92200557  0.90668524]

Cross Validation Scores mean:0.9169928040854224<jupyter_text>Logistic Regression<jupyter_code>lr =  LogisticRegression(solver='saga',max_iter=10).fit(X_train,Y_train)
y_pred=lr.predict(X_test)
print("Accuracy:",accuracy_score(Y_test, y_pred))<jupyter_output>Accuracy: 0.643492769744<jupyter_text>Decision Tree<jupyter_code>start = time.time()


dt = tree.DecisionTreeClassifier().fit(X_train,Y_train)
y_pred=dt.predict(X_test)
end = time.time()

print("Accuracy:",accuracy_score(Y_test, y_pred))
print(end-start)



cm = confusion_matrix(Y_test,y_pred)
print(cm)

TP =cm[0][0]
FN =cm[0][1]
TN = cm[1][1]
FP = cm[1][0]

print("TP: ", TP)
print("TN", TN)
print("FP", FP)
print("FN", FN)

Precision = (TP / (TP+FP))
Recall = (TP / (TP+FN))

print("Accuracy: ", ((TP+TN) / (TP+FP+FN+TN)))
print("Precision", (TP / (TP+FP)))
print("Recall", (TP / (TP+FN)))
print("Specificity", (TN / (TN+FP)) )
print("F1 Score",  ((2 * Precision * Recall) / (Precision + Recall)))

print(classification_report(Y_test, y_pred))<jupyter_output>Accuracy: 0.905450500556
0.03830528259277344
[[738 155]
 [ 15 890]]
TP:  738
TN 890
FP 15
FN 155
Accuracy:  0.905450500556
Precision 0.980079681275
Recall 0.826427771557
Specificity 0.983425414365
F1 Score 0.896719319563
              precision    recall  f1-score   support

           0       0.98      0.83      0.90       893
           1       0.85      0.98      0.91       905

   micro avg       0.91      0.91      0.91      1798
   macro avg       0.92      0.90      0.90      1798
weighted avg       0.92      0.91      0.90      1798<jupyter_text>Gaussian Naive Bayes<jupyter_code>gnb = GaussianNB()
gnb.fit(X_train,Y_train)
y_pred=gnb.predict(X_test)
print("Accuracy:",accuracy_score(Y_test, y_pred))<jupyter_output>Accuracy: 0.58787541713<jupyter_text>Random Forest<jupyter_code>start = time.time()

rf=RandomForestClassifier(n_estimators=10)
rf.fit(X_train,Y_train)
y_pred=rf.predict(X_test)

end = time.time()

print("Accuracy:",accuracy_score(Y_test, y_pred))

print(end-start)

cm = confusion_matrix(Y_test,y_pred)
print(cm)

TP =cm[0][0]
FN =cm[0][1]
TN = cm[1][1]
FP = cm[1][0]

print("TP: ", TP)
print("TN", TN)
print("FP", FP)
print("FN", FN)

Precision = (TP / (TP+FP))
Recall = (TP / (TP+FN))

print("Accuracy: ", ((TP+TN) / (TP+FP+FN+TN)))
print("Precision", (TP / (TP+FP)))
print("Recall", (TP / (TP+FN)))
print("Specificity", (TN / (TN+FP)) )
print("F1 Score",  ((2 * Precision * Recall) / (Precision + Recall)))

print(classification_report(Y_test, y_pred))



from sklearn.model_selection import cross_val_score

cv_scores = cross_val_score(rf, X, Y, cv=10)

print(cv_scores)
print("\nCross Validation Scores mean:{}".format(np.mean(cv_scores)))<jupyter_output>[ 0.9625      0.96388889  0.96805556  0.97916667  0.98472222  0.96666667
  0.97214485  0.97771588  0.9735376   0.96239554]

Cross Validation Scores mean:0.9710793871866296<jupyter_text>ROC(Receiver Operating Characteristics) Curve<jupyter_code>import sklearn.metrics as metrics
y_pred_knn =knn.predict_proba(X_test)[:,1]
y_pred_rf =rf.predict_proba(X_test)[:,1]
y_pred_dt = dt.predict_proba(X_test)[:,1]
#_pred_lr=lr.predict_proba(X_test)[:,1]

models=[y_pred_rf,y_pred_dt,y_pred_knn]
label=['RF','DT','KNN']
fig1 = plt.figure(figsize=[6,6])

for i in range(3):
    fpr, tpr,thresholds= metrics.roc_curve(Y_test,models[i])
    #rint('model:',label[i])
    #rint('thresholds:',np.round(thresholds,3))
    #rint('tpr:       ',np.round(tpr,3))
    #rint('fpr:       ',np.round(fpr,3))
    roc_auc = auc(fpr, tpr)
    plt.plot(fpr,tpr,lw=2,label = ' %s (AUC = %0.2f)' % (label[i],roc_auc))
    plt.xlim([0.0,1.0])
    plt.ylim([0.0,1.0])
    plt.title('ROC curve ')
    plt.xlabel('False positive rate (1-specificity)')
    plt.ylabel('True positive rate (sensitivity)')
    plt.legend(loc=4,)
from sklearn.model_selection import StratifiedKFold
import matplotlib.patches as patches
from sklearn.metrics import roc_curve,auc
from scipy import interp

cvscores = []

clf=RandomForestClassifier(n_estimators=10)
clf.fit(X_train,Y_train)
cv = StratifiedKFold(n_splits=10)
# plot arrows
fig1 = plt.figure(figsize=[8,8])
#ax1 = fig1.add_subplot(111,aspect = 'equal')
#ax1.add_patch(
 #   patches.Arrow(0.45,0.5,-0.25,0.25,width=0.3,color='green',alpha = 0.5)
 #   )
#ax1.add_patch(
 #   patches.Arrow(0.5,0.45,0.25,-0.25,width=0.3,color='red',alpha = 0.5)
  #  )

tprs = []
aucs = []
mean_fpr = np.linspace(0,1,100)
i = 1
start = time.time()
for train,test in cv.split(X,Y):
    prediction = clf.fit(X_train,Y_train).predict_proba(X_test)
    y_pred=clf.predict(X_test)
    print("Accuracy:",accuracy_score(Y_test, y_pred))
    cvscores.append(accuracy_score(Y_test, y_pred))
    fpr, tpr, t = roc_curve(Y_test, prediction[:, 1])
    tprs.append(interp(mean_fpr, fpr, tpr))
    roc_auc = auc(fpr, tpr)
    aucs.append(roc_auc)
    plt.plot(fpr, tpr, lw=2, alpha=0.3, label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))
    i= i+1
end = time.time()
plt.plot([0,1],[0,1],linestyle = '--',lw = 2,color = 'black')
mean_tpr = np.mean(tprs, axis=0)
mean_auc = auc(mean_fpr, mean_tpr)
plt.plot(mean_fpr, mean_tpr, color='blue',
         label=r'Mean ROC (AUC = %0.2f )' % (mean_auc),lw=2, alpha=1)

plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC')
plt.legend(loc="lower right")
#plt.text(0.32,0.7,'More accurate area',fontsize = 12)
#plt.text(0.63,0.4,'Less accurate area',fontsize = 12)
plt.show()

print(cvscores)
print("\nCross Validation Scores mean:{}".format(np.mean(cvscores)))

print(end-start)<jupyter_output>Accuracy: 0.954949944383
Accuracy: 0.957730812013
Accuracy: 0.954949944383
Accuracy: 0.950500556174
Accuracy: 0.952169076752
Accuracy: 0.958286985539
Accuracy: 0.959955506118
Accuracy: 0.956618464961
Accuracy: 0.958286985539
Accuracy: 0.959955506118<jupyter_text>SVM<jupyter_code>clf=svm.SVC()
clf.fit(X_train,Y_train)
y_pred=clf.predict(X_test)
print("Accuracy:",accuracy_score(Y_test, y_pred))<jupyter_output>/home/divya/anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)<jupyter_text>CNN<jupyter_code>import keras
from keras.models import Sequential
from keras.layers import Dense
print(keras.__version__)
clf = Sequential()
clf.add(Dense(units = 12, init = 'uniform', activation = 'relu', input_dim = X_train.shape[1]))
clf.add(Dense(units = 8, init = 'uniform', activation = 'relu'))
clf.add(Dense(units = 1, init = 'uniform', activation = 'sigmoid'))
clf.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

clf.fit(X_train,Y_train, batch_size = 20, nb_epoch = 50)
ypred = clf.predict(X_test)
ypred = (ypred > 0.5)
accuracy = accuracy_score(Y_test,ypred)
accuracy<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1547042900500369, 'fraction_numerical': 0.06406365351488803, 'mean_word_length': 5.784641068447413, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '73131', 'license': 'MIT', 'n_tokens_mistral': 5463, 'n_tokens_neox': 4904, 'n_words': 891, 'path': 'CHD_Prediction_Final.ipynb', 'repo_name': 'DivyaKrishnani/Coronary-Heart-Disease-Prediction-using-ML', 'size': '12191'}