Wednesday, March 10, 2021

Support Vector Machine in Python

At first download the Heart Disease Dataset from Kaggle:  https://www.kaggle.com/ronitf/heart-disease-uci.  Following, you should put the dataset in google drive. Then Using Google Colab you can run the following Support Vector Machine code:


!pip install -U -q PyDrive 

from pydrive.auth import GoogleAuth 
from pydrive.drive import GoogleDrive 
from google.colab import auth 
from oauth2client.client import GoogleCredentials 


# Authenticate and create the PyDrive client. 
auth.authenticate_user() 
gauth = GoogleAuth() 
gauth.credentials = GoogleCredentials.get_application_default() 
drive = GoogleDrive(gauth)
link =
'https://drive.google.com/file/d/1OglM8GvGIiVMMUXAchljXc86RtOq9K02
/view?usp=sharing'

import pandas as pd 
import numpy as np
# to get the id part of the file 
id = link.split("/")[-2

downloaded = drive.CreateFile({'id':id}) 
downloaded.GetContentFile('heart.csv'

df = pd.read_csv('heart.csv'
X = df.drop('target', axis=1)
y = df['target']
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y,
 test_size = 0.30)

from sklearn.svm import SVC
svclassifier = SVC(kernel='linear')
svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)
from sklearn.metrics import confusion_matrix, classification_report
print(confusion_matrix(y_test,y_pred))
print(classification_report(y_test,y_pred))
from sklearn import metrics
print("Accuracy:",
metrics.accuracy_score(y_test, y_pred))
print("Precision:",
metrics.precision_score(y_test, y_pred))
print("Recall:",
metrics.recall_score(y_test, y_pred))
print("F1 Score:",metrics.f1_score(y_test, y_pred))

print('Mean Absolute Error:',
 metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:'
metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:'
np.sqrt(metrics.mean_squared_error(y_test, y_pred)))



Output:



No comments:

Post a Comment

How to Write Summary of a Research Paper

Paper Summary should contain the following points: What problem author’s solved? What are the motivations for that problem? Why is it import...