-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM_1.py
More file actions
54 lines (49 loc) · 1.14 KB
/
SVM_1.py
File metadata and controls
54 lines (49 loc) · 1.14 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
import matplotlib.pyplot as plt
import numpy as np
n=int(input('Enter number of elements in class 1: '))
m=int(input('Enter number of elements in class 2: '))
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([0,10])
ax.set_ylim([0,10])
allx=[]
ally=[]
clicks=0
def onclick(event):
global n, clicks, m
#plt.clf()
ix, iy = (event.xdata), (event.ydata)
print('x = ', ix,'\ty = ', iy)
clicks=clicks+1
allx.append(ix)
ally.append(iy)
print(clicks)
plt.xlim(0,10)
plt.ylim(0,10)
if clicks<=n:
plt.scatter(ix,iy,color='red')
elif clicks<=n+m:
plt.scatter(ix,iy,color='blue')
elif clicks>n+m:
pi, pj, md=0, 0, 1000000
for i in range(n):
for j in range(n,n+m):
print(i,' ',j)
dist=(allx[i]-allx[j])**2+(ally[i]-ally[j])**2
if dist<md:
md=dist
pi=i
pj=j
print(pi,' ',pj)
px=(allx[pi]+allx[pj])/2
py=(ally[pi]+ally[pj])/2
xval=np.array([0,10])
slope=(allx[pi]-allx[pj])/(ally[pj]-ally[pi])
intercept=-slope*px+py
yval=slope*xval+intercept
plt.plot(xval,yval)
fig.canvas.mpl_disconnect(cid)
for i in range(0,1):
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
plt.close()