Open In App

Heart Disease Prediction using Support Vector Machine

Last Updated : 10 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Heart disease is a significant health concern worldwide, and early detection plays a crucial role in effective treatment and prevention. Machine learning algorithms, such as Support Vector Machines (SVM), have shown promising results in predicting heart disease based on patient data.

SVM stands for Support Vector Machine are a set of supervised learning methods used for classification, regression and outliers detection. SVM can be imagined as a surface that maximizes the boundaries between various types of points of data that is represented in multi-dimensional space also known as hyperplane. It can be used for both binary classification and multi-class classification

Implementation of Heart Disease Prediction using Support Vector Machine

Here, we will use the heart disease dataset. The dataset provides the information of age, sex, cp, trestbps and other terms based on which the candidate is labeled as 0 and 1.

Importing Necessary Libraries

Python3




import numpy as np
import pandas as pd
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split


Loading the dataset

Python3




heart_data = pd.read_csv('heart.csv')
heart_data.info()


Output:

RangeIndex: 1025 entries, 0 to 1024
Data columns (total 14 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 age 1025 non-null int64
1 sex 1025 non-null int64
2 cp 1025 non-null int64
3 trestbps 1025 non-null int64
4 chol 1025 non-null int64
5 fbs 1025 non-null int64
6 restecg 1025 non-null int64
7 thalach 1025 non-null int64
8 exang 1025 non-null int64
9 oldpeak 1025 non-null float64
10 slope 1025 non-null int64
11 ca 1025 non-null int64
12 thal 1025 non-null int64
13 target 1025 non-null int64
dtypes: float64(1), int64(13)
memory usage: 112.2 KB

Now after loading the dataset we observe the target attribute present in the dataset .

Selecting the Target Variable

Python3




X = heart_data.drop(columns='target',axis=1)
Y = heart_data['target']
heart_data['target'].value_counts()


Output:

1    526
0 499
Name: target, dtype: int64

Splitting the data

Python3




X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, stratify=Y, random_state=2)


Fitting the Model

  • we have imported SVC model from sklearn.svm module and the object name of our model is clf1 and in the next line the model is trained successfully .
  • In SVC we have used kernel function to convert low dimensional feature space to high dimensional feature space to make the data present in dataset classify properly

Python3




from sklearn.svm import SVC
clf1 = SVC(kernel="linear")
clf1.fit(X_train,Y_train)


Output :

SVC
SVC(kernel='linear') .

Testing Accuracy of the model

Python3




y_pred= clf1.predict(X_test)
from sklearn.metrics import classification_report
 
# Evaluate the classification report
classification_rep = classification_report(y_pred,Y_test)
print("Classification Report:")
print(classification_rep)


Output:

Classification Report:
precision recall f1-score support
0 0.72 0.90 0.80 80
1 0.92 0.78 0.84 125
accuracy 0.82 205
macro avg 0.82 0.84 0.82 205
weighted avg 0.84 0.82 0.83 205

Defining a Prediction Function

  1. Function Definition: The function predict_heart_disease takes two arguments: model (the trained SVM model) and input_data (a tuple containing input features for a single instance).
  2. Convert Input Data to NumPy Array: The input data is converted to a NumPy array using np.asarray(input_data). This ensures that the input data is in a format compatible with the SVM model.
  3. Reshape Input Data: The NumPy array is reshaped to match the expected input shape of the SVM model. In this case, the shape is (1, -1), where -1 indicates that NumPy should infer the number of columns based on the number of elements in the input data.
  4. Make Prediction: The SVM model’s predict method is used to make a prediction on the reshaped input data. The result is stored in the prediction variable.
  5. Print Prediction Result: Based on the prediction result, the function prints either “The Person does not have Heart Disease” if prediction[0] is 0, or “The Person has Heart Disease” if prediction[0] is not 0.

Python3




def predict_heart_disease(model, input_data):
    # Change the input data to a numpy array
    input_data_as_numpy_array = np.asarray(input_data)
     
    # Reshape the numpy array as we are predicting for only one instance
    input_data_reshaped = input_data_as_numpy_array.reshape(1, -1)
     
    # Make the prediction
    prediction = model.predict(input_data_reshaped)
     
    # Print the prediction result
    if prediction[0] == 0:
        print('The Person does not have Heart Disease')
    else:
        print('The Person has Heart Disease')


Prediction on Random Data

Python3




input_data = (62, 0, 0, 140, 268, 0, 0, 160, 0, 3.6, 0, 2, 2)
predict_heart_disease(clf1, input_data)


Output:

The Person does not have Heart Disease


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads