-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
163 lines (120 loc) · 4.36 KB
/
app.py
File metadata and controls
163 lines (120 loc) · 4.36 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
from flask import Flask,render_template,request
import matplotlib.pyplot as plt
import io
import base64
import numpy as np
import cvxopt.solvers
app = Flask(__name__)
@app.route("/")
def decision():
return render_template("home.html")
@app.route('/main',methods = ['POST'])
def home():
form_data = request.form
flag = []
for i in form_data.values():
flag.append(int(i))
if flag[0]==1:
ip = np.array([[1.682, -11.852],[0.386, 16.851],[-1.913, -11.315],[-1.754, 4.084],[-1.656, -10.834],[0.655, -8.111],
[-0.704, 5.832],[2.704, -10.758],[-2.656, -3.552],[0.861, 8.853],[0.975, 19.607],[3.621, -2.048],
[-1.195, -3.235],[1.202, 10.168],[3.193, 11.248]])
return render_template("index.html",ip = ip)
else:
return render_template("empty.html")
@app.route('/plot',methods = ['POST'])
def build_plot():
form_data = request.form
ip = []
for i in form_data.values():
ip.append(float(i))
ip = np.reshape(ip,(15,2))
img = io.BytesIO()
plt.scatter(ip[:,0],ip[:,1],c="black")
for i in range(15):
j = 0
plt.annotate(i+1, (ip[i][j], ip[i][j+1]))
plt.savefig(img,format = 'png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode()
plt.switch_backend('agg')
#return '<img src="data:image/png;base64,{}">'.format(plot_url)
return render_template("results.html",ip = ip,plot_url = plot_url)
@app.route('/result',methods = ['POST'])
def final():
form_data = request.form
global ip
ip = []
for i in form_data.values():
ip.append(float(i))
ip = np.reshape(ip,(15,3))
ip_s = ip[ip[:,2].argsort()]
X = ip_s[:,[0,1]]
y = ip_s[:,2]
m = X.shape[0]
K = np.array([np.dot(X[i], X[j])
for j in range(m)
for i in range(m)]).reshape((m, m))
P = cvxopt.matrix(np.outer(y, y) * K)
q = cvxopt.matrix(-1 * np.ones(m))
A = cvxopt.matrix(y, (1, m))
b = cvxopt.matrix(0.0)
G = cvxopt.matrix(np.diag(-1 * np.ones(m)))
h = cvxopt.matrix(np.zeros(m))
solution = cvxopt.solvers.qp(P, q, G, h, A, b)
multipliers = np.ravel(solution['x'])
has_positive_multiplier = multipliers > 1e-7
sv_multipliers = multipliers[has_positive_multiplier]
support_vectors = X[has_positive_multiplier]
support_vectors_y = y[has_positive_multiplier]
def compute_w(multipliers, X, y):
return np.sum(multipliers[i] * y[i] * X[i]
for i in range(len(y)))
global w
w = compute_w(sv_multipliers, support_vectors, support_vectors_y)
print(w)
def compute_b(w, X, y):
return np.sum([y[i] - np.dot(w, X[i])
for i in range(len(X))])/len(X)
global bia
bia = compute_b(w, support_vectors, support_vectors_y)
img = io.BytesIO()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
x = np.linspace(-4,4,100)
plt.plot(x, (-w[0]*x - bia)/w[1], '-r')
plt.plot(x, (-w[0]*x - bia-1)/w[1], '-.r')
plt.plot(x, (-w[0]*x - bia+1)/w[1], '-.r')
plt.scatter(ip[:,0],ip[:,1],c=ip[:,2])
plt.savefig(img,format = 'png')
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode()
plt.switch_backend('agg')
return render_template("final.html",ip = ip,plot_url = plot_url,w = w , bia = bia)
@app.route('/calc',methods = ['POST'])
def calculate():
form_data = request.form
op = []
for i in form_data.values():
op.append(float(i))
img = io.BytesIO()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
x = np.linspace(-4,4,100)
plt.plot(x, (-w[0]*x - bia)/w[1], '-r')
plt.plot(x, (-w[0]*x - bia-1)/w[1], '-.r')
plt.plot(x, (-w[0]*x - bia+1)/w[1], '-.r')
plt.scatter(ip[:,0],ip[:,1],c=ip[:,2])
plt.scatter(op[0],op[1],c="blue")
plt.savefig(img,format = 'png')
img.seek(0)
y = np.dot(w,op) + bia
if y >= 0:
y = 1
else:
y = -1
plot_url = base64.b64encode(img.getvalue()).decode()
plt.switch_backend('agg')
#return '<img src="data:image/png;base64,{}">'.format(plot_url)
return render_template("cal.html",op = op,plot_url = plot_url,y = y)
if __name__ == "__main__":
app.run(debug=True,threaded = True)