Last active
December 16, 2015 16:29
-
-
Save bwhite/5463217 to your computer and use it in GitHub Desktop.
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
import sklearn.svm | |
import random | |
import numpy as np | |
if 0: | |
import kernels | |
kernel = kernels.histogram_intersection | |
else: | |
kernel = lambda x, y: np.dot(x, y.T) | |
def sample_generator(): | |
num_points = 100 | |
num_dims = 10 | |
num_dims_half = num_dims / 2 | |
num_pos_points = num_points / 2 | |
x = np.random.random((num_points, num_dims)) | |
x[:num_pos_points, :num_dims_half] += .5 | |
x = (x.T / np.sum(x, 1)).T | |
print(x[0]) | |
print(x[num_pos_points]) | |
y = np.zeros(num_points) | |
y[:num_pos_points] = 1 | |
y = map(int, y) | |
x = np.ascontiguousarray(x) | |
y = np.ascontiguousarray(y) | |
return x, y | |
x, y = sample_generator() | |
print(len(y)) | |
print(x.shape) | |
svm = sklearn.svm.SVC(kernel=kernel) | |
svm.fit(x, y) | |
gram = kernel(x, x) | |
svm2 = sklearn.svm.SVC(kernel='precomputed') | |
svm2.fit(gram, y) | |
a, b = sample_generator() | |
dual_coef = svm.dual_coef_ | |
intercept = svm.intercept_ | |
support = svm.support_ | |
for n, ax in zip(b, a): | |
k = kernel(x, np.ascontiguousarray(ax.reshape((-1, 1)).T)).T | |
dec_custom = np.sum(k[:, support] * dual_coef) | |
d_custom = int(dec_custom.ravel()[0] < intercept[0]) | |
d = int(svm2.decision_function(k) >= svm2.intercept_) | |
print('true_label[%d] predictions: svm.predict[%d] svm2.predict[%d] svm2.dec >= intercept[%d] manual[%d] decision_functions: svm.decision_function[%g] svm2.decision_function[%g] manual[%g]' % (n, svm.predict(ax)[0], svm2.predict(k)[0], d, d_custom, svm.decision_function(ax).ravel()[0], svm2.decision_function(k).ravel()[0], dec_custom.ravel()[0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment