2011-11-13 18:58:43 +08:00
|
|
|
"""
|
2011-11-14 07:00:08 +08:00
|
|
|
==================================================
|
|
|
|
|
Explicit feature map approximation for RBF kernels
|
|
|
|
|
==================================================
|
2011-11-13 18:58:43 +08:00
|
|
|
|
2011-11-14 07:00:08 +08:00
|
|
|
An example shows how to use RBFSampler to appoximate
|
|
|
|
|
the feature map of an RBF kernel. Results for varying
|
|
|
|
|
amounts of Monte Carlo samplings are shown.
|
2011-11-13 18:58:43 +08:00
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
print __doc__
|
|
|
|
|
|
|
|
|
|
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
|
|
|
|
|
# modified Andreas Mueller
|
|
|
|
|
# License: Simplified BSD
|
|
|
|
|
|
|
|
|
|
# Standard scientific Python imports
|
|
|
|
|
import pylab as pl
|
2011-11-14 07:00:08 +08:00
|
|
|
import numpy as np
|
2011-11-13 18:58:43 +08:00
|
|
|
|
|
|
|
|
# Import datasets, classifiers and performance metrics
|
2011-11-14 18:59:15 +08:00
|
|
|
from sklearn import datasets, svm, pipeline
|
2011-12-18 20:48:36 +08:00
|
|
|
from sklearn.kernel_approximation import RBFSampler
|
2011-11-13 18:58:43 +08:00
|
|
|
|
|
|
|
|
# The digits dataset
|
|
|
|
|
digits = datasets.load_digits()
|
|
|
|
|
|
|
|
|
|
# To apply an classifier on this data, we need to flatten the image, to
|
|
|
|
|
# turn the data in a (samples, feature) matrix:
|
|
|
|
|
n_samples = len(digits.images)
|
|
|
|
|
data = digits.images.reshape((n_samples, -1))
|
|
|
|
|
|
2011-11-14 07:00:08 +08:00
|
|
|
# We learn the digits on the first half of the digits
|
|
|
|
|
data_train, targets_train = data[:n_samples/2], digits.target[:n_samples/2]
|
|
|
|
|
|
|
|
|
|
# Now predict the value of the digit on the second half:
|
|
|
|
|
data_test, targets_test = data[n_samples/2:], digits.target[n_samples/2:]
|
|
|
|
|
|
2011-11-13 18:58:43 +08:00
|
|
|
# Create a classifier: a support vector classifier
|
|
|
|
|
kernel_svm = svm.SVC(gamma=0.001)
|
|
|
|
|
linear_svm = svm.LinearSVC()
|
|
|
|
|
|
|
|
|
|
# create pipeline from kernel approximation
|
|
|
|
|
# and linear svm
|
2011-11-14 07:00:08 +08:00
|
|
|
feature_map = RBFSampler(gamma=0.001)
|
2011-11-13 18:58:43 +08:00
|
|
|
approx_kernel_svm = pipeline.Pipeline([("feature_map", feature_map),
|
|
|
|
|
("svm", svm.LinearSVC())])
|
|
|
|
|
|
2011-11-14 07:00:08 +08:00
|
|
|
# fit and predict using linear and kernel svm:
|
|
|
|
|
kernel_svm.fit(data_train, targets_train)
|
|
|
|
|
kernel_svm_score = kernel_svm.score(data_test, targets_test)
|
|
|
|
|
|
|
|
|
|
linear_svm.fit(data_train, targets_train)
|
|
|
|
|
linear_svm_score = linear_svm.score(data_test, targets_test)
|
2011-11-13 18:58:43 +08:00
|
|
|
|
2011-11-14 07:00:08 +08:00
|
|
|
sample_sizes = 20*np.arange(1,15)
|
|
|
|
|
approx_kernel_scores = []
|
|
|
|
|
for D in sample_sizes:
|
2011-12-17 04:29:18 +08:00
|
|
|
approx_kernel_svm.set_params(feature_map__n_components=D)
|
2011-11-14 07:00:08 +08:00
|
|
|
approx_kernel_svm.fit(data_train, targets_train)
|
|
|
|
|
score = approx_kernel_svm.score(data_test, targets_test)
|
|
|
|
|
approx_kernel_scores.append(score)
|
2011-11-13 18:58:43 +08:00
|
|
|
|
2011-11-14 18:59:15 +08:00
|
|
|
# plot the results:
|
|
|
|
|
pl.plot(sample_sizes, approx_kernel_scores, label="approximate kernel")
|
|
|
|
|
|
|
|
|
|
# horizontal lines for exact rbf and linear kernels:
|
|
|
|
|
pl.plot([sample_sizes[0], sample_sizes[-1]], [linear_svm_score, linear_svm_score], label="linear svm")
|
|
|
|
|
pl.plot([sample_sizes[0], sample_sizes[-1]], [kernel_svm_score, kernel_svm_score], label="rbf svm")
|
|
|
|
|
|
|
|
|
|
# vertical line for dataset dimensionality = 64
|
|
|
|
|
pl.plot([64, 64], [0.7, 1], label="original dimensionality")
|
|
|
|
|
|
|
|
|
|
# legends and labels
|
|
|
|
|
pl.xlim(sample_sizes[0], sample_sizes[-1])
|
|
|
|
|
pl.ylim(np.min(approx_kernel_scores),1)
|
|
|
|
|
pl.xlabel("Sampling steps = transformed feature dimension")
|
|
|
|
|
pl.ylabel("Classification accuracy")
|
|
|
|
|
pl.legend()
|
2011-11-14 07:00:08 +08:00
|
|
|
pl.show()
|