-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmachine_learning_20_1_svm.py
41 lines (31 loc) · 1.9 KB
/
machine_learning_20_1_svm.py
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
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
# обучающая выборка с тремя признаками (третий - константа +1)
x_train = [[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]
clf = svm.SVC(kernel='linear') # SVM с линейным ядром
clf.fit(x_train, y_train) # нахождение вектора w по обучающей выборке
lin_clf = svm.LinearSVC() # SVM для линейно разделимой выборки (используется для получения вектора w)
lin_clf.fit(x_train, y_train) # нахождение вектора w по обучающей выборке
v = clf.support_vectors_ # выделение опорных векторов
w = lin_clf.coef_[0] # коэффициенты линейной модели
print(w, v, sep='\n')
# формирование графиков для визуализации полученных результатов
x_train = np.array(x_train)
y_train = np.array(y_train)
line_x = list(range(max(x_train[:, 0]))) # формирование графика разделяющей линии
line_y = [-x*w[0]/w[1] - w[2] for x in line_x]
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.plot(line_x, line_y, color='green')
plt.xlim([0, 45])
plt.ylim([0, 75])
plt.ylabel("длина")
plt.xlabel("ширина")
plt.grid(True)
plt.show()