-
Notifications
You must be signed in to change notification settings - Fork 17
/
kernel_matrix.py
83 lines (72 loc) · 2.48 KB
/
kernel_matrix.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
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
from django.http import HttpResponse,Http404
from django.template import RequestContext
from django.shortcuts import render_to_response
import modshogun as sg
import numpy as np
import scipy as sp
import json
def entrance(request):
properties = { 'title': 'Kernel Matrix Visualization' }
return render_to_response("kernel_matrix/index.html",
properties,
context_instance = RequestContext(request))
def create_toy_data(request):
xmin = -5
xmax = 5
x = sp.arange(xmin, xmax, (xmax-xmin)/100.0)
C = 0 #offset
b = 0
amplitude = 1
frequency = 1
noise_level = 0.1
try:
amplitude = float(request.POST['amplitude'])
frequency = float(request.POST['frequency'])
noise_level = float(request.POST['noise_level'])
except:
raise Http404
y = b*x + C + amplitude * sp.sin(frequency * x)
y += noise_level * np.random.randn(y.shape[0])
toy_data = { 'data': [] }
for i in xrange(len(x)):
toy_data['data'].append( { 'x': x[i], 'y': y[i]})
return HttpResponse(json.dumps(toy_data))
def generate_matrix(request):
result = []
try:
arguments = _read_toy_data(request)
result = _process(*arguments)
except:
import traceback
print traceback.format_exc()
raise Http404
return HttpResponse(json.dumps(result))
def _read_toy_data(request):
y_set = []
x_set = []
toy_data = json.loads(request.POST['toy_data'])
for pt in toy_data:
y_set.append(float(pt["y"]))
x_set.append(float(pt["x"]))
kernel_width = float(request.POST['kernel_width'])
degree = int(request.POST['degree'])
kernel_name = request.POST['kernel']
return (x_set, y_set, kernel_width, kernel_name, degree)
def _process(x1_set, x2_set, kernel_width, kernel_name, degree):
num = len(x1_set)
if num == 0:
raise Http404
examples = np.zeros((2, num))
for i in xrange(num):
examples[0,i] = x1_set[i]
examples[1,i] = x2_set[i]
feat_train = sg.RealFeatures(examples)
# construct covariance function
if kernel_name == "line":
kernel = sg.LinearKernel(feat_train, feat_train)
elif kernel_name == "poly":
kernel = sg.PolyKernel(feat_train, feat_train, degree, True)
elif kernel_name == "gaus":
kernel = sg.GaussianKernel(feat_train, feat_train, kernel_width)
kernel_matrix=kernel.get_kernel_matrix()
return kernel_matrix.tolist()