Question

In: Computer Science

From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000...

From the MNIST dataset introduced in class, write code in Python that

a) Splits the 42000 training images into a training set (50% of all the data) and a test set (the rest). The labels should also be split accordingly. [10 points]

b) Use this training set for training a multi-class logistic regression model and test it on the test set. Report the score. [10 points]

c) Use this training set for training a multi-class support vector machine model and test it on the test set. Report the score.

Solutions

Expert Solution

Part a :
I have taken 60000 instances instead of 42000 as given in question because no of intances in training set of MNIST dataset =60000 only.
After that I have divided the dataset into 50 % train set and 50 % test set as per given in question.

Code :

!pip3 install python-mnist
from mnist import MNIST
from sklearn.model_selection import train_test_split
mndata = MNIST('data')
print('dataset loading- trainng')
##Loading dataset from the drive as I have download the dataset and store it in my drive
X,y = mndata.load('/content/drive/My Drive/MNIST/train-images.idx3-ubyte','/content/drive/My Drive/MNIST/train-labels.idx1-ubyte')
print("No of instances in data",len(X))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)
print("No of instances in train data",len(X_train))
print("No of instances in test data",len(X_test))

Output :

Part b:

Code :

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings("ignore") # To ignore warnings
clf = LogisticRegression(random_state=0).fit(X_train, y_train) ## Fitting the logistic model on train set
y_pred=clf.predict(X_train) # predicting value for train data
accuracy= accuracy_score(y_train, y_pred) # calculating accuracy score for train data
print ("Train accuracy :",accuracy)
y_pred=clf.predict(X_test) # predicting value for test data
accuracy= accuracy_score(y_test, y_pred) # calculating accuracy score for test data
print ("Test accuracy :",accuracy)

Output:

Training acuuracy using multi-class logistic regression model  = 94.01 %
Testing acuuracy using multi-class logistic regression model  = 91.37 %

Part c:

Code :

from sklearn import svm
from sklearn.metrics import accuracy_score
import warnings
warnings.filterwarnings("ignore") # To ignore warnings
clf = svm.SVC()
clf.fit(X_train, y_train) ## Fitting the svm model on train set
y_pred=clf.predict(X_train) # predicting value for train data
accuracy= accuracy_score(y_train, y_pred) # calculating accuracy score for train data
print ("Train accuracy :",accuracy)
y_pred=clf.predict(X_test) # predicting value for test data
accuracy= accuracy_score(y_test, y_pred) # calculating accuracy score for test data
print ("Test accuracy :",accuracy)

Output :

Training acuuracy using multi-class support vector machine model  = 98.8 %
Testing acuuracy using multi-class support vector machine modell  = 97.27 %


Related Solutions

From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000...
From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000 training images into a training set (50% of all the data) and a test set (the rest). The labels should also be split accordingly. (PLEASE ONLY SOLVE 2 & 3) 2) Basically repeat Part 1, but now use 80% of the images for training and the other 20% for testing. Report scores. [10 points] 3) Use the SVM model from part 2 to print...
1) Code in python for solving the MNIST classification problem (for full size of the training...
1) Code in python for solving the MNIST classification problem (for full size of the training set Classify two digits from MNIST dataset 2) Logistic regression (LR) with L1 regularization where LR is differentiable, But L1 norm is not • Use proximal gradient descent • For L1 norm, that’s soft-thresholding • Use tensorflow library
Write a python source code for a Unit class corresponding to the UML model of a...
Write a python source code for a Unit class corresponding to the UML model of a Unit shown. The description method should return a string value corresponding to the attributes of a Movie. Unit -code: String -name: String -credit points: int + __init__ (self, code, name, credit_points) + description (): String
Python Explain Code #Python program class TreeNode:
Python Explain Code   #Python program class TreeNode:    def __init__(self, key):        self.key = key        self.left = None        self.right = Nonedef findMaxDifference(root, diff=float('-inf')):    if root is None:        return float('inf'), diff    leftVal, diff = findMaxDifference(root.left, diff)    rightVal, diff = findMaxDifference(root.right, diff)    currentDiff = root.key - min(leftVal, rightVal)    diff = max(diff, currentDiff)     return min(min(leftVal, rightVal), root.key), diff root = TreeNode(6)root.left = TreeNode(3)root.right = TreeNode(8)root.right.left = TreeNode(2)root.right.right = TreeNode(4)root.right.left.left = TreeNode(1)root.right.left.right = TreeNode(7)print(findMaxDifference(root)[1])
This is using Python, it is utilizing code from a Fraction class to create a Binary...
This is using Python, it is utilizing code from a Fraction class to create a Binary Class Please leave comments so I may be able to learn from this. Instruction for Binary Class: Exercise 6.18: Design an immutable class BinaryNumber, in the style of our Fraction class. Internally, your only instance variable should be a text string that represents the binary value, of the form '1110100'. Implement a constructor that takes a string parameter that specifies the original binary value....
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Code in python: Write a class that implements a struct. In your struct store Student information...
Code in python: Write a class that implements a struct. In your struct store Student information such as name, netid, and gpa. Write a function that can access a student in a list of 5 structs by netid and print their name and gpa. Show that your function works by calling it.
write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
Write a single python file to perform the following tasks: (a) Get dataset “from sklearn.datasets import...
Write a single python file to perform the following tasks: (a) Get dataset “from sklearn.datasets import load_iris”. Split the dataset into two sets: 20% of samples for training, and 80% of samples for testing. NOTE 1: Please use “from sklearn.model_selection import train_test_split” with “random_state=N” and “test_size=0.80”. NOTE 2: The offset/bias column is not needed here for augmenting the input features. (b) Generate the target output using one-hot encoding for both the training set and the test set. (c) Using the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT