RBFSampler (Radial Basis Function Kernel)#
Kernel:
f(x) = \exp(-\gamma \cdot x^2)
[1]:
from sklearn.kernel_approximation import RBFSampler
from sklearn.linear_model import SGDClassifier
X = [
[0, 0],
[1, 1],
[1, 0],
[0, 1],
]
y = [
0,
0,
1,
1,
]
rbfSampler = RBFSampler(
# -------------------------------------------------------------------------
# Parameter of RBF kernel: exp(-gamma * x^2).
gamma=1,
# -------------------------------------------------------------------------
# Number of Monte Carlo samples per original feature.
n_components=100,
# -------------------------------------------------------------------------
# Pseudo-random number generator to control the generation of the random
# weights and random offset when fitting the training data.
random_state=1,
)
X_features = rbfSampler.fit_transform(X)
X_features.shape
[1]:
(4, 100)
[2]:
sgdClassifier = SGDClassifier(max_iter=100, tol=1e-3)
sgdClassifier.fit(X_features, y)
sgdClassifier.score(X_features, y)
[2]:
1.0