Skip to content

Instantly share code, notes, and snippets.

@nkt1546789
Created January 25, 2017 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkt1546789/733e376a5c63b52b183cc548d3124bd3 to your computer and use it in GitHub Desktop.
Save nkt1546789/733e376a5c63b52b183cc548d3124bd3 to your computer and use it in GitHub Desktop.
Nonlinear data transformer using Gaussian kernel.
import numpy as np
class GaussianTransformer(object):
def __init__(self, sigma=1.0, b=100, random_state=1):
self.sigma = sigma
self.b = 100
self.random_state = random_state
def fit(self, X):
b = np.min([X.shape[0], self.b])
r = np.random.RandomState(self.random_state)
idx = r.permutation(X.shape[0])
self.Xce = X[idx[:b]]
self.Xce2 = np.c_[np.sum(self.Xce**2, axis=1)]
return self
def fit_transform(self, X):
self.fit(X)
return self.transform(X)
def transform(self, X):
X2 = np.c_[np.sum(X**2, axis=1)]
dist2 = X2 + self.Xce2.T - 2*X.dot(self.Xce.T)
return np.exp(-dist2/(2*self.sigma**2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment