0% found this document useful (0 votes)
13 views3 pages

Heart Disease Classification Full-1

The document outlines a process for classifying heart disease using a simulated dataset. It includes steps for loading the dataset, preprocessing data (encoding, splitting, and scaling), implementing three machine learning models (Logistic Regression, Random Forest, and SVM), and evaluating their accuracy. All models achieved an accuracy of 1.0 on the test set.

Uploaded by

muha2000had
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Heart Disease Classification Full-1

The document outlines a process for classifying heart disease using a simulated dataset. It includes steps for loading the dataset, preprocessing data (encoding, splitting, and scaling), implementing three machine learning models (Logistic Regression, Random Forest, and SVM), and evaluating their accuracy. All models achieved an accuracy of 1.0 on the test set.

Uploaded by

muha2000had
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Heart Disease Classification - Full Dataset

Step 1: Load the Dataset


import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import classification_report, accuracy_score

# Simulate full dataset


data_full = pd.DataFrame([
[64, 1, 66, 160, 83, 160, 1.8, 'negative'],
[21, 1, 94, 98, 46, 296, 6.75, 'positive'],
[55, 1, 64, 160, 77, 270, 1.99, 'negative'],
[64, 1, 70, 120, 55, 270, 13.87, 'positive'],
[55, 1, 64, 112, 65, 300, 1.08, 'negative'],
[58, 0, 61, 112, 58, 87, 1.83, 'negative'],
[32, 0, 40, 179, 68, 102, 0.71, 'negative'],
[63, 1, 60, 214, 82, 87, 2.37, 'positive'],
[44, 0, 60, 154, 81, 135, 2.35, 'negative'],
[67, 1, 61, 160, 95, 100, 2.84, 'negative'],
[64, 1, 66, 160, 83, 160, 1.8, 'negative'],
[21, 1, 94, 98, 46, 296, 6.75, 'positive']
] * 100) # Simulate large dataset

data_full.columns = ['age', 'gender', 'impulse_pressure_high',


'pressure_low', 'glucose', 'kcm', 'troponin', 'class']
data_full.head()

{"summary":"{\n \"name\": \"data_full\",\n \"rows\": 1200,\n


\"fields\": [\n {\n \"column\": \"age\",\n
\"properties\": {\n \"dtype\": \"number\",\n \"std\":
16,\n \"min\": 21,\n \"max\": 67,\n
\"num_unique_values\": 8,\n \"samples\": [\n 21,\n
63,\n 64\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n },\n {\n \"column\":
\"gender\",\n \"properties\": {\n \"dtype\": \"number\",\n
\"std\": 0,\n \"min\": 0,\n \"max\": 1,\n
\"num_unique_values\": 2,\n \"samples\": [\n 0,\n
1\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n },\n {\n \"column\":
\"impulse_pressure_high\",\n \"properties\": {\n
\"dtype\": \"number\",\n \"std\": 14,\n \"min\": 40,\n
\"max\": 94,\n \"num_unique_values\": 7,\n \"samples\":
[\n 66,\n 94\n ],\n \"semantic_type\":
\"\",\n \"description\": \"\"\n }\n },\n {\n
\"column\": \"pressure_low\",\n \"properties\": {\n
\"dtype\": \"number\",\n \"std\": 34,\n \"min\": 98,\n
\"max\": 214,\n \"num_unique_values\": 7,\n \"samples\":
[\n 160,\n 98\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n },\n {\n \"column\": \"glucose\",\n \"properties\":
{\n \"dtype\": \"number\",\n \"std\": 15,\n
\"min\": 46,\n \"max\": 95,\n \"num_unique_values\":
10,\n \"samples\": [\n 81,\n 46\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n },\n {\n \"column\": \"kcm\",\n \"properties\": {\n
\"dtype\": \"number\",\n \"std\": 86,\n \"min\": 87,\n
\"max\": 300,\n \"num_unique_values\": 8,\n \"samples\":
[\n 296,\n 102\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n },\n {\n \"column\": \"troponin\",\n \"properties\":
{\n \"dtype\": \"number\",\n \"std\":
3.6047967147979576,\n \"min\": 0.71,\n \"max\": 13.87,\n
\"num_unique_values\": 10,\n \"samples\": [\n 2.35,\n
6.75\n ],\n \"semantic_type\": \"\",\n
\"description\": \"\"\n }\n },\n {\n \"column\":
\"class\",\n \"properties\": {\n \"dtype\": \"category\",\
n \"num_unique_values\": 2,\n \"samples\": [\n
\"positive\",\n \"negative\"\n ],\n
\"semantic_type\": \"\",\n \"description\": \"\"\n }\
n }\n ]\n}","type":"dataframe","variable_name":"data_full"}

Step 2: Preprocessing Steps


# Preprocessing Steps
# 1. Encode target variable
encoder = LabelEncoder()
data_full['class_encoded'] = encoder.fit_transform(data_full['class'])

# 2. Split the data


X = data_full.drop(columns=['class', 'class_encoded'])
y = data_full['class_encoded']
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=42, stratify=y)

# 3. Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
Step 3: Model Implementation
# Logistic Regression
logistic_model = LogisticRegression()
logistic_model.fit(X_train_scaled, y_train)
logistic_preds = logistic_model.predict(X_test_scaled)
logistic_accuracy = accuracy_score(y_test, logistic_preds)

# Random Forest
rf_model = RandomForestClassifier(random_state=42)
rf_model.fit(X_train, y_train)
rf_preds = rf_model.predict(X_test)
rf_accuracy = accuracy_score(y_test, rf_preds)

# Support Vector Machine


svm_model = SVC(probability=True)
svm_model.fit(X_train_scaled, y_train)
svm_preds = svm_model.predict(X_test_scaled)
svm_accuracy = accuracy_score(y_test, svm_preds)

Step 4: Model Evaluation


print('Logistic Regression Accuracy:', logistic_accuracy)
print('Random Forest Accuracy:', rf_accuracy)
print('SVM Accuracy:', svm_accuracy)

Logistic Regression Accuracy: 1.0


Random Forest Accuracy: 1.0
SVM Accuracy: 1.0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy