<jupyter_start><jupyter_text>CSE 555 - Introduction to pattern recognition Problem Set 1: Linear Discriminant Functions and Support Vector Machines  PART 1 Dataloader to read the MNIST dataset. The dataset is read and is flattened. Hence each 28x28 image is now a vector of length 784.<jupyter_code>#Using data loader from previous assignment

import numpy as np

def load_mnist(kind='train'):
    import os
    import gzip

    """Load MNIST data from `path`"""
    labels_path = ('%s-labels-idx1-ubyte.gz' % kind)
    images_path = ('%s-images-idx3-ubyte.gz' % kind)

    with gzip.open(labels_path, 'rb') as lbpath:
        labels = np.frombuffer(lbpath.read(), dtype=np.uint8,
                               offset=8)

    with gzip.open(images_path, 'rb') as imgpath:
        images = np.frombuffer(imgpath.read(), dtype=np.uint8,
                               offset=16).reshape(len(labels), 784)

    return images, labels


x_train, y_train = load_mnist(kind='train')
x_test, y_test = load_mnist(kind='t10k')<jupyter_output><empty_output><jupyter_text>Preprocessing the data so that the mean of the dataset is zero and the standard deviation is one.<jupyter_code>from sklearn import preprocessing
scaler = preprocessing.StandardScaler().fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)<jupyter_output><empty_output><jupyter_text>The penalty is chosen as 'l1' since the requirement is to use '1-norm soft margin'.The regularization parameter (C) is a hyper-parameter. This is tuned using cross-validation.<jupyter_code>from sklearn import svm
from sklearn.model_selection import GridSearchCV

parameters = {'C':[0.001, 0.01, 0.1, 1]}
svc = svm.LinearSVC(loss='squared_hinge', penalty='l1', dual=False)
clf = GridSearchCV(svc, parameters)
# clf = svm.LinearSVC(penalty='l1')
clf.fit(x_train, y_train)
y_pred = clf.predict(x_test)<jupyter_output><empty_output><jupyter_text>The best value for regularization parameter is obtained through cross-validation.<jupyter_code>print("Best C from cross validation: ", clf.best_params_)<jupyter_output>Best C from cross validation:  {'C': 0.1}<jupyter_text>The performance of the model is evaluated through accuracy and confusion matrix as metrics.<jupyter_code>from sklearn.metrics import confusion_matrix
print("score = %3.2f" %(clf.score(x_test, y_test)))
print("confusion matrix: \n ", confusion_matrix(y_test, y_pred))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10645293875873407, 'fraction_numerical': 0.01726263871763255, 'mean_word_length': 4.965686274509804, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '324275', 'license': 'MIT', 'n_tokens_mistral': 832, 'n_tokens_neox': 788, 'n_words': 217, 'path': 'PA2/SourceCode.ipynb', 'repo_name': 'ajithkumar-natarajan/CSE555-PR', 'size': '2433'}