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
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
numpy
matplotlib
pandas
scikit-learn
scikit-learn
pytest
7 changes: 6 additions & 1 deletion src/classifiers/nmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ def class_labels(self):
return self._class_labels

def fit(self, xtr, ytr):
pass
self._classes = np.unique(ytr)
num_classes = self._classes.size
self._centroids = np.zeros(shape=(num_classes, xtr.shape[1]))
for k in range(num_classes):
xk = xtr[ytr == self._classes[k], :]
self._centroids[k, :] = np.mean(xk, axis=0)

def predict(self, xts):
pass
37 changes: 18 additions & 19 deletions src/fun_utils.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
from pandas import read_csv
import numpy as np
from sklearn.model_selection import train_test_split

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)


def load_data(filename):
"""
Load data from a csv file

Parameters
----------
filename : string
Filename to be loaded.

Returns
-------
X : ndarray
the data matrix.

y : ndarray
the labels of each sample.
"""
data = read_csv(filename)

data = read_csv(csv_filename)
z = np.array(data)
y = z[:, 0]
X = z[:, 1:]
Expand All @@ -31,4 +26,8 @@ def split_data(x, y, tr_fraction=0.5):
Split the data x, y into two random subsets

"""
pass

Xtr, Xts, ytr, yts = train_test_split(x, y, train_size=0.5, random_state=10, shuffle=True)

return Xtr, ytr, Xts, yts

6 changes: 3 additions & 3 deletions src/tests/test_nmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from os import path
import unittest

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


class TestNMC(unittest.TestCase):
Expand All @@ -12,7 +12,7 @@ def setUp(self):
# Loading MNIST dataset
mnist_path = path.join(
path.dirname(__file__), '..', '../data', 'mnist_data.csv')
x, y = load_data(filename=mnist_path)
x, y = load_mnist(csv_filename=mnist_path)
# Rescale data in 0-1
self.x = x / 255
self.y = y
Expand Down