<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 LEARNING APPLICATIONS
In this notebook we will take a look at some indicative applications of machine learning techniques. We will cover content from learning.py, for chapter 18 from Stuart Russel's and Peter Norvig's book Artificial Intelligence
Step1:  CONTENTS
MNIST Handwritten Digits
Loading and Visualising
Testing
MNIST Fashion
MNIST HANDWRITTEN DIGITS CLASSIFICATION
The MNIST Digits database, available from this page, is a large database of handwritten digits that is commonly used for training and testing/validating in Machine learning.
The dataset has 60,000 training images each of size 28x28 pixels with labels and 10,000 testing images of size 28x28 pixels with labels.
In this section, we will use this database to compare performances of different learning algorithms.
It is estimated that humans have an error rate of about 0.2% on this problem. Let's see how our algorithms perform!
NOTE
Step2:  Check the shape of these NumPy arrays to make sure we have loaded the database correctly.
Each 28x28 pixel image is flattened to a 784x1 array and we should have 60,000 of them in training data. Similarly, we should have 10,000 of those 784x1 arrays in testing data.
Step3:  Visualizing Data
To get a better understanding of the dataset, let's visualize some random images for each class from training and testing datasets.
Step4:  Let's have a look at the average of all the images of training and testing data.
Step5:  Testing
Now, let us convert this raw data into DataSet.examples to run our algorithms defined in learning.py. Every image is represented by 784 numbers (28x28 pixels) and we append them with its label or class to make them work with our implementations in learning module.
Step6:  Now, we will initialize a DataSet with our training examples, so we can use it in our algorithms.
Step7:  Moving forward we can use MNIST_DataSet to test our algorithms.
Plurality Learner
The Plurality Learner always returns the class with the most training samples. In this case, 1.
Step8:  It is obvious that this Learner is not very efficient. In fact, it will guess correctly in only 1135/10000 of the samples, roughly 10%. It is very fast though, so it might have its use as a quick first guess.
Naive-Bayes
The Naive-Bayes classifier is an improvement over the Plurality Learner. It is much more accurate, but a lot slower.
Step9:  To make sure that the output we got is correct, let's plot that image along with its label.
Step10:  k-Nearest Neighbors
We will now try to classify a random image from the dataset using the kNN classifier.
Step11:  To make sure that the output we got is correct, let's plot that image along with its label.
Step12:  Hurray! We've got it correct. Don't worry if our algorithm predicted a wrong class. With this techinique we have only ~97% accuracy on this dataset.
MNIST FASHION
Another dataset in the same format is MNIST Fashion. This dataset, instead of digits contains types of apparel (t-shirts, trousers and others). As with the Digits dataset, it is split into training and testing images, with labels from 0 to 9 for each of the ten types of apparel present in the dataset. The below table shows what each label means
Step13:  Visualizing Data
Let's visualize some random images for each class, both for the training and testing sections
Step14:  Let's now see how many times each class appears in the training and testing data
Step15:  Unlike Digits, in Fashion all items appear the same number of times.
Testing
We will now begin testing our algorithms on Fashion.
First, we need to convert the dataset into the learning-compatible Dataset class
Step16:  Plurality Learner
The Plurality Learner always returns the class with the most training samples. In this case, 9.
Step17:  Naive-Bayes
The Naive-Bayes classifier is an improvement over the Plurality Learner. It is much more accurate, but a lot slower.
Step18:  Let's check if we got the right output.
Step19:  K-Nearest Neighbors
With the dataset in hand, we will first test how the kNN algorithm performs
Step20:  The output is 1, which means the item at index 211 is a trouser. Let's see if the prediction is correct
<ASSISTANT_TASK:>
Python Code:
from learning import *
from probabilistic_learning import *
from notebook import *
Explanation: LEARNING APPLICATIONS
In this notebook we will take a look at some indicative applications of machine learning techniques. We will cover content from learning.py, for chapter 18 from Stuart Russel's and Peter Norvig's book Artificial Intelligence: A Modern Approach. Execute the cell below to get started:
End of explanation
train_img, train_lbl, test_img, test_lbl = load_MNIST()
Explanation: CONTENTS
MNIST Handwritten Digits
Loading and Visualising
Testing
MNIST Fashion
MNIST HANDWRITTEN DIGITS CLASSIFICATION
The MNIST Digits database, available from this page, is a large database of handwritten digits that is commonly used for training and testing/validating in Machine learning.
The dataset has 60,000 training images each of size 28x28 pixels with labels and 10,000 testing images of size 28x28 pixels with labels.
In this section, we will use this database to compare performances of different learning algorithms.
It is estimated that humans have an error rate of about 0.2% on this problem. Let's see how our algorithms perform!
NOTE: We will be using external libraries to load and visualize the dataset smoothly (numpy for loading and matplotlib for visualization). You do not need previous experience of the libraries to follow along.
Loading MNIST Digits Data
Let's start by loading MNIST data into numpy arrays.
The function load_MNIST() loads MNIST data from files saved in aima-data/MNIST. It returns four numpy arrays that we are going to use to train and classify hand-written digits in various learning approaches.
End of explanation
print("Training images size:", train_img.shape)
print("Training labels size:", train_lbl.shape)
print("Testing images size:", test_img.shape)
print("Testing labels size:", test_lbl.shape)
Explanation: Check the shape of these NumPy arrays to make sure we have loaded the database correctly.
Each 28x28 pixel image is flattened to a 784x1 array and we should have 60,000 of them in training data. Similarly, we should have 10,000 of those 784x1 arrays in testing data.
End of explanation
# takes 5-10 seconds to execute this
show_MNIST(train_lbl, train_img)
# takes 5-10 seconds to execute this
show_MNIST(test_lbl, test_img)
Explanation: Visualizing Data
To get a better understanding of the dataset, let's visualize some random images for each class from training and testing datasets.
End of explanation
print("Average of all images in training dataset.")
show_ave_MNIST(train_lbl, train_img)
print("Average of all images in testing dataset.")
show_ave_MNIST(test_lbl, test_img)
Explanation: Let's have a look at the average of all the images of training and testing data.
End of explanation
print(train_img.shape, train_lbl.shape)
temp_train_lbl = train_lbl.reshape((60000,1))
training_examples = np.hstack((train_img, temp_train_lbl))
print(training_examples.shape)
Explanation: Testing
Now, let us convert this raw data into DataSet.examples to run our algorithms defined in learning.py. Every image is represented by 784 numbers (28x28 pixels) and we append them with its label or class to make them work with our implementations in learning module.
End of explanation
# takes ~10 seconds to execute this
MNIST_DataSet = DataSet(examples=training_examples, distance=manhattan_distance)
Explanation: Now, we will initialize a DataSet with our training examples, so we can use it in our algorithms.
End of explanation
pL = PluralityLearner(MNIST_DataSet)
print(pL(177))
%matplotlib inline
print("Actual class of test image:", test_lbl[177])
plt.imshow(test_img[177].reshape((28,28)))
Explanation: Moving forward we can use MNIST_DataSet to test our algorithms.
Plurality Learner
The Plurality Learner always returns the class with the most training samples. In this case, 1.
End of explanation
# takes ~45 Secs. to execute this
nBD = NaiveBayesLearner(MNIST_DataSet, continuous = False)
print(nBD(test_img[0]))
Explanation: It is obvious that this Learner is not very efficient. In fact, it will guess correctly in only 1135/10000 of the samples, roughly 10%. It is very fast though, so it might have its use as a quick first guess.
Naive-Bayes
The Naive-Bayes classifier is an improvement over the Plurality Learner. It is much more accurate, but a lot slower.
End of explanation
%matplotlib inline
print("Actual class of test image:", test_lbl[0])
plt.imshow(test_img[0].reshape((28,28)))
Explanation: To make sure that the output we got is correct, let's plot that image along with its label.
End of explanation
# takes ~20 Secs. to execute this
kNN = NearestNeighborLearner(MNIST_DataSet, k=3)
print(kNN(test_img[211]))
Explanation: k-Nearest Neighbors
We will now try to classify a random image from the dataset using the kNN classifier.
End of explanation
%matplotlib inline
print("Actual class of test image:", test_lbl[211])
plt.imshow(test_img[211].reshape((28,28)))
Explanation: To make sure that the output we got is correct, let's plot that image along with its label.
End of explanation
train_img, train_lbl, test_img, test_lbl = load_MNIST(fashion=True)
Explanation: Hurray! We've got it correct. Don't worry if our algorithm predicted a wrong class. With this techinique we have only ~97% accuracy on this dataset.
MNIST FASHION
Another dataset in the same format is MNIST Fashion. This dataset, instead of digits contains types of apparel (t-shirts, trousers and others). As with the Digits dataset, it is split into training and testing images, with labels from 0 to 9 for each of the ten types of apparel present in the dataset. The below table shows what each label means:
| Label | Description |
| ----- | ----------- |
|   0   | T-shirt/top |
|   1   | Trouser     |
|   2   | Pullover    |
|   3   | Dress       |
|   4   | Coat        |
|   5   | Sandal      |
|   6   | Shirt       |
|   7   | Sneaker     |
|   8   | Bag         |
|   9   | Ankle boot  |
Since both the MNIST datasets follow the same format, the code we wrote for loading and visualizing the Digits dataset will work for Fashion too! The only difference is that we have to let the functions know which dataset we're using, with the fashion argument. Let's start by loading the training and testing images:
End of explanation
# takes 5-10 seconds to execute this
show_MNIST(train_lbl, train_img, fashion=True)
# takes 5-10 seconds to execute this
show_MNIST(test_lbl, test_img, fashion=True)
Explanation: Visualizing Data
Let's visualize some random images for each class, both for the training and testing sections:
End of explanation
print("Average of all images in training dataset.")
show_ave_MNIST(train_lbl, train_img, fashion=True)
print("Average of all images in testing dataset.")
show_ave_MNIST(test_lbl, test_img, fashion=True)
Explanation: Let's now see how many times each class appears in the training and testing data:
End of explanation
temp_train_lbl = train_lbl.reshape((60000,1))
training_examples = np.hstack((train_img, temp_train_lbl))
# takes ~10 seconds to execute this
MNIST_DataSet = DataSet(examples=training_examples, distance=manhattan_distance)
Explanation: Unlike Digits, in Fashion all items appear the same number of times.
Testing
We will now begin testing our algorithms on Fashion.
First, we need to convert the dataset into the learning-compatible Dataset class:
End of explanation
pL = PluralityLearner(MNIST_DataSet)
print(pL(177))
%matplotlib inline
print("Actual class of test image:", test_lbl[177])
plt.imshow(test_img[177].reshape((28,28)))
Explanation: Plurality Learner
The Plurality Learner always returns the class with the most training samples. In this case, 9.
End of explanation
# takes ~45 Secs. to execute this
nBD = NaiveBayesLearner(MNIST_DataSet, continuous = False)
print(nBD(test_img[24]))
Explanation: Naive-Bayes
The Naive-Bayes classifier is an improvement over the Plurality Learner. It is much more accurate, but a lot slower.
End of explanation
%matplotlib inline
print("Actual class of test image:", test_lbl[24])
plt.imshow(test_img[24].reshape((28,28)))
Explanation: Let's check if we got the right output.
End of explanation
# takes ~20 Secs. to execute this
kNN = NearestNeighborLearner(MNIST_DataSet, k=3)
print(kNN(test_img[211]))
Explanation: K-Nearest Neighbors
With the dataset in hand, we will first test how the kNN algorithm performs:
End of explanation
%matplotlib inline
print("Actual class of test image:", test_lbl[211])
plt.imshow(test_img[211].reshape((28,28)))
Explanation: The output is 1, which means the item at index 211 is a trouser. Let's see if the prediction is correct:
End of explanation
<END_TASK>