Last active
August 29, 2015 14:02
-
-
Save kmkurn/d830e24990a76ab938fd to your computer and use it in GitHub Desktop.
Functions related to Gaussian kernel model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
from __future__ import division | |
import numpy as np | |
def gaussian_kernel(p, q, h): | |
"""Compute Gaussian kernel function.""" | |
return np.exp(-(p-q)**2 / (2*h*h)) | |
def kernel_matrix(train_data, bandwith=0.25): | |
"""Compute kernel matrix.""" | |
x = train_data | |
n, = x.shape | |
K = np.empty((n, n)) | |
for i, x1 in enumerate(x): | |
for j, x2 in enumerate(x): | |
K[i, j] = gaussian_kernel(x1, x2, bandwith) | |
return K | |
def kernel_model(test_point, train_data, weights, bandwith=0.25, | |
skip_zeros=False, tol=1e-7): | |
"""Compute the estimated value of a test point using kernel model.""" | |
if skip_zeros: | |
tpl = [(a, xi) for a, xi in zip(weights, train_data) if abs(a) >= tol] | |
else: | |
tpl = [(a, xi) for a, xi in zip(weights, train_data)] | |
return sum(a * gaussian_kernel(test_point, xi, bandwith) for a, xi in tpl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment