<jupyter_start><jupyter_text>Expectation Reflection for Heart Disease DiagnosisIn this work, we apply our method, Expectation Reflection (ER), to predict heart disease. We compare the performance of ER with other existing methods such as Logistic Regression, Naive Bayes, Dicision Tree, Random Forest, k-nearest neighbors, and Support Vector Machines (SVM).<jupyter_code>import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.metrics import accuracy_score

import expectation_reflection as ER

from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier

import matplotlib.pyplot as plt
%matplotlib inline
np.random.seed(1)
# load data
df = pd.read_csv('../heartdisease_data.csv',sep= ',')
df[0:10]<jupyter_output><empty_output><jupyter_text>The data contains 13 features:0) age: Age (years) --> discrete 1) sex: Sex (1: male, 0: female) --> binary 2) cp: Chest pain type (1: typical angina, 2: atypical angina, 3: non-anginal pain, 4: asymptomatic) --> categorical 3) trestbps: Resting blood pressure (mm Hg on admission to the hospital) --> continuous 4) chol: Cholesterol measurement (mg/dl) --> continuous 5) fbs: Fasting blood sugar (0:  120 mg/dl) --> binary 6) restecg: Resting electrocardiographic measurement (0: normal, 1: having ST-T wave abnormality, 2: showing probable or definite left ventricular hypertrophy by Estes' criteria) --> categorical 7) thalach: Maximum heart rate achieved --> continuous8) exang: Exercise induced angina (1: yes; 0: no) --> binary 9) oldpeak: ST depression induced by exercise relative to rest ('ST' relates to positions on the ECG plot) --> continuous10) slope: The slope of the peak exercise ST segment (1: upsloping, 2: flat, 3: downsloping) --> categorical11) ca: The number of major vessels (0-4) --> categorical 12) thal: Thalassemia (a type of blood disorder)  (1: normal; 2: fixed defect; 3: reversable defect) --> categorical and 1 target: Heart disease (0: no, 1: yes)<jupyter_code># select features and target:
df = np.array(df).astype(float)

# features:
X = df[:,:-1]
l,n = X.shape
print(l,n)

# target:
y = df[:,-1]
# convert 1,0 to 1,-1:
y = 2*y - 1<jupyter_output>303 13<jupyter_text>Convert categorical variables to one hot<jupyter_code>from sklearn.preprocessing import OneHotEncoder
onehot_encoder = OneHotEncoder(sparse=False,categories='auto')

# sex = X[:,1] = 0,1 --> 2
#x1 = onehot_encoder.fit_transform(X[:,1].reshape(-1,1))
x1 = 2*X[:,1] - 1   # 0,1 --> -1, 1

# cp = X[:,2] = 1,2,3,4 --> 4
x2 = onehot_encoder.fit_transform(X[:,2].reshape(-1,1))

# fbs = X[:,5] = 0,1 --> 2
#x5 = onehot_encoder.fit_transform(X[:,5].reshape(-1,1)) 
x5 = 2*X[:,5] - 1  # 0,1 --> -1, 1

# restecg = X[:,6] = 0,1,2 --> 3
x6 = onehot_encoder.fit_transform(X[:,6].reshape(-1,1))

#exang: = X[:,8] = 0,1 --> 2
#x8 = onehot_encoder.fit_transform(X[:,8].reshape(-1,1))
x8 = X[:,8]  # 0,1 --> -1, 1

# X[:,10] = 0,1,2 --> 3
x10 = onehot_encoder.fit_transform(X[:,10].reshape(-1,1))

# X[:,11] = 0,1,2,3,4 --> 5
x11 = onehot_encoder.fit_transform(X[:,11].reshape(-1,1)) 

# X[:,12] = 0,1,2,3 --> 4
x12= onehot_encoder.fit_transform(X[:,12].reshape(-1,1)) 
Xnew = np.hstack([X[:,0][:,np.newaxis],x1[:,np.newaxis]])
Xnew = np.hstack([Xnew,x2])
Xnew = np.hstack([Xnew,X[:,3:5]])
Xnew = np.hstack([Xnew,x5[:,np.newaxis]])
Xnew = np.hstack([Xnew,x6,X[:,7][:,np.newaxis],x8[:,np.newaxis],X[:,9][:,np.newaxis],x10,x11,x12])
X = Xnew
X.shape
np.unique(y,return_counts=True)
t1 = y == -1.
X1 = X[t1]
y1 = y[t1]

t2 = y == 1.
X2 = X[t2]
y2 = y[t2]
t2 = np.random.choice(len(y2),len(y1),replace=False)
X2 = X2[t2]
y2 = y2[t2]
X = np.vstack([X1,X2])
y = np.hstack([y1,y2])

np.unique(y,return_counts=True)
from sklearn.utils import shuffle
X, y = shuffle(X, y)
from sklearn.preprocessing import MinMaxScaler
X = MinMaxScaler().fit_transform(X)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>def inference(X_train,y_train,X_test,y_test,method='expectation_reflection'):
    if method == 'expectation_reflection':
        h0,w = ER.fit(X_train,y_train,niter_max=100,regu=0.1)
        y_pred = ER.predict(X_test,h0,w)

    else:
        if method == 'logistic_regression':
            model = LogisticRegression(solver='liblinear')

        if method == 'naive_bayes': 
            model = GaussianNB()

        if method == 'random_forest':
            model = RandomForestClassifier(criterion = "gini", random_state = 1,
                           max_depth=3, min_samples_leaf=5,n_estimators=100)   
            
        if method == 'decision_tree':
            model = DecisionTreeClassifier()  

        model.fit(X_train, y_train)
        y_pred = model.predict(X_test)
        
    accuracy = accuracy_score(y_test,y_pred)                
                  
    return accuracy
def compare_inference(X,y,train_size):
    npred = 100
    accuracy = np.zeros((len(list_methods),npred))
    precision = np.zeros((len(list_methods),npred))
    recall = np.zeros((len(list_methods),npred))
    accuracy_train = np.zeros((len(list_methods),npred))
    for ipred in range(npred):
        X_train0,X_test,y_train0,y_test = train_test_split(X,y,test_size=0.2,random_state = ipred)

        idx_train = np.random.choice(len(y_train0),size=int(train_size*len(y)),replace=False)
        X_train,y_train = X_train0[idx_train],y_train0[idx_train]

        for i,method in enumerate(list_methods):
            accuracy[i,ipred] = inference(X_train,y_train,X_test,y_test,method)
            
    return accuracy.mean(axis=1),accuracy.std(axis=1)
list_train_size = [0.8,0.6,0.4,0.2]
list_methods=['logistic_regression','naive_bayes','random_forest','expectation_reflection']
acc = np.zeros((len(list_train_size),len(list_methods)))
acc_std = np.zeros((len(list_train_size),len(list_methods)))
for i,train_size in enumerate(list_train_size):
    acc[i,:],acc_std[i,:] = compare_inference(X,y,train_size)
    print(train_size,acc[i,:]) 
df = pd.DataFrame(acc,columns = list_methods)
df.insert(0, "train_size",list_train_size, True)
df
plt.figure(figsize=(4,3))    
plt.plot(list_train_size,acc[:,0],'k--',marker='o',mfc='none',label='Logistic Regression')
plt.plot(list_train_size,acc[:,1],'b--',marker='s',mfc='none',label='Naive Bayes')
plt.plot(list_train_size,acc[:,2],'r--',marker='^',mfc='none',label='Random Forest')
plt.plot(list_train_size,acc[:,-1],'k-',marker='o',label='Expectation Reflection')
plt.xlabel('train size')
plt.ylabel('accuracy mean')
plt.legend()
plt.figure(figsize=(4,3))    
plt.plot(list_train_size,acc_std[:,0],'k--',marker='o',mfc='none',label='Logistic Regression')
plt.plot(list_train_size,acc_std[:,1],'b--',marker='s',mfc='none',label='Naive Bayes')
plt.plot(list_train_size,acc_std[:,2],'r--',marker='^',mfc='none',label='Random Forest')
plt.plot(list_train_size,acc_std[:,-1],'k-',marker='o',label='Expectation Reflection')
plt.xlabel('train size')
plt.ylabel('accuracy standard deviation')
plt.legend()
#np.savetxt('../heart_acc.txt',acc,fmt='%f')
#np.savetxt('../heart_acc_std.txt',acc_std,fmt='%f')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup