The following SVM is used to perform the same heart disease classification task and the final classficiation accuracies.
Step-by-step explanation:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
dataset = pd.read_csv('heart_disease. csv')
dataset.head(2)
# creating target variables
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y=le.fit_transform(dataset['Car Acceptability'])
#standardizing the input feature
from sklearn.preprocessing import StandardScaler
sc= StandardScaler()
X= sc.fit_transform(all_input_columns)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, output_category, test_size=0.3)
# K fold validation
def build_classifier():
classifier = Sequential()
#First Hidden Layer
classifier.add(Dense(8, activation='relu', kernel_initializer='random_uniform', input_dim=13))
#Second Hidden Layer
classifier.add(Dense(8, activation='relu', kernel_initializer='random_uniform'))
#Output Layer
classifier.add(Dense(4, activation='softmax', kernel_initializer='random_uniform'))
#Compiling the neural network
classifier.compile(optimizer ='adam',loss='categorical_crossentropy', metrics =['accuracy'])
return classifier
We pass build_classifier function to the build_fn argument while constructing the KerasClassifier class. Batch_size is 10 and we run 150 epochs
classifier = KerasClassifier(build_fn=build_classifier, batch_size=10, nb_epoch=150)
As we have used the KerasClassifier to warp the model, we can use scikit-learn. Once we do that then we can use neural network like any other scikit algorithms like Random forest and perform 10 fold cross-validation.
accuracies = cross_val_score(estimator=classifier, X= X, y=output_category,cv=10, n_jobs=-1)
note: do with each fold by replacing 10 with other