nafees369 commited on
Commit
e6b3df4
verified
1 Parent(s): 17a06b9

Upload App.py

Browse files
Files changed (1) hide show
  1. App.py +172 -0
App.py ADDED
@@ -0,0 +1,172 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Workshop-Linear kernel.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1PlQo6At9kkwbacgmJdJ7FuN93WVLFoEY
8
+ """
9
+
10
+ import numpy as np
11
+
12
+ from sklearn import datasets
13
+
14
+ from sklearn.model_selection import train_test_split
15
+
16
+ from sklearn.preprocessing import StandardScaler
17
+
18
+ from sklearn.svm import SVC
19
+
20
+ from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
21
+
22
+ # Breast cancer dataset: target = malignant (0) / benign (1)
23
+
24
+ data = datasets.load_breast_cancer()
25
+
26
+ X = data.data # features
27
+
28
+ y = data.target # labels
29
+
30
+
31
+
32
+ print("Feature matrix shape:", X.shape)
33
+
34
+ print("Labels shape:", y.shape)
35
+
36
+ X_train, X_test, y_train, y_test = train_test_split(
37
+
38
+ X, y, test_size=0.3, random_state=42, stratify=y
39
+
40
+ )
41
+
42
+ print("Train size:", X_train.shape[0], "Test size:", X_test.shape[0])
43
+
44
+ scaler = StandardScaler()
45
+
46
+ X_train_scaled = scaler.fit_transform(X_train)
47
+
48
+ X_test_scaled = scaler.transform(X_test)
49
+
50
+ linear_svm = SVC(kernel='linear', C=1.0, random_state=42)
51
+
52
+ linear_svm.fit(X_train_scaled, y_train)
53
+
54
+
55
+
56
+ y_pred_linear = linear_svm.predict(X_test_scaled)
57
+
58
+ acc_linear = accuracy_score(y_test, y_pred_linear)
59
+
60
+
61
+
62
+ print("\n=== Linear Kernel SVM (Dot Product) ===")
63
+
64
+ print("Accuracy:", acc_linear)
65
+
66
+ print("Confusion matrix:\n", confusion_matrix(y_test, y_pred_linear))
67
+
68
+ print("Classification report:\n", classification_report(y_test, y_pred_linear))
69
+
70
+ rbf_svm = SVC(kernel='rbf', C=1.0, gamma='scale', random_state=42)
71
+
72
+ rbf_svm.fit(X_train_scaled, y_train)
73
+
74
+
75
+
76
+ y_pred_rbf = rbf_svm.predict(X_test_scaled)
77
+
78
+ acc_rbf = accuracy_score(y_test, y_pred_rbf)
79
+
80
+
81
+
82
+ print("\n=== RBF Kernel SVM ===")
83
+
84
+ print("Accuracy:", acc_rbf)
85
+
86
+ print("Confusion matrix:\n", confusion_matrix(y_test, y_pred_rbf))
87
+
88
+ print("Classification report:\n", classification_report(y_test, y_pred_rbf))
89
+
90
+ X_small = X_train_scaled[:5]
91
+
92
+
93
+
94
+ # Dot product kernel matrix: K_ij = x_i 路 x_j
95
+
96
+ K_dot = X_small @ X_small.T
97
+
98
+
99
+
100
+ # RBF kernel matrix by hand
101
+
102
+ def rbf_kernel(X1, X2, gamma):
103
+
104
+ # ||x - x'||^2 = (x^2 summed) + (x'^2 summed) - 2 x路x'
105
+
106
+ X1_sq = np.sum(X1 ** 2, axis=1).reshape(-1, 1)
107
+
108
+ X2_sq = np.sum(X2 ** 2, axis=1).reshape(1, -1)
109
+
110
+ sq_dist = X1_sq + X2_sq - 2 * X1 @ X2.T
111
+
112
+ return np.exp(-gamma * sq_dist)
113
+
114
+
115
+
116
+ gamma_value = 0.1
117
+
118
+ K_rbf = rbf_kernel(X_small, X_small, gamma=gamma_value)
119
+
120
+
121
+
122
+ print("\n=== Example Kernel Matrices on 5 Training Samples ===")
123
+
124
+ print("\nDot-product kernel matrix (linear):\n", K_dot)
125
+
126
+ print("\nRBF kernel matrix (gamma = 0.1):\n", K_rbf)
127
+
128
+ import gradio as gr
129
+
130
+ 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):
131
+ # Collect all features into a numpy array
132
+ features = np.array([
133
+ feature1, feature2, feature3, feature4, feature5, feature6, feature7, feature8, feature9, feature10,
134
+ feature11, feature12, feature13, feature14, feature15, feature16, feature17, feature18, feature19, feature20,
135
+ feature21, feature22, feature23, feature24, feature25, feature26, feature27, feature28, feature29, feature30
136
+ ]).reshape(1, -1)
137
+
138
+ # Scale the input features using the pre-trained scaler
139
+ features_scaled = scaler.transform(features)
140
+
141
+ # Get predictions from both models
142
+ prediction_linear = linear_svm.predict(features_scaled)[0]
143
+ prediction_rbf = rbf_svm.predict(features_scaled)[0]
144
+
145
+ # Map predictions to human-readable labels
146
+ label_map = {0: "Malignant", 1: "Benign"}
147
+ output_linear = label_map[prediction_linear]
148
+ output_rbf = label_map[prediction_rbf]
149
+
150
+ return output_linear, output_rbf
151
+
152
+ # Create Gradio input components for 30 features
153
+ inputs = [
154
+ gr.Number(label=f"Feature {i+1}") for i in range(30)
155
+ ]
156
+
157
+ # Create Gradio output components for both models' predictions
158
+ outputs = [
159
+ gr.Textbox(label="Linear SVM Prediction"),
160
+ gr.Textbox(label="RBF SVM Prediction")
161
+ ]
162
+
163
+ # Create and launch the Gradio interface
164
+ iface = gr.Interface(
165
+ fn=predict_cancer,
166
+ inputs=inputs,
167
+ outputs=outputs,
168
+ title="Breast Cancer Prediction using SVM",
169
+ description="Enter 30 features to predict if a tumor is Malignant (0) or Benign (1) using Linear and RBF Kernel SVMs."
170
+ )
171
+
172
+ iface.launch()