-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboth_techniques.py
More file actions
153 lines (127 loc) · 5.58 KB
/
Copy pathboth_techniques.py
File metadata and controls
153 lines (127 loc) · 5.58 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
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, learning_rate=0.1, n_iterations=50):
self.learning_rate = learning_rate
self.n_iterations = n_iterations
def train(self, X, y):
self.weights = np.zeros(X.shape[1] + 1)
for _ in range(self.n_iterations):
for xi, target in zip(X, y):
prediction = self.predict(xi)
error = target - prediction
self.weights[1:] += self.learning_rate * error * xi
self.weights[0] += self.learning_rate * error
def predict(self, X):
activation = np.dot(X, self.weights[1:]) + self.weights[0]
return np.where(activation >= 0, 1, -1)
def accuracy(self, X, y):
predictions = self.predict(X)
correct = np.sum(predictions == y)
total = len(y)
return correct / total
# Generowanie losowych danych
np.random.seed(0)
X1 = np.random.normal(loc=[2, 2], scale=[0.5, 0.5], size=(50, 2))
X2 = np.random.normal(loc=[8, 2], scale=[0.5, 0.5], size=(50, 2))
X3 = np.random.normal(loc=[2, 8], scale=[0.5, 0.5], size=(50, 2))
X4 = np.random.normal(loc=[8, 8], scale=[0.5, 0.5], size=(50, 2))
X_train = np.vstack([X1, X2, X3, X4])
y_train = np.array([0]*50 + [1]*50 + [2]*50 + [3]*50)
indices = np.random.permutation(len(X_train))
split = int(0.8 * len(X_train))
X_train, X_test = X_train[indices[:split]], X_train[indices[split:]]
y_train, y_test = y_train[indices[:split]], y_train[indices[split:]]
# Klasyfikator perceptron - One-Versus-The-Rest
class PerceptronOvR:
def __init__(self, learning_rate=0.1, n_iterations=50):
self.learning_rate = learning_rate
self.n_iterations = n_iterations
self.perceptrons = []
def train(self, X, y):
unique_classes = np.unique(y)
for cls in unique_classes:
y_binary = np.where(y == cls, 1, -1)
perceptron = Perceptron(learning_rate=self.learning_rate, n_iterations=self.n_iterations)
perceptron.train(X, y_binary)
self.perceptrons.append(perceptron)
def predict(self, X):
predictions = []
for perceptron in self.perceptrons:
predictions.append(perceptron.predict(X))
return np.argmax(predictions, axis=0)
def accuracy(self, X, y):
predictions = self.predict(X)
correct = np.sum(predictions == y)
total = len(y)
return correct / total
# Trenowanie klasyfikatora
perceptron_ovr = PerceptronOvR(learning_rate=0.1, n_iterations=100)
perceptron_ovr.train(X_train, y_train)
# Obliczanie dokładności
accuracy_ovr = perceptron_ovr.accuracy(X_test, y_test)
print("Dokładność klasyfikatora Perceptron One-Versus-The-Rest:", accuracy_ovr)
# Wizualizacja granic decyzyjnych
plt.figure(figsize=(8, 6))
h = 0.02
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = perceptron_ovr.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.8)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, s=20, edgecolors='k')
plt.xlabel('X1')
plt.ylabel('X2')
plt.title('Perceptron - One-Versus-The-Rest')
plt.show()
# Klasyfikator perceptron - One-Versus-One
class PerceptronOvO:
def __init__(self, learning_rate=0.1, n_iterations=50):
self.learning_rate = learning_rate
self.n_iterations = n_iterations
self.perceptrons = []
def train(self, X, y):
unique_classes = np.unique(y)
for i in range(len(unique_classes)):
for j in range(i+1, len(unique_classes)):
X_subset = X[(y == unique_classes[i]) | (y == unique_classes[j])]
y_subset = y[(y == unique_classes[i]) | (y == unique_classes[j])]
y_binary = np.where(y_subset == unique_classes[i], 1, -1)
perceptron = Perceptron(learning_rate=self.learning_rate, n_iterations=self.n_iterations)
perceptron.train(X_subset, y_binary)
self.perceptrons.append((unique_classes[i], unique_classes[j], perceptron))
def predict(self, X):
predictions = []
for perceptron in self.perceptrons:
class1, class2, perceptron_model = perceptron
prediction = perceptron_model.predict(X)
prediction = np.where(prediction == 1, class1, class2)
predictions.append(prediction)
predictions = np.array(predictions)
return np.array([max(set(row), key=list(row).count) for row in predictions.T])
def accuracy(self, X, y):
predictions = self.predict(X)
correct = np.sum(predictions == y)
total = len(y)
return correct / total
# Trenowanie klasyfikatora One-Versus-One
perceptron_ovo = PerceptronOvO(learning_rate=0.1, n_iterations=100)
perceptron_ovo.train(X_train, y_train)
# Obliczanie dokładności
accuracy_ovo = perceptron_ovo.accuracy(X_test, y_test)
print("Dokładność klasyfikatora Perceptron One-Versus-One:", accuracy_ovo)
# Wizualizacja granic decyzyjnych
plt.figure(figsize=(8, 6))
h = 0.02
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = perceptron_ovo.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.8)
plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train, s=20, edgecolors='k')
plt.xlabel('X1')
plt.ylabel('X2')
plt.title('Perceptron - One-Versus-One')
plt.show()