jinzishuai / learn2deeplearn

A repository of codes in learning deep learning
GNU General Public License v3.0
13 stars 1 forks source link

How to sort a 3D numpy arry #47

Closed jinzishuai closed 6 years ago

jinzishuai commented 6 years ago

problem 5 of https://github.com/jinzishuai/learn2deeplearn/blob/master/google_dl_udacity/lesson1/1_notmnist.ipynb

my code of sorting 4 images

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import data

def look_for_duplicates (dataset):
    print ('looking for duplicates in a dataset of shape: ', dataset.shape)

def sort4images(mytest):
    plt.figure(1)
    plt.subplot(221)
    plt.imshow(mytest[0,:,:])
    plt.subplot(222)
    plt.imshow(mytest[1,:,:])
    plt.subplot(223)
    plt.imshow(mytest[2,:,:])
    plt.subplot(224)
    plt.imshow(mytest[3,:,:])

    mytest.sort(axis=0)
    plt.figure(2)
    plt.subplot(221)
    plt.imshow(mytest[0,:,:])
    plt.subplot(222)
    plt.imshow(mytest[1,:,:])
    plt.subplot(223)
    plt.imshow(mytest[2,:,:])
    plt.subplot(224)
    plt.imshow(mytest[3,:,:])
    plt.show()

letters=np.array(['A', 'B', 'C','D','E','F','G','H','I','J'])
train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = data.load_all_data_from_single_pickle_file('../notMNIST.pickle')
# sort the train_set
print('my test results are',letters[train_labels[0:4]])
sort4images(train_dataset[0:4,:,:])

Before Sort

image

After Sort: all messed up

image

jinzishuai commented 6 years ago

mytest.sort(axis=-1) the same as mytest.sort(axis=2)

image

mytest.sort(axis=1)

image

jinzishuai commented 6 years ago

Solution: sorted, indices = np.unique(mytest, axis=0, return_index = True)

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import data

def look_for_duplicates (dataset):
    print ('looking for duplicates in a dataset of shape: ', dataset.shape)

def sort4images(mytest):
    plt.figure(1)
    plt.subplot(221)
    plt.imshow(mytest[0,:,:])
    plt.subplot(222)
    plt.imshow(mytest[1,:,:])
    plt.subplot(223)
    plt.imshow(mytest[2,:,:])
    plt.subplot(224)
    plt.imshow(mytest[3,:,:])

    #mytest.sort(axis=2)
    sorted, indices = np.unique(mytest, axis=0, return_index = True)
    plt.figure(2)
    plt.subplot(221)
    plt.imshow(sorted[0,:,:])
    plt.subplot(222)
    plt.imshow(sorted[1,:,:])
    plt.subplot(223)
    plt.imshow(sorted[2,:,:])
    plt.subplot(224)
    plt.imshow(sorted[3,:,:])
    plt.show()
    return sorted, indices

letters=np.array(['A', 'B', 'C','D','E','F','G','H','I','J'])
train_dataset, train_labels, valid_dataset, valid_labels, test_dataset, test_labels = data.load_all_data_from_single_pickle_file('../notMNIST.pickle')
# sort the train_set
print('my test results are',letters[train_labels[0:4]])
sorted, indices = sort4images(train_dataset[0:4,:,:])
print('my sorted test results are',letters[train_labels[indices]])
jinzishuai commented 6 years ago

image