|
|
|
|
|
"""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 |
|
|
|
|
|
|
|
|
|
|
|
data = datasets.load_breast_cancer() |
|
|
|
|
|
X = data.data |
|
|
|
|
|
y = data.target |
|
|
|
|
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
K_dot = X_small @ X_small.T |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rbf_kernel(X1, X2, gamma): |
|
|
|
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
features_scaled = scaler.transform(features) |
|
|
|
|
|
|
|
|
prediction_linear = linear_svm.predict(features_scaled)[0] |
|
|
prediction_rbf = rbf_svm.predict(features_scaled)[0] |
|
|
|
|
|
|
|
|
label_map = {0: "Malignant", 1: "Benign"} |
|
|
output_linear = label_map[prediction_linear] |
|
|
output_rbf = label_map[prediction_rbf] |
|
|
|
|
|
return output_linear, output_rbf |
|
|
|
|
|
|
|
|
inputs = [ |
|
|
gr.Number(label=f"Feature {i+1}") for i in range(30) |
|
|
] |
|
|
|
|
|
|
|
|
outputs = [ |
|
|
gr.Textbox(label="Linear SVM Prediction"), |
|
|
gr.Textbox(label="RBF SVM Prediction") |
|
|
] |
|
|
|
|
|
|
|
|
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() |