Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/classifiers/nmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ def fit(self, xtr, ytr):
pass

def predict(self, xts):
pass
"""Predict the class of each input.
input:
x: set of images (test set, numpy array)
output:
y: labels (numpy array)
"""
dist = euclidean_distances(xts, self._centroids)
y = self._class_labels[np.argmin(dist, axis=1)]

return y
16 changes: 14 additions & 2 deletions src/fun_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ def load_data(filename):


def split_data(x, y, tr_fraction=0.5):
"""
Split the data x, y into two random subsets

"""
pass

def load_mnist(csv_filename):
"""Load the MNIST dataset.
input:
csv_filename: string with the path to the dataset to load
output:
X: set of images (numpy array)
y: labels (numpy array)
"""
data = read_csv(csv_filename)
z = np.array(data)
y = z[:, 0]
X = z[:, 1:]
return X, y
17 changes: 16 additions & 1 deletion src/tests/test_nmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from os import path
import unittest

from fun_utils import load_data, split_data
from fun_utils import load_data, split_data,load_mnist
from classifiers import NMC


Expand Down Expand Up @@ -53,3 +53,18 @@ def test_predict(self):
yc = self.clf.predict(self.x)
assert yc.ndim == 1
assert yc.shape[0] == self.x.shape[0]

def test_load_mnist(self):
mnist_path = path.join(
path.dirname(__file__), '..', '../data', 'mnist_data.csv')

x, y = load_mnist(mnist_path)

assert type(x) == np.ndarray
assert type(y) == np.ndarray

assert x.ndim == 2
assert y.ndim == 1

# Verifica che il numero di etichette corrisponda al numero di immagini
assert x.shape[0] == y.shape[0]