Skip to content

Commit

Permalink
euclidean distance and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SalatielBairros committed Jan 2, 2024
1 parent 04732dd commit 7e82ef9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## What's new?

* **Problem**: Problem description
* **Solution**: Solution description
* **Related/Additional changes**: Related/Additional changes description

Closes #
26 changes: 26 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Unit tests execution

on:
push:
branches:
- main

jobs:
tests:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Test with pytest
run: |
pip install pytest
python -m pytest tests/ --disable-warnings
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
numpy
scikit-learn
pandas
polars
4 changes: 4 additions & 0 deletions src/core/distances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import numpy as np

def euclidean_distances(x_train: np.array, x_test_point: np.array):
return np.sqrt(np.sum((x_train - x_test_point) ** 2, axis=1))
Empty file added tests/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions tests/core/test_distances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from unittest import TestCase
import numpy as np
from sklearn.metrics.pairwise import euclidean_distances as sk_ed
from src.core.distances import euclidean_distances

class TestDistances(TestCase):
def test_should_calculate_euclidean_distance(self):
x = np.array([
[0.5, 0.3, 0.23],
[0.4, 0.4, 0.43],
[0.3, 0.5, 0.33]
])

target = np.array([0.3, 0.5, 0.33])

distances = euclidean_distances(x, target)
expected_distances = sk_ed(x, [target])

for distance_actual, distance_expected in zip(distances, expected_distances):
assert round(distance_actual, 4) == round(distance_expected[0], 4)
4 changes: 4 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import unittest

if __name__ == "__main__":
unittest.main()

0 comments on commit 7e82ef9

Please sign in to comment.