- 9th Jul 2024
- 18:10 pm

In this problem you will implement a linear classifier as covered in the lecture videos. The file linear_classifier.py provides instructions. An additional file linear_classifier_outline.py has been provided. The outline here follows the video lectures closely. Your solution must still implement the functions provided, and you may not modify the signatures, or return values of these functions in any way. Run the classifier on the mnist data following the steps outlined in the file linear_classifier.py. Adjust the parameters so that you obtain at least 90 percent accuracy on the test data. Do not import anything besides numpy.

**Implement A Linear Classifier for MNIST Data - Get Assignment Solution**

Please note that this is a sample Implement A Linear Classifier for MNIST Data assignment solved by our Python Experts. * These solutions are intended to be used for research and reference purposes only. *If you can learn any concepts by going through the reports and code, then our Python Tutors would be very happy.

- To download the complete solution along with Code, Report and screenshots - Please visit our
**Python Assignment Sample Solution**page - Reach out to our Python Tutors to get online tutoring related to this assignment and get your doubts cleared
- You can check the partial solution for this assignment in this blog below

**Free Assignment Solution - Implement A Linear Classifier for MNIST Data **

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

"""

@author:

"""

import numpy as np

import pandas as pd

X = np.random.randn(1000, 2)

y = X[: , 0] - X[:, 1] > 0

positive_points = X[y]

negative_points = X[np.logical_not(y)]

y = y.astype("int64")

def train(X, y, num_classes, learning_rate, num_iterations, num_batches):

'''- Assume that X has size m x n. So there are m examples each having n features.

- y is the vector of classes or labels of the data in X. The first entry of y corresponds

to the example in the first row of X and so on. Note that there is no parameter corresponding to

lambda so it is assumed to be 0.

- Randomly separate X and y into num_batches. This means you will split X into groups having roughly the

- num_classes is the number of different classes in the data.

- You will perform gradient descent on the weights for each batch. This will be done num_iterations

times and during each iteration gradient descent will be carried out on each batch.

- Return weights W and bias b.

You may not import anything else besides numpy(and pandas if you'd like).'''

m, n = X.shape

W = np.random.rand(n,num_classes) * 1/n

b = np.random.rand(num_classes,1)

y_1hot = np.zeros((m, num_classes))

y_1hot[np.arange(m), y] = 1

batch_size = int(np.ceil(len(X) / num_batches))

for epoch in range(num_iterations):

p = np.random.permutation(len(X))

X, y_1hot = X[p], y_1hot[p]

for index in range(0, X.shape[0], batch_size):

X_batch = X[index:min(index + batch_size, X.shape[0]),:]

y_batch = y_1hot[index:min(index + batch_size, y_1hot.shape[0]),:]

W, b = train_batch(X_batch, y_batch, W, b, learning_rate)

a = X @ W + b.reshape(-1,)

e = a - y_1hot

return W, b

def train_batch(X, y, W, b, learning_rate):

'''perform gradient descent using X. Modify W and b by performing

gradient descent and return the updated arrays.'''

a = X @ W + b.reshape(-1,)

e = a - y

grad_b = np.sum(e, axis=0) * 2 * learning_rate / (X.shape[0])

grad_W = ((X.T) @ e) * 2 * learning_rate / (X.shape[0])

b -= grad_b.reshape(-1,1)

W -= grad_W

return W, b

def predict(W, b, Xtest, ytest):

'''Return the accuracy, measured from 0 to 1, when the linear classifier

determined by W and b is applied to the data in Xtest. 0 means there

were no correct predictions, while 1 indicates that everything was predicted

correctly.'''

a = Xtest @ W + b.reshape(-1,)

ypred = np.argmax(a, axis=1)

accuracy = (ytest == ypred).sum() / (len(ytest)-Xtest.shape[1])

#ypred = np.argmax(a, axis=1)

#accuracy = (ytest == ypred).sum() / len(ytest)-len(Xtest)

return accuracy

W, b = train(X, y, 2, .001, 1000, 10)

print(predict(W, b, X, y)) ###When the code is correctly implemented the value should be 1 or very close to it.

'''Include your code for the mnist dataset here. You should print out the

accuracy of your trained classifier when applied to the test data. Note that the

last column of both data sets is the class label which should be extracted and

used as ytrain, and ytest respectively. Carry out the following steps:

1. Read the training and test data into arrays.

2. Split the arrays into features and labels. Call these Xtrain, ytrain, Xtest, ytest.

3. Standardize the data so that the columns have mean zero and variance 1.

4. Train a model with the training data.

5. Get the accuracy.

Note you will be graded on accuracy, you will need over 90% to get full credit.

This will require you to adjust the learning rate. I will run the program myself.

Assume that the data is in the same directory as the script.'''

train_data = pd.read_csv('mnist_train.csv', header=None)

test_data = pd.read_csv('mnist_test.csv', header=None)

Xtrain = train_data.drop(columns=[784]).values

ytrain = train_data[784].values.astype("int64")

Xtest = test_data.drop(columns=[784]).values

ytest = test_data[784].values.astype("int64")

Xtrain = Xtrain/255

Xtest = Xtest/255

num_classes = 10

learning_rate = 1e-3

num_iterations = 20

num_batches = 200

W, b = train(Xtrain, ytrain, num_classes, learning_rate, num_iterations, num_batches)

print("\nMNIST model Accuracy is:", predict(W, b, Xtest, ytest))

Get the best Linear Classifier for MNIST Data assignment help and tutoring services from our experts now!

**About The Author - Jane Smith**

Jane Smith is a software engineer specializing in machine learning and data science. With extensive experience in Python and Numpy, she excels at developing efficient algorithms. Jane enjoys sharing her knowledge through tutorials and open-source projects, as demonstrated in her linear classifier implementation for MNIST data.