[HOTFIX] Add missing iris.py and fix knnc bug
This commit is contained in:
parent
09256956f3
commit
b6145223c8
15
prototorch/datasets/iris.py
Normal file
15
prototorch/datasets/iris.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
"""Thin wrapper for the Iris classification dataset from sklearn.
|
||||||
|
|
||||||
|
URL:
|
||||||
|
https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_iris.html
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from prototorch.datasets.abstract import NumpyDataset
|
||||||
|
from sklearn.datasets import load_iris
|
||||||
|
|
||||||
|
|
||||||
|
class Iris(NumpyDataset):
|
||||||
|
def __init__(self):
|
||||||
|
x, y = load_iris(return_X_y=True)
|
||||||
|
super().__init__(x, y)
|
@ -38,6 +38,7 @@ def wtac(distances, labels):
|
|||||||
|
|
||||||
def knnc(distances, labels, k=1):
|
def knnc(distances, labels, k=1):
|
||||||
winning_indices = torch.topk(-distances, k=k, dim=1).indices
|
winning_indices = torch.topk(-distances, k=k, dim=1).indices
|
||||||
winning_labels = torch.mode(labels[winning_indices].squeeze(),
|
# winning_labels = torch.mode(labels[winning_indices].squeeze(),
|
||||||
dim=1).values
|
# dim=1).values
|
||||||
|
winning_labels = torch.mode(labels[winning_indices], dim=1).values
|
||||||
return winning_labels
|
return winning_labels
|
||||||
|
@ -4,7 +4,6 @@ import unittest
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
from prototorch.functions import (activations, competitions, distances,
|
from prototorch.functions import (activations, competitions, distances,
|
||||||
initializers, losses)
|
initializers, losses)
|
||||||
|
|
||||||
@ -139,7 +138,7 @@ class TestCompetitions(unittest.TestCase):
|
|||||||
def test_knnc_k1(self):
|
def test_knnc_k1(self):
|
||||||
d = torch.tensor([[2.0, 3.0, 1.99, 3.01], [2.0, 3.0, 2.01, 3.0]])
|
d = torch.tensor([[2.0, 3.0, 1.99, 3.01], [2.0, 3.0, 2.01, 3.0]])
|
||||||
labels = torch.tensor([0, 1, 2, 3])
|
labels = torch.tensor([0, 1, 2, 3])
|
||||||
actual = competitions.knnc(d, labels, k=torch.tensor([1]))
|
actual = competitions.knnc(d, labels, k=1)
|
||||||
desired = torch.tensor([2, 0])
|
desired = torch.tensor([2, 0])
|
||||||
mismatch = np.testing.assert_array_almost_equal(actual,
|
mismatch = np.testing.assert_array_almost_equal(actual,
|
||||||
desired,
|
desired,
|
||||||
|
Loading…
Reference in New Issue
Block a user