Heart Disease Prediction using SVM with Feature Scaling and Visualization
import pandas as pd
import [Link] as plt
from [Link] import SVC
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import Pipeline
from [Link] import accuracy_score, confusion_matrix, classification_report
# Dataset
data = {
'Age': [29, 45, 34, 50, 63, 39, 58, 41, 67, 52, 48, 36],
'Blood_Pressure': [120, 140, 128, 150, 165, 132, 158, 135, 170, 148, 142, 130],
'Cholesterol': [180, 240, 195, 260, 300, 210, 280, 225, 320, 255, 245, 205],
'Heart_Disease': [0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0]
df = [Link](data)
# Features and target
X = df[['Age', 'Blood_Pressure', 'Cholesterol']]
y = df['Heart_Disease']
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42
# Pipeline (Scaling + SVM)
model = Pipeline([
('scaler', StandardScaler()),
('svm', SVC(kernel='rbf'))
])
# Train model
[Link](X_train, y_train)
# Predictions
y_pred = [Link](X_test)
# Evaluation
print("Heart Disease Prediction using SVM")
print("---------------------------------")
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
# Real-time prediction (NO WARNING)
new_patient = [Link](
[[54, 150, 265]],
columns=['Age', 'Blood_Pressure', 'Cholesterol']
result = [Link](new_patient)
print("---------------------------------")
print("New Patient Prediction:",
"Heart Disease Risk ⚠️" if result[0] == 1 else "No Heart Disease ✅")
# ---------------- Visualization ----------------
[Link]()
[Link](df['Age'], df['Cholesterol'], c=df['Heart_Disease'])
[Link]("Age")
[Link]("Cholesterol")
[Link]("Heart Disease Classification using SVM")
[Link]()
Heart Disease Prediction using Linear and Non-Linear Support Vector Machine (SVM)
import pandas as pd
import [Link] as plt
from [Link] import SVC
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
# Step 1: Create dataset
data = {
'Age': [25, 45, 35, 50, 60, 30, 55, 40, 65, 48],
'Blood_Pressure': [120, 140, 130, 150, 160, 125, 155, 135, 165, 145],
'Cholesterol': [180, 240, 200, 260, 300, 190, 280, 220, 310, 250],
'Disease': [0, 1, 0, 1, 1, 0, 1, 0, 1, 1]
df = [Link](data)
# Step 2: Features and target
X = df[['Age', 'Blood_Pressure', 'Cholesterol']]
y = df['Disease']
# Step 3: Train-Test Split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=1
# ---------------- LINEAR SVM ----------------
linear_svm = SVC(kernel='linear')
linear_svm.fit(X_train, y_train)
linear_pred = linear_svm.predict(X_test)
linear_accuracy = accuracy_score(y_test, linear_pred)
# ---------------- NON-LINEAR SVM (RBF) ----------------
rbf_svm = SVC(kernel='rbf')
rbf_svm.fit(X_train, y_train)
rbf_pred = rbf_svm.predict(X_test)
rbf_accuracy = accuracy_score(y_test, rbf_pred)
# Step 4: Results
print("Heart Disease Prediction using SVM")
print("----------------------------------")
print("Linear SVM Accuracy:", linear_accuracy)
print("Non-Linear SVM (RBF) Accuracy:", rbf_accuracy)
# Step 5: Real-time Prediction (using better model)
new_patient = [Link](
[[52, 148, 270]],
columns=['Age', 'Blood_Pressure', 'Cholesterol']
linear_result = linear_svm.predict(new_patient)
rbf_result = rbf_svm.predict(new_patient)
print("----------------------------------")
print("Linear SVM Prediction:",
"Disease " if linear_result[0] == 1 else "No Disease ")
print("RBF SVM Prediction:",
"Disease " if rbf_result[0] == 1 else "No Disease ")
# ---------------- Visualization ----------------
[Link]()
[Link](df['Age'], df['Cholesterol'], c=df['Disease'])
[Link]("Age")
[Link]("Cholesterol")
[Link]("Heart Disease Data Distribution")
[Link]()
Student Pass Prediction using Support Vector Machine (SVM Classification)
import pandas as pd
import numpy as np
import [Link] as plt
from [Link] import SVC
# Step 1: Create dataset (ONLY 2 FEATURES for clear hyperplane)
data = {
'Study_Hours': [1, 2, 3, 4, 5, 6, 7, 8],
'Attendance': [40, 50, 55, 65, 70, 80, 85, 90],
'Result': [0, 0, 0, 1, 1, 1, 1, 1] # 0 = Fail, 1 = Pass
df = [Link](data)
# Step 2: Features and target
X = df[['Study_Hours', 'Attendance']]
y = df['Result']
# Step 3: Train Linear SVM
svm_model = SVC(kernel='linear')
svm_model.fit(X, y)
# Step 4: Plot data points
[Link]()
[Link](
df['Study_Hours'],
df['Attendance'],
c=df['Result']
# Step 5: Plot hyperplane
w = svm_model.coef_[0]
b = svm_model.intercept_[0]
x_points = [Link](X['Study_Hours'].min(), X['Study_Hours'].max())
y_points = -(w[0] / w[1]) * x_points - b / w[1]
[Link](x_points, y_points)
# Step 6: Labels and title
[Link]("Study Hours")
[Link]("Attendance (%)")
[Link]("Linear SVM Hyperplane for Student Pass Prediction")
[Link]()