From b876d8697c1a3205b79214bcb057810fc4a74360 Mon Sep 17 00:00:00 2001 From: eleonora Date: Wed, 14 Oct 2026 17:31:25 +0200 Subject: [PATCH 1/7] implementation of split_data --- src/fun_utils.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/fun_utils.py b/src/fun_utils.py index 37d579d..db725fe 100644 --- a/src/fun_utils.py +++ b/src/fun_utils.py @@ -31,4 +31,14 @@ def split_data(x, y, tr_fraction=0.5): Split the data x, y into two random subsets """ - pass + idx = np.array(range(x.shape[0])) + np.random.shuffle(idx) + ntr = int(tr_fraction * x.shape[0]) + + xtr = x[idx[0:ntr], :] + ytr = y[idx[0:ntr]] + + xts = x[idx[ntr:], :] + yts = y[idx[ntr:]] + + return xtr, ytr, xts, yts From 3ac980cd0bb5691a3a42653b84604cfebeb5de30 Mon Sep 17 00:00:00 2001 From: alessiousai Date: Wed, 14 Oct 2026 17:34:25 +0200 Subject: [PATCH 2/7] implementation of load_mnist and adding of test for load_mnist function --- src/fun_utils.py | 14 ++++++++++++++ src/tests/test_nmc.py | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/fun_utils.py b/src/fun_utils.py index 37d579d..c01f7d6 100644 --- a/src/fun_utils.py +++ b/src/fun_utils.py @@ -32,3 +32,17 @@ def split_data(x, y, tr_fraction=0.5): """ 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 \ No newline at end of file diff --git a/src/tests/test_nmc.py b/src/tests/test_nmc.py index 8c2f337..0036294 100644 --- a/src/tests/test_nmc.py +++ b/src/tests/test_nmc.py @@ -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 @@ -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] \ No newline at end of file From bd7152ee6bdfeaab19706d9613c4add01230c5c0 Mon Sep 17 00:00:00 2001 From: Dario <269144495+Dario-202@users.noreply.github.com> Date: Wed, 14 Oct 2026 17:36:28 +0200 Subject: [PATCH 3/7] implementation of fit function --- src/fun_utils.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/fun_utils.py b/src/fun_utils.py index 37d579d..3a83954 100644 --- a/src/fun_utils.py +++ b/src/fun_utils.py @@ -32,3 +32,17 @@ def split_data(x, y, tr_fraction=0.5): """ pass + +def fit(x_train, y_train): + """ + Fit a model to the training data + + """ + num_classes = np.unique(y_train).size + num_features = x_train.shape[1] + + centroids = np.zeros(shape=(num_classes,num_features)) + for i in range(num_classes): + centroids[i] = np.mean(x_train[y_train == i], axis=0) + return centroids + \ No newline at end of file From 70db2d896ea31c57fa1cd2dc2fa71f124526239d Mon Sep 17 00:00:00 2001 From: eleonora Date: Wed, 14 Oct 2026 17:43:36 +0200 Subject: [PATCH 4/7] implementation of split_data --- src/fun_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/fun_utils.py b/src/fun_utils.py index db725fe..7c083ff 100644 --- a/src/fun_utils.py +++ b/src/fun_utils.py @@ -28,6 +28,7 @@ def load_data(filename): def split_data(x, y, tr_fraction=0.5): """ + Split the data x, y into two random subsets """ From 43840dd552b82a7ba3f6b4308ad7e7ff521b2496 Mon Sep 17 00:00:00 2001 From: Alessio Usai Date: Wed, 14 Oct 2026 17:58:28 +0200 Subject: [PATCH 5/7] Remove fit function from fun_utils.py Removed the fit function that calculates centroids from training data. --- src/fun_utils.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/fun_utils.py b/src/fun_utils.py index 3a83954..25ef8df 100644 --- a/src/fun_utils.py +++ b/src/fun_utils.py @@ -33,16 +33,5 @@ def split_data(x, y, tr_fraction=0.5): """ pass -def fit(x_train, y_train): - """ - Fit a model to the training data - """ - num_classes = np.unique(y_train).size - num_features = x_train.shape[1] - - centroids = np.zeros(shape=(num_classes,num_features)) - for i in range(num_classes): - centroids[i] = np.mean(x_train[y_train == i], axis=0) - return centroids - \ No newline at end of file + From e52f969600c9a9f4bef074be45bc1ff11c5058f9 Mon Sep 17 00:00:00 2001 From: alessiousai Date: Wed, 14 Oct 2026 18:05:01 +0200 Subject: [PATCH 6/7] definition of fit --- src/classifiers/nmc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/classifiers/nmc.py b/src/classifiers/nmc.py index 6030cf5..5332f04 100644 --- a/src/classifiers/nmc.py +++ b/src/classifiers/nmc.py @@ -35,7 +35,8 @@ def class_labels(self): return self._class_labels def fit(self, xtr, ytr): - pass + self._class_labels = np.unique(ytr) + self._centroids = np.array([np.mean(xtr[ytr == c], axis=0) for c in self._class_labels]) def predict(self, xts): pass From eb588ead0eafa5fd376bddf36b1478174979d333 Mon Sep 17 00:00:00 2001 From: alessiousai Date: Wed, 14 Oct 2026 18:05:52 +0200 Subject: [PATCH 7/7] definition of predict --- src/classifiers/nmc.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/classifiers/nmc.py b/src/classifiers/nmc.py index 5332f04..82426e1 100644 --- a/src/classifiers/nmc.py +++ b/src/classifiers/nmc.py @@ -35,8 +35,16 @@ def class_labels(self): return self._class_labels def fit(self, xtr, ytr): - self._class_labels = np.unique(ytr) - self._centroids = np.array([np.mean(xtr[ytr == c], axis=0) for c in self._class_labels]) + 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