-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_tipe.py
More file actions
713 lines (555 loc) · 22.9 KB
/
Copy pathpython_tipe.py
File metadata and controls
713 lines (555 loc) · 22.9 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
"""
#code no 1
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
class LinearSVMUsingSoftMargin:
def __init__(self, C=1.0):
self._support_vectors = None
self.C = C
self.beta = None
self.b = None
self.X = None
self.y = None
# n is the number of data points
self.n = 0
# d is the number of dimensions
self.d = 0
def __decision_function(self, X):
return X.dot(self.beta) + self.b
def __cost(self, margin):
return (1 / 2) * self.beta.dot(self.beta) + self.C * np.sum(np.maximum(0, 1 - margin))
def __margin(self, X, y):
return y * self.__decision_function(X)
def fit(self, X, y, lr=1e-3, epochs=500):
# Initialize Beta and b
self.n, self.d = X.shape
self.beta = np.random.randn(self.d)
self.b = 0
# Required only for plotting
self.X = X
self.y = y
loss_array = []
for _ in range(epochs):
margin = self.__margin(X, y)
loss = self.__cost(margin)
loss_array.append(loss)
misclassified_pts_idx = np.where(margin < 1)[0]
d_beta = self.beta - self.C * y[misclassified_pts_idx].dot(X[misclassified_pts_idx])
self.beta = self.beta - lr * d_beta
d_b = - self.C * np.sum(y[misclassified_pts_idx])
self.b = self.b - lr * d_b
self._support_vectors = np.where(self.__margin(X, y) <= 1)[0]
def predict(self, X):
return np.sign(self.__decision_function(X))
def score(self, X, y):
P = self.predict(X)
return np.mean(y == P)
def plot_decision_boundary(self):
plt.scatter(self.X[:, 0], self.X[:, 1], c=self.y, s=50, cmap=plt.cm.Paired, alpha=.7)
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = self.__decision_function(xy).reshape(XX.shape)
# plot decision boundary and margins
ax.contour(XX, YY, Z, colors=['r', 'b', 'r'], levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'], linewidths=[2.0, 2.0, 2.0])
# highlight the support vectors
ax.scatter(self.X[:, 0][self._support_vectors], self.X[:, 1][self._support_vectors], s=100,
linewidth=1, facecolors='none', edgecolors='k')
plt.show()
def load_data(cols):
iris = sns.load_dataset("iris")
iris = iris.tail(100)
le = preprocessing.LabelEncoder()
y = le.fit_transform(iris["species"])
X = iris.drop(["species"], axis=1)
if len(cols) > 0:
X = X[cols]
return X.values, y
if __name__ == '__main__':
# make sure the targets are (-1, +1)
cols = ["petal_length", "petal_width"]
X, y = load_data(cols)
y[y == 0] = -1
# scale the data
scaler = StandardScaler()
X = scaler.fit_transform(X)
# now we'll use our custom implementation
model = LinearSVMUsingSoftMargin(C=15.0)
model.fit(X, y)
print("train score:", model.score(X, y))
model.plot_decision_boundary()
# code no2
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
class LinearSVMUsingSoftMargin:
def __init__(self, C=1.0):
self._support_vectors = None
self.C = C
self.beta = None
self.b = None
self.X = None
self.y = None
# n is the number of data points
self.n = 0
# d is the number of dimensions
self.d = 0
def __decision_function(self, X):
return X.dot(self.beta) + self.b
def __cost(self, margin):
return (1 / 2) * self.beta.dot(self.beta) + self.C * np.sum(np.maximum(0, 1 - margin))
def __margin(self, X, y):
return y * self.__decision_function(X)
def fit(self, X, y, lr=1e-3, epochs=500):
# Initialize Beta and b
self.n, self.d = X.shape
self.beta = np.random.randn(self.d)
self.b = 0
# Required only for plotting
self.X = X
self.y = y
loss_array = []
for _ in range(epochs):
margin = self.__margin(X, y)
loss = self.__cost(margin)
loss_array.append(loss)
misclassified_pts_idx = np.where(margin < 1)[0]
d_beta = self.beta - self.C * y[misclassified_pts_idx].dot(X[misclassified_pts_idx])
self.beta = self.beta - lr * d_beta
d_b = - self.C * np.sum(y[misclassified_pts_idx])
self.b = self.b - lr * d_b
self._support_vectors = np.where(self.__margin(X, y) <= 1)[0]
def predict(self, X):
return np.sign(self.__decision_function(X))
def score(self, X, y):
P = self.predict(X)
return np.mean(y == P)
def plot_decision_boundary(self):
if self.X.shape[1] != 2:
raise ValueError("Plotting is only supported for 2D data.")
plt.scatter(self.X[:, 0], self.X[:, 1], c=self.y, s=50, cmap=plt.cm.Paired, alpha=.7)
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = self.__decision_function(xy).reshape(XX.shape)
# plot decision boundary and margins
ax.contour(XX, YY, Z, colors=['r', 'b', 'r'], levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'], linewidths=[2.0, 2.0, 2.0])
# highlight the support vectors
ax.scatter(self.X[:, 0][self._support_vectors], self.X[:, 1][self._support_vectors], s=100,
linewidth=1, facecolors='none', edgecolors='k')
plt.show()
def load_data(file_path, target_column, feature_columns, delimiter):
data = pd.read_csv(file_path, delimiter=delimiter, header=None)
# Assuming the dataset does not have headers and we need to assign them manually
data.columns = ["age", "sex", "cp", "restbp", "chol", "fbs", "restecg", "thalach", "exang",
"oldpeak", "slope", "ca", "thal", "hd"]
le = preprocessing.LabelEncoder()
y = le.fit_transform(data[target_column])
X = data[feature_columns]
return X.values, y
if __name__ == '__main__':
# Les colonnes de caractéristiques à utiliser pour le tracé
feature_columns = ["age", "thalach"] # Choisissez deux colonnes pour la visualisation
target_column = "hd"
# Chemin du fichier .data
file_path = "C:/Users/Lenovo/Downloads/processed.cleveland.data" # Chemin complet ou chemin relatif
# Délimiteur utilisé dans le fichier .data, souvent une virgule ou un espace
delimiter = ',' # ou ' '
X, y = load_data(file_path, target_column, feature_columns, delimiter)
y[y == 0] = -1
# scale the data
scaler = StandardScaler()
X = scaler.fit_transform(X)
# now we'll use our custom implementation
model = LinearSVMUsingSoftMargin(C=15.0)
model.fit(X, y)
print("train score:", model.score(X, y))
# Tracer la frontière de décision
model.plot_decision_boundary()
"""
"""
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt
class SVMDualProblem:
def __init__(self, C=1.0, kernel='rbf', sigma=0.1, degree=2):
self.C = C
if kernel == 'poly':
self.kernel = self._polynomial_kernel
self.c = 1
self.degree = degree
else:
self.kernel = self._rbf_kernel
self.sigma = sigma
self.X = None
self.y = None
self.alpha = None
self.b = 0
self.ones = None
def _rbf_kernel(self, X1, X2):
return np.exp(-(1 / self.sigma ** 2) * np.linalg.norm(X1[:, np.newaxis] - X2[np.newaxis, :], axis=2) ** 2)
def _polynomial_kernel(self, X1, X2):
return (self.c + X1.dot(X2.T)) ** self.degree
def fit(self, X, y, lr=1e-3, epochs=500):
self.X = X
self.y = y
# (500,)
self.alpha = np.random.random(X.shape[0])
self.b = 0
# (500,)
self.ones = np.ones(X.shape[0])
# (500,500) = (500,500) * (500,500)
y_iy_jk_ij = np.outer(y, y) * self.kernel(X, X)
losses = []
for _ in range(epochs):
# (500,) = (500,) (500,500).(500,)=(500,)
gradient = self.ones - y_iy_jk_ij.dot(self.alpha)
# Same code
# gradient = self.ones - np.sum(y_iy_jk_ij * self.alpha)
self.alpha = self.alpha + lr * gradient
self.alpha[self.alpha > self.C] = self.C
self.alpha[self.alpha < 0] = 0
# (500,500) (500,500)
loss = np.sum(self.alpha) - 0.5 * np.sum(np.outer(self.alpha, self.alpha) * y_iy_jk_ij)
losses.append(loss)
index = np.where((self.alpha) > 0 & (self.alpha < self.C))[0]
# (m,)= (m,) (n,).(n,m)= (m,)
b_i = y[index] - (self.alpha * y).dot(self.kernel(X, X[index]))
# Alternative code
# b_i = y[index] - np.sum((self.alpha * y).reshape(-1, 1)*self.kernel(X, X[index]), axis=0)
self.b = np.mean(b_i)
plt.plot(losses)
plt.title("loss per epochs")
plt.show()
def _decision_function(self, X):
return (self.alpha * self.y).dot(self.kernel(self.X, X)) + self.b
def predict(self, X):
return np.sign(self._decision_function(X))
def score(self, X, y):
y_hat = self.predict(X)
return np.mean(y == y_hat)
def plot_decision_boundary(self):
plt.scatter(self.X[:, 0], self.X[:, 1], c=self.y, s=50, cmap=plt.cm.Paired, alpha=.5)
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = self._decision_function(xy).reshape(XX.shape)
# plot decision boundary and margins
ax.contour(XX, YY, Z, colors=['b', 'g', 'r'], levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'], linewidths=[2.0, 2.0, 2.0])
# highlight the support vectors
ax.scatter(self.X[:, 0][self.alpha > 0.], self.X[:, 1][self.alpha > 0.], s=50,
linewidth=1, facecolors='none', edgecolors='k')
plt.show()
class SampleData:
def get_moon(self, n_samples, noise=0.05):
noisy_moons = datasets.make_moons(n_samples=n_samples, noise=noise, random_state=6)
return noisy_moons[0], noisy_moons[1]
def get_donut(self, n_samples, noise=0.05, factor=0.5):
noisy_circles = datasets.make_circles(n_samples=n_samples, factor=factor, noise=noise)
return noisy_circles[0], noisy_circles[1]
def plot(self, X, y):
ax = plt.gca()
ax.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
plt.show()
if __name__ == '__main__':
sample = SampleData()
X, y = sample.get_donut(n_samples=500, noise=0.08)
y[y == 0] = -1
svm = SVMDualProblem(C=1.0, kernel='poly', degree=2)
svm.fit(X, y, lr=1e-3)
print("train score:", svm.score(X, y))
svm.plot_decision_boundary()
X, y = sample.get_moon(n_samples=400, noise=0.1)
y[y == 0] = -1
svm = SVMDualProblem(C=1.0, kernel='rbf', sigma=0.5)
svm.fit(X, y, lr=1e-2)
print("train score:", svm.score(X, y))
svm.plot_decision_boundary()
"""
"""
#code 4
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
class SVMDualProblem:
def __init__(self, C=1.0, kernel='rbf', sigma=0.1, degree=2):
self.C = C
if kernel == 'poly':
self.kernel = self._polynomial_kernel
self.c = 1
self.degree = degree
else:
self.kernel = self._rbf_kernel
self.sigma = sigma
self.X = None
self.y = None
self.alpha = None
self.b = 0
self.ones = None
def _rbf_kernel(self, X1, X2):
return np.exp(-(1 / self.sigma ** 2) * np.linalg.norm(X1[:, np.newaxis] - X2[np.newaxis, :], axis=2) ** 2)
def _polynomial_kernel(self, X1, X2):
return (self.c + X1.dot(X2.T)) ** self.degree
def fit(self, X, y, lr=1e-3, epochs=500):
self.X = X
self.y = y
self.alpha = np.random.random(X.shape[0])
self.b = 0
self.ones = np.ones(X.shape[0])
y_iy_jk_ij = np.outer(y, y) * self.kernel(X, X)
losses = []
for _ in range(epochs):
gradient = self.ones - y_iy_jk_ij.dot(self.alpha)
self.alpha = self.alpha + lr * gradient
self.alpha[self.alpha > self.C] = self.C
self.alpha[self.alpha < 0] = 0
loss = np.sum(self.alpha) - 0.5 * np.sum(np.outer(self.alpha, self.alpha) * y_iy_jk_ij)
losses.append(loss)
index = np.where((self.alpha) > 0 & (self.alpha < self.C))[0]
b_i = y[index] - (self.alpha * y).dot(self.kernel(X, X[index]))
self.b = np.mean(b_i)
plt.plot(losses)
plt.title("loss per epochs")
plt.show()
def _decision_function(self, X):
return (self.alpha * self.y).dot(self.kernel(self.X, X)) + self.b
def predict(self, X):
return np.sign(self._decision_function(X))
def score(self, X, y):
y_hat = self.predict(X)
return np.mean(y == y_hat)
def plot_decision_boundary(self):
plt.scatter(self.X[:, 0], self.X[:, 1], c=self.y, s=50, cmap=plt.cm.Paired, alpha=.5)
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
Z = self._decision_function(xy).reshape(XX.shape)
ax.contour(XX, YY, Z, colors=['b', 'g', 'r'], levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'], linewidths=[2.0, 2.0, 2.0])
ax.scatter(self.X[:, 0][self.alpha > 0.], self.X[:, 1][self.alpha > 0.], s=50,
linewidth=1, facecolors='none', edgecolors='k')
plt.show()
def load_data(file_path, target_column, feature_columns, delimiter):
data = pd.read_csv(file_path, delimiter=delimiter, header=None, na_values='?')
data = data.dropna()
data.columns = ["age", "sex", "cp", "restbp", "chol", "fbs", "restecg", "thalach", "exang",
"oldpeak", "slope", "ca", "thal", "hd"]
le = preprocessing.LabelEncoder()
y = le.fit_transform(data[target_column])
y = np.where(y > 0, 1, -1) # Converting the target to binary (-1, 1)
X = data[feature_columns]
return X.values, y
if __name__ == '__main__':
file_path = "C:/Users/Lenovo/Downloads/processed.cleveland.data"
feature_columns = ["age", "thalach"] # Choose two columns for visualization
target_column = "hd"
delimiter = ','
X, y = load_data(file_path, target_column, feature_columns, delimiter)
y[y == 0] = -1
svm = SVMDualProblem(C=1.0, kernel='rbf', sigma=0.5)
svm.fit(X, y, lr=1e-2)
print("train score:", svm.score(X, y))
svm.plot_decision_boundary()"""
"""
#code 5
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
class SVMDualProblem:
def __init__(self, C=1.0, kernel='rbf', sigma=0.1, degree=2):
self.C = C
if kernel == 'poly':
self.kernel = self._polynomial_kernel
self.c = 1
self.degree = degree
else:
self.kernel = self._rbf_kernel
self.sigma = sigma
self.X = None
self.y = None
self.alpha = None
self.b = 0
self.ones = None
def _rbf_kernel(self, X1, X2):
return np.exp(-(1 / self.sigma ** 2) * np.linalg.norm(X1[:, np.newaxis] - X2[np.newaxis, :], axis=2) ** 2)
def _polynomial_kernel(self, X1, X2):
return (self.c + X1.dot(X2.T)) ** self.degree
def fit(self, X, y, lr=1e-3, epochs=500):
self.X = X
self.y = y
self.alpha = np.random.random(X.shape[0])
self.b = 0
self.ones = np.ones(X.shape[0])
y_iy_jk_ij = np.outer(y, y) * self.kernel(X, X)
losses = []
for epoch in range(epochs):
gradient = self.ones - y_iy_jk_ij.dot(self.alpha)
self.alpha = self.alpha + lr * gradient
self.alpha[self.alpha > self.C] = self.C
self.alpha[self.alpha < 0] = 0
loss = np.sum(self.alpha) - 0.5 * np.sum(np.outer(self.alpha, self.alpha) * y_iy_jk_ij)
losses.append(loss)
if epoch % 100 == 0:
print(f"Epoch {epoch}: Loss = {loss}")
index = np.where((self.alpha > 0) & (self.alpha < self.C))[0]
b_i = y[index] - (self.alpha * y).dot(self.kernel(X, X[index]))
self.b = np.mean(b_i)
plt.plot(losses)
plt.title("Loss per Epoch")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.show()
def _decision_function(self, X):
return (self.alpha * self.y).dot(self.kernel(self.X, X)) + self.b
def predict(self, X):
return np.sign(self._decision_function(X))
def score(self, X, y):
y_hat = self.predict(X)
return np.mean(y == y_hat)
def load_data(file_path, delimiter):
data = pd.read_csv(file_path, delimiter=delimiter, header=None, na_values='?')
data = data.dropna()
data.columns = ["age", "sex", "cp", "restbp", "chol", "fbs", "restecg", "thalach", "exang",
"oldpeak", "slope", "ca", "thal", "hd"]
le = preprocessing.LabelEncoder()
y = le.fit_transform(data["hd"])
y = np.where(y > 0, 1, -1) # Converting the target to binary (-1, 1)
X = data.drop(columns=["hd"])
return X.values, y
if __name__ == "__main__":
file_path = "/Users/Lenovo/Downloads/processed.cleveland.data"
X, y = load_data(file_path, delimiter=',')
# Split data into training and testing sets with stratification
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
svm = SVMDualProblem(C=1.0, kernel='rbf', sigma=0.5)
svm.fit(X_train, y_train, lr=1e-3, epochs=500)
print("Training accuracy:", svm.score(X_train, y_train))
print("Testing accuracy:", svm.score(X_test, y_test))
"""
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import seaborn as sns
import matplotlib.pyplot as plt
class SVMDualProblem:
def __init__(self, C=0, kernel='rbf', sigma=2, degree=2):
self.C = C
if kernel == 'poly':
self.kernel = self._polynomial_kernel
self.c = 1
self.degree = degree
else:
self.kernel = self._rbf_kernel
self.sigma = sigma
self.X = None
self.y = None
self.alpha = None
self.b = 0
self.ones = None
def _rbf_kernel(self, X1, X2):
return np.exp(-(1 / self.sigma ** 2) * np.linalg.norm(X1[:, np.newaxis] - X2[np.newaxis, :], axis=2) ** 2)
def _polynomial_kernel(self, X1, X2):
return (self.c + X1.dot(X2.T)) ** self.degree
def fit(self, X, y, lr=1e-3, epochs=500):
self.X = X
self.y = y
self.alpha = np.random.random(X.shape[0])
self.b = 0
self.ones = np.ones(X.shape[0])
y_iy_jk_ij = np.outer(y, y) * self.kernel(X, X)
losses = []
for epoch in range(epochs):
gradient = self.ones - y_iy_jk_ij.dot(self.alpha)
self.alpha = self.alpha + lr * gradient
self.alpha[self.alpha > self.C] = self.C
self.alpha[self.alpha < 0] = 0
loss = np.sum(self.alpha) - 0.5 * np.sum(np.outer(self.alpha, self.alpha) * y_iy_jk_ij)
losses.append(loss)
if epoch % 100 == 0:
print(f"Epoch {epoch}: Loss = {loss}")
index = np.where((self.alpha > 0) & (self.alpha < self.C))[0]
b_i = y[index] - (self.alpha * y).dot(self.kernel(X, X[index]))
self.b = np.mean(b_i)
plt.plot(losses)
plt.title("Loss per Epoch")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.show()
def _decision_function(self, X):
return (self.alpha * self.y).dot(self.kernel(self.X, X)) + self.b
def predict(self, X):
return np.sign(self._decision_function(X))
def score(self, X, y):
y_hat = self.predict(X)
return np.mean(y == y_hat)
def load_data(file_path, delimiter):
data = pd.read_csv(file_path, delimiter=delimiter, header=None, na_values='?')
data = data.dropna()
data.columns = ["age", "sex", "cp", "restbp", "chol", "fbs", "restecg", "thalach", "exang",
"oldpeak", "slope", "ca", "thal", "hd"]
le = preprocessing.LabelEncoder()
y = le.fit_transform(data["hd"])
y = np.where(y > 0, 1, -1) # Converting the target to binary (-1, 1)
X = data.drop(columns=["hd"])
return X.values, y
def plot_confusion_matrix(y_true, y_pred):
cm = confusion_matrix(y_true, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['No Disease', 'Disease'], yticklabels=['No Disease', 'Disease'])
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.title('Confusion Matrix')
plt.show()
if __name__ == "__main__":
file_path = "/Users/Lenovo/Downloads/processed.cleveland.data"
X, y = load_data(file_path, delimiter=',')
# Split data into training and testing sets with stratification
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
svm = SVMDualProblem(C=1.0, kernel='rbf', sigma=0.5)
svm.fit(X_train, y_train, lr=1e-3, epochs=500)
y_train_pred = svm.predict(X_train)
y_test_pred = svm.predict(X_test)
print("Training accuracy:", accuracy_score(y_train, y_train_pred))
print("Testing accuracy:", accuracy_score(y_test, y_test_pred))
print("\nClassification Report (Training):")
print(classification_report(y_train, y_train_pred))
print("\nClassification Report (Testing):")
print(classification_report(y_test, y_test_pred))
print("\nConfusion Matrix (Testing):")
plot_confusion_matrix(y_test, y_test_pred)