<jupyter_start><jupyter_text>Kernel Approximations for Large-Scale Non-Linear Learning Predictions in a kernel-SVM are made using the formular$$\hat{y} = \alpha_1 y_1 k(\mathbf{x^{(1)}}, \mathbf{x}) + ... + \alpha_n y_n k(\mathbf{x^{(n)}}, \mathbf{x})> 0$$$$0 \leq \alpha_i \leq C$$ Radial basis function (Gaussian) kernel:$$k(\mathbf{x}, \mathbf{x'}) = \exp(-\gamma ||\mathbf{x} - \mathbf{x'}||^2)$$ Kernel approximation $\phi$:    $$\phi(\mathbf{x})\phi(\mathbf{x'}) \approx k(\mathbf{x}, \mathbf{x'})$$ $$\hat{y} \approx w^T \phi(\mathbf{x})> 0$$<jupyter_code>from helpers import Timer

from sklearn.datasets import load_digits
from sklearn.cross_validation import train_test_split
digits = load_digits()
X, y = digits.data / 16., digits.target

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)<jupyter_output><empty_output><jupyter_text>Linear SVM<jupyter_code>from sklearn.svm import LinearSVC
from sklearn.grid_search import GridSearchCV

grid = GridSearchCV(LinearSVC(random_state=0),
                    param_grid={'C': np.logspace(-3, 2, 6)}, cv=5)
with Timer():
    grid.fit(X_train, y_train)
grid.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Kernel SVM<jupyter_code>from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV

grid = GridSearchCV(SVC(), param_grid={'C': np.logspace(-3, 2, 6),
                                       'gamma': np.logspace(-3, 2, 6)}, cv=5)
with Timer():
    grid.fit(X_train, y_train)
grid.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Kernel Approximation + Linear SVM<jupyter_code>from sklearn.kernel_approximation import RBFSampler
from sklearn.pipeline import make_pipeline

pipe = make_pipeline(RBFSampler(random_state=0),
                     LinearSVC(dual=False, random_state=0))

grid = GridSearchCV(pipe, param_grid={'linearsvc__C': np.logspace(-3, 2, 6),
                                      'rbfsampler__gamma': np.logspace(-3, 2, 6)}, cv=5)
with Timer():
    grid.fit(X_train, y_train)
grid.score(X_test, y_test)<jupyter_output><empty_output><jupyter_text>Out of core Kernel approximation<jupyter_code>import cPickle
from sklearn.linear_model import SGDClassifier

sgd = SGDClassifier(random_state=0)

for iteration in range(30):
    for i in range(9):
        X_batch, y_batch = cPickle.load(open("data/batch_%02d.pickle" % i))
        sgd.partial_fit(X_batch, y_batch, classes=range(10))
X_test, y_test = cPickle.load(open("data/batch_09.pickle"))

sgd.score(X_test, y_test)

sgd = SGDClassifier(random_state=0)
rbf_sampler = RBFSampler(gamma=.2, random_state=0).fit(np.ones((1, 64)))

for iteration in range(30):
    for i in range(9):
        X_batch, y_batch = cPickle.load(open("data/batch_%02d.pickle" % i))
        X_kernel = rbf_sampler.transform(X_batch)
        sgd.partial_fit(X_kernel, y_batch, classes=range(10))
sgd.score(rbf_sampler.transform(X_test), y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1486394557823129, 'fraction_numerical': 0.018367346938775512, 'mean_word_length': 4.812252964426878, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '451799', 'license': 'Apache-2.0', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1090, 'n_words': 207, 'path': 'scikit/Chapter 8/Kernel Approximations.ipynb', 'repo_name': 'KarthikKothareddy/Data-Science-Practice', 'size': '2940'}