application_vnd.jupyter.stderr_output_2.png application_vnd.jupyter.stderr_output_1.png image_output_1.png
from sklearn.decomposition import PCA, RandomizedPCA

from sklearn.grid_search import GridSearchCV

from sklearn.svm import SVC

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')
y_train = train.label.values.ravel()
X_train = train.values[:, 1:]
X_test = test.values

def show(array):
    array = array.reshape(28, 28)
def plot_gallery(images, n_row=2, n_col=5):
    h = w = 28
    'Helper function to plot a gallery of portraits'
    for i in range(n_row * n_col):
        plt.xticks(())
        plt.yticks(())

def normalized_data():
    X = np.concatenate((X_train, X_test))
    x_test = X_test - X.mean(0)
    x_train = X_train - X.mean(0)
    x_test /= x_test.std(1).reshape((-1, 1))
    x_train /= x_train.std(1).reshape((-1, 1))
    return (x_train, x_test)

from sklearn.decomposition import PCA, RandomizedPCA
def components(n_components, random=False, show_components=True):
    x_train, x_test = normalized_data()
    pca = PCA(n_components=n_components)
    if random:
        pca = RandomPCA(n_components=n_components)
    pca.fit(x_train)
    components = pca.components_
    x_train = pca.transform(x_train)
    x_test = pca.transform(x_test)
    return (x_train, x_test)

from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVC
def testCLF(clf, parameters, X):
    search = GridSearchCV(clf, parameters)
    search.fit(X, y_train)
    return search.best_estimator_

clf = SVC()
parameters = {'C': [0.5, 1.0], 'kernel': ['linear', 'poly', 'rbf', 'sigmoid', 'precomputed'], 'degree': [2, 3]}
X = components(10)[0]
testCLF(clf, parameters, X)