Skip to content

Instantly share code, notes, and snippets.

@lan496
Created July 15, 2018 15:32
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 lan496/3ce0fe7ece96f06039b4462b32c59360 to your computer and use it in GitHub Desktop.
Save lan496/3ce0fe7ece96f06039b4462b32c59360 to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.special import iv
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.utils.validation import check_is_fitted
class ExpSineSquaredSampler(BaseEstimator, TransformerMixin):
"""
Approximates feature map of an Exp-Sine-Squared kernel by its Fourier series expansion.
Exp-Sine-Squared kernel is given by:
k(x, y) = exp(-2(sin(pi * d(x, y) / periodicity) / length_scale)^2)
Parameters
----------
length_scale : float
periodicity : float
degrees : int
the number of incorporated terms
Attributes
----------
weights_ : array, (degrees, )
coef : array, (degrees, )
"""
def __init__(self, length_scale=1., periodicity=1., degrees=30):
self.length_scale = length_scale
self.periodicity = periodicity
self.degrees = degrees
def fit(self, X=None, y=None):
"""
Parameters
----------
Returns
-------
self
"""
self.weights_ = np.arange(1, self.degrees + 1) * 2 * np.pi / self.periodicity
self.coef_ = iv(np.arange(1, self.degrees + 1), self.length_scale ** (-2)) \
/ np.exp(self.length_scale ** (-2))
self.coef_ = np.sqrt(self.coef_)
return self
def transform(self, X):
"""
Parameters
----------
X : (n_samples, [n_features])
Returns
-------
X_new : (n_samples, 2 * degrees * n_features)
"""
check_is_fitted(self, 'weights_')
if X.ndim == 1:
X = X[:, np.newaxis]
n_samples, n_features = X.shape
projection = X[:, :, np.newaxis] * self.weights_[np.newaxis, np.newaxis, :]
X_new = np.concatenate([self.coef_[np.newaxis, np.newaxis, :] * np.cos(projection),
self.coef_[np.newaxis, np.newaxis, :] * np.sin(projection)],
axis=2)
X_new = np.reshape(X_new, (n_samples, n_features * 2 * self.degrees))
return X_new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment