<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 CIFAR embedding through sklearn
Simple program to check how the different algorithms available through sklearn do at classifing images of cats and dogs. As expected, the problem is not simple enough for this programs to work. To be able to distinguish between the two, a more complex routine will be needed.
Step1:  A little function for plotting
Step2:  Let's use Patrick's program to load the CIFAR images. We load the images + reshape them as numpy arrays, needed for the following steps.
Step3:  Let's first try the  Isomap embedding
Step4:  Now let's do Spectral embedding. There are different method for this routine. However this 'standard' is the one giving less problems!
Step5:  Finally t-SNE
<ASSISTANT_TASK:>
Python Code:
import math
import os
from matplotlib import pyplot as plt
import numpy as np
from six.moves import cPickle 
import matplotlib.pyplot as plt
from sklearn import manifold
from tools import CifarLoader
# General parameters for classification
n_neighbors = 30
n_components = 2
Explanation: CIFAR embedding through sklearn
Simple program to check how the different algorithms available through sklearn do at classifing images of cats and dogs. As expected, the problem is not simple enough for this programs to work. To be able to distinguish between the two, a more complex routine will be needed.
End of explanation
def plotting(X, Y):
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    
    ax1.scatter(X[:, 0],X[:, 1], c='b', label='Cats')
    ax1.scatter(Y[:, 0], Y[:, 1], c='r', label='Dogs')
    plt.legend(loc='upper left');
    
    return plt.show()
Explanation: A little function for plotting
End of explanation
catsdogs = CifarLoader(path = 'data_batch_1')
cats_and_dogs = catsdogs.cats + catsdogs.dogs
n_cats, n_dogs = len(catsdogs.cats), len(catsdogs.dogs)
dims = cats_and_dogs[0].shape
cats_and_dogs = np.array(cats_and_dogs).reshape((n_cats+n_dogs, dims[0]*dims[1]*dims[1]))
Explanation: Let's use Patrick's program to load the CIFAR images. We load the images + reshape them as numpy arrays, needed for the following steps.
End of explanation
print("Computing Isomap embedding")
iso = manifold.Isomap(n_neighbors, n_components).fit_transform(cats_and_dogs)
print("Done.")
plotting(iso[:n_cats], iso[n_cats:])
Explanation: Let's first try the  Isomap embedding
End of explanation
print("Computing Spectral embedding")
emb = manifold.LocallyLinearEmbedding(n_neighbors, n_components, eigen_solver='auto', 
                                        method = 'standard').fit_transform(cats_and_dogs)
print("Done.")
plotting(emb[:n_cats], emb[n_cats:])
Explanation: Now let's do Spectral embedding. There are different method for this routine. However this 'standard' is the one giving less problems!
End of explanation
print("Computing t-SNE")
tSNE = manifold.TSNE(n_components=n_components, init='pca', random_state=0).fit_transform(cats_and_dogs)
print("Done.")
plotting(tSNE[:n_cats], tSNE[n_cats:])
Explanation: Finally t-SNE
End of explanation
<END_TASK>