<jupyter_start><jupyter_text>SVC on [hand-written digits](https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits)* compare different kernels<jupyter_code>import pandas as pd
from sklearn.svm import SVC
import matplotlib.pyplot as plt
import numpy as np
# load the data
flc = '/Users/pinqingkan/Desktop/DataScience/PythonDataScience/DAT210x-master/Module6/Datasets/'
fname = flc + 'optdigits.tra'
train0 = pd.read_csv(fname, header = None)
fname = flc + 'optdigits.tes'
test0 = pd.read_csv(fname, header = None)
# separate into data & labels
Nfeature = train0.shape[1]
X_train = train0.iloc[:, :Nfeature-1]
Y_train = train0.iloc[:, Nfeature-1:].values.ravel()
X_test = test0.iloc[:, :Nfeature-1]
Y_test = test0.iloc[:, Nfeature-1:].values.ravel()
# reduce the training set to test the effect: every 100, keep the first 4
Nsample = X_train.shape[0]
Nblock = Nsample // 100
Nblock = Nblock+1 if Nsample%100 > 0 else Nblock
for iblock in range(Nblock):
    i1 = iblock*100
    i2 = i1+4
    train2 = X_train.iloc[i1:i2,:]
    label2 = Y_train[i1:i2]
    if iblock == 0:
        X_train2, Y_train2 = train2, label2
    else:
        X_train2 = pd.concat([X_train2, train2], axis = 0)
        Y_train2 = np.r_[Y_train2, label2]
# show some plots as example
plt.figure()
cnt = 0
for col in range(5):
    for row in range(5):
        plt.subplot(5,5,cnt+1)
        plt.imshow(X_train.iloc[cnt,:].values.reshape(8,8), cmap=plt.cm.gray_r)
        plt.axis('off')
        cnt += 1
plt.show()
# SVC classifier: compare different kernel
for icase in range(3):
    if icase == 0: fkernel = 'linear'
    elif icase == 1: fkernel = 'poly'
    else: fkernel = 'rbf'
    
    svc = SVC(C = 1.0, gamma = 1e-3, kernel = 'rbf', cache_size = 200)
    svc.fit(X_train, Y_train)
    #svc.fit(X_train2, Y_train2)
    print('score: ', svc.score(X_test, Y_test))
# prediction with SVC (rbf)
svc = SVC(C = 1.0, gamma = 1e-3, kernel = 'rbf', cache_size = 200)
svc.fit(X_train, Y_train)
Y_test2 = svc.predict(X_test)
# visualize the prediction results
plt.figure()
cnt = 0
istart = 0
for col in range(10):
    for row in range(5):
        cnt += 1
        idc = cnt + istart
        plt.subplot(5,10,cnt)
        plt.imshow(X_test.iloc[idc,:].values.reshape(8,8), cmap=plt.cm.gray_r)
        plt.axis('off')
        
        color2 = 'g' if Y_test2[idc] == Y_test[idc] else 'r'
        fttl = str(Y_test[idc]) + ': ' + str(Y_test2[idc])
        plt.title(fttl, color = color2)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14518695306284804, 'fraction_numerical': 0.03898170246618934, 'mean_word_length': 3.435626102292769, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '173424', 'license': 'MIT', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1005, 'n_words': 231, 'path': 'Lab6_1_Modeling_SVC/assignment2.ipynb', 'repo_name': 'rongqingpin/Python_Data_Science_edX_DAT210x', 'size': '2514'}