nafees369's picture
Rename App.py to app.py
8903687 verified
# -*- coding: utf-8 -*-
"""Workshop-Linear kernel.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1PlQo6At9kkwbacgmJdJ7FuN93WVLFoEY
"""
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# Breast cancer dataset: target = malignant (0) / benign (1)
data = datasets.load_breast_cancer()
X = data.data # features
y = data.target # labels
print("Feature matrix shape:", X.shape)
print("Labels shape:", y.shape)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=42, stratify=y
)
print("Train size:", X_train.shape[0], "Test size:", X_test.shape[0])
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
linear_svm = SVC(kernel='linear', C=1.0, random_state=42)
linear_svm.fit(X_train_scaled, y_train)
y_pred_linear = linear_svm.predict(X_test_scaled)
acc_linear = accuracy_score(y_test, y_pred_linear)
print("\n=== Linear Kernel SVM (Dot Product) ===")
print("Accuracy:", acc_linear)
print("Confusion matrix:\n", confusion_matrix(y_test, y_pred_linear))
print("Classification report:\n", classification_report(y_test, y_pred_linear))
rbf_svm = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)
rbf_svm.fit(X_train_scaled, y_train)
y_pred_rbf = rbf_svm.predict(X_test_scaled)
acc_rbf = accuracy_score(y_test, y_pred_rbf)
print("\n=== RBF Kernel SVM ===")
print("Accuracy:", acc_rbf)
print("Confusion matrix:\n", confusion_matrix(y_test, y_pred_rbf))
print("Classification report:\n", classification_report(y_test, y_pred_rbf))
X_small = X_train_scaled[:5]
# Dot product kernel matrix: K_ij = x_i 路 x_j
K_dot = X_small @ X_small.T
# RBF kernel matrix by hand
def rbf_kernel(X1, X2, gamma):
# ||x - x'||^2 = (x^2 summed) + (x'^2 summed) - 2 x路x'
X1_sq = np.sum(X1 ** 2, axis=1).reshape(-1, 1)
X2_sq = np.sum(X2 ** 2, axis=1).reshape(1, -1)
sq_dist = X1_sq + X2_sq - 2 * X1 @ X2.T
return np.exp(-gamma * sq_dist)
gamma_value = 0.1
K_rbf = rbf_kernel(X_small, X_small, gamma=gamma_value)
print("\n=== Example Kernel Matrices on 5 Training Samples ===")
print("\nDot-product kernel matrix (linear):\n", K_dot)
print("\nRBF kernel matrix (gamma = 0.1):\n", K_rbf)
import gradio as gr
def predict_cancer(feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12, feature13, feature14, feature15, feature16, feature17, feature18, feature19, feature20, feature21, feature22, feature23, feature24, feature25, feature26, feature27, feature28, feature29, feature30):
# Collect all features into a numpy array
features = np.array([
feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10,
feature11, feature12, feature13, feature14, feature15, feature16, feature17, feature18, feature19, feature20,
feature21, feature22, feature23, feature24, feature25, feature26, feature27, feature28, feature29, feature30
]).reshape(1, -1)
# Scale the input features using the pre-trained scaler
features_scaled = scaler.transform(features)
# Get predictions from both models
prediction_linear = linear_svm.predict(features_scaled)[0]
prediction_rbf = rbf_svm.predict(features_scaled)[0]
# Map predictions to human-readable labels
label_map = {0: "Malignant", 1: "Benign"}
output_linear = label_map[prediction_linear]
output_rbf = label_map[prediction_rbf]
return output_linear, output_rbf
# Create Gradio input components for 30 features
inputs = [
gr.Number(label=f"Feature {i+1}") for i in range(30)
]
# Create Gradio output components for both models' predictions
outputs = [
gr.Textbox(label="Linear SVM Prediction"),
gr.Textbox(label="RBF SVM Prediction")
]
# Create and launch the Gradio interface
iface = gr.Interface(
fn=predict_cancer,
inputs=inputs,
outputs=outputs,
title="Breast Cancer Prediction using SVM",
description="Enter 30 features to predict if a tumor is Malignant (0) or Benign (1) using Linear and RBF Kernel SVMs."
)
iface.launch()