-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathmachine_learning_20_2_svm.py
More file actions
34 lines (27 loc) · 1.46 KB
/
machine_learning_20_2_svm.py
File metadata and controls
34 lines (27 loc) · 1.46 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
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
# обучающая выборка с тремя признаками (третий - константа +1)
x_train = [[30, 10], [15, 50], [10, 50], [20, 30], [25, 30], [20, 60], [15, 70], [40, 40], [30, 45], [20, 45], [40, 30], [7, 35]]
x_train = [x + [1] for x in x_train]
y_train = [-1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1]
clf = svm.SVC(kernel='linear') # SVM с линейным ядром
clf.fit(x_train, y_train) # нахождение вектора w по обучающей выборке
y_pr = clf.predict(x_train) # проверка на обучающей выборке
print(np.array(y_train) - np.array(y_pr)) # нули - без ошибок; иначе - ошибка
v = clf.support_vectors_ # выделение опорных векторов
print(v)
# формирование графиков для визуализации полученных результатов
x_train = np.array(x_train)
y_train = np.array(y_train)
x_0 = x_train[y_train == 1] # формирование точек для 1-го
x_1 = x_train[y_train == -1] # и 2-го классов
plt.scatter(x_0[:, 0], x_0[:, 1], color='red')
plt.scatter(x_1[:, 0], x_1[:, 1], color='blue')
plt.scatter(v[:, 0], v[:, 1], s=70, edgecolor=None, linewidths=0, marker='s')
plt.xlim([0, 45])
plt.ylim([0, 75])
plt.ylabel("длина")
plt.xlabel("ширина")
plt.grid(True)
plt.show()