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. (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 out all (a few will do if there are too many) the 1s in your test set that the system predicted to be 7s.

Solutions

Expert Solution

Code and output attached.

Code tested with

  • Python 3.6.9
  • sklearn version 0.23.2
  • matplotlib version 3.3.2

Note:

  • sklearn will take some time (~ 2-3 mins, depending on your internet speed) to download the 70000 images MNIST dataset (~128 MB zip file) so please be patient. If you already have the dataset, you can use that as well.
  • Model training will take around ~7-8 mins, depending on the hardware on your machine.
  • Calculating score on test set will take around ~7-8 mins, depending on the hardware on your machine.

Part 2) and Part 3) combined code

import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
from sklearn.svm import SVC
from sklearn import metrics
from sklearn.model_selection import train_test_split

print('Downloading dataset')
X, Y = fetch_openml('mnist_784', version=1, return_X_y=True)
print('Download complete')

# To apply a classifier on this data, we need to flatten the image, to
# turn the data in a [samples, feature] matrix:
number_of_samples = len(X)
X = X.reshape((number_of_samples, -1))

# Split the data in train and test sets
X_train, X_test, y_train, y_test = train_test_split(
  X,
  Y,
  test_size = 0.2, # 20 % test set, rest 80 % train set
  shuffle = False
)

# # Create a SVM classifier model
model = SVC()

print('Training model')
# # Train the model
model.fit(X_train, y_train)
print('Training complete')

# # Get preditions on test set
predicted = model.predict(X_test)

print("Classification report for SVM model:\n", metrics.classification_report(y_test, predicted))

print("1s digits in the test set that the model predicted to be 7s (showing atmost 4):")

test_images_and_truth_and_predictions = list(zip(X[len(y_train):], y_test, predicted))
# filter the images with true label as 1 but model predicted 7.
filtered = [x for x in test_images_and_truth_and_predictions if x[1] == 1 and x[2] == 7]
if len(filtered) == 0:
  print('No such prediction found!')
else:
  # Plot the predictions
  _, axes = plt.subplots(1, len(filtered[:4]))
  for ax, (test_image, truth, prediction) in zip(axes[:], filtered[:4]):
    ax.set_axis_off()
    ax.imshow(test_image, cmap=plt.cm.gray_r, interpolation='nearest')
    ax.set_title('Prediction: {}'.format(prediction))

  plt.show()

Output

As you can see, accuracy is 98% and my trained model did not make any prediction where true label was "digit 1" but it predicted "digit 7". You can try other configurations of SVM model, different parameters, different train/test split, etc. and see if it outputs any such predictions.


Related Solutions

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 *
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the users enters a negative number. The program then prints out: a) the sum of all the numbers b) the average of all the numbers c) the max of the numbers d) the min of the numbers Note we did not cover lists (array) so you will not use them in this problem. Finally, ask the user for their name, then print their name as...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
Python Clean up the code Recall that once a subclass inherits from a parent class, it...
Python Clean up the code Recall that once a subclass inherits from a parent class, it automatically has access to all of the parents' methods. Sometimes, the subclass needs to do extra things within a method which the parent does not do. For example, both UserPlayer and BasicMonster have their specialized __init__ methods which override the one from Player. Discuss the following question with your partner: What are the other methods that are being overridden by a subclass in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT