コード例 #1
0
def load_dataset():
    # Loading the MNIST dataset
    data = {}
    x_train, x_test, y_train, y_test = load()
    data['x_train'], data['y_train'], data['x_test'], data[
        'y_test'] = x_train, x_test, y_train, y_test
    data['x_train'] = data['x_train'].reshape(60000, 784)
    data['x_test'] = data['x_test'].reshape(10000, 784)
    data['x_train'] = data['x_train'].astype(float)
    data['x_test'] = data['x_test'].astype(float)

    # Transpose the training and test datasets
    data['x_train'], data['x_test'] = np.transpose(
        data['x_train']), np.transpose(data['x_test'])

    return data
コード例 #2
0
import math
import numpy as np
from download_mnist import load
import operator
import time
# classify using kNN
# x_train = np.load('../x_train.npy')
# y_train = np.load('../y_train.npy')
# x_test = np.load('../x_test.npy')
# y_test = np.load('../y_test.npy')
x_train, y_train, x_test, y_test = load()
x_train = x_train.reshape(60000, 28, 28)
x_test = x_test.reshape(10000, 28, 28)
x_train = x_train.astype(float)
x_test = x_test.astype(float)


def kNNClassify(newInput, dataSet, labels, k):
    result = []
    ########################
    # Input your code here #
    ########################
    test_len = len(newInput)  # Getting the length of test images
    train_len = len(dataSet)  # Getting the length of training images

    l2_distance = np.zeros((test_len, train_len))
    for i in range(test_len):  # Finding L2 distance of each test image
        for j in range(train_len):
            distance = np.linalg.norm(
                dataSet[j] - newInput[i])  # Calculating the L2 Distance
            l2_distance[
コード例 #3
0
# import math
import numpy as np
from download_mnist import load
# import operator
import time
import heapq
from collections import Counter

# classify using kNN
# x_train = np.load('../x_train.npy')
# y_train = np.load('../y_train.npy')
# x_test = np.load('../x_test.npy')
# y_test = np.load('../y_test.npy')
x_train_ori, y_train, x_test_ori, y_test = load()
x_train_ori = x_train_ori.reshape(60000, 28, 28)
x_test_ori = x_test_ori.reshape(10000, 28, 28)
x_train_ori = x_train_ori.astype(float)
x_test_ori = x_test_ori.astype(float)

# turn each entry into an 1-D array
x_train = []
x_test = []
for X_pic in x_train_ori:
    tmp = []
    for X in X_pic:
        tmp = np.append(tmp, X)
    x_train.append(tmp)

for X_pic in x_test_ori:
    tmp = []
    for X in X_pic: