File size: 4,433 Bytes
e6b3df4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- 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()