torchml.neighbors

Classes

torchml.neighbors.NearestNeighbors

Description

Unsupervised learner for implementing neighbor searches.

Implementation of scikit-learn's nearest neighbors APIs using PyTorch.

References
  1. Fix, E. and Hodges, J.L. (1951) Discriminatory Analysis, Nonparametric Discrimination: Consistency Properties. Technical Report 4, USAF School of Aviation Medicine, Randolph Field.
  2. MIT 6.034 Artificial Intelligence, Fall 2010, 10. Introduction to Learning, Nearest Neighbors
  3. The scikit-learn documentation page for nearest neighbors.
  4. Referenced Implementation
Arguments
  • n_neighbors (int, default=5): Number of neighbors to use by default for kneighbors queries.

  • radius (float, default=1.0): Range of parameter space to use by default for radius_neighbors queries.

  • algorithm (string, default=’auto’): Dummy variable to mimic the sklearn API

  • leaf_size (int, default=30): Dummy variable to mimic the sklearn API

  • metric (str or callable, default=’minkowski’): No metric supprt right now, dummy variable and always minkowski

  • p (int, default=2): No metric supprt right now, dummy variable and always 2

  • metric_paramsdict (default=None): Dummy variable to mimic the sklearn API

  • n_jobs (int, default=None): Dummy variable to mimic the sklearn API

Example
import numpy as np
import torchml as ml
samples = np.array([[1, 0, 0], [0, 1, 0], [0, 5, 0], [20, 50, 30]])
point = np.array([[20, 50, 1]])
neigh = ml.neighbors.NearestNeighbors(n_neighbors=3)
neigh.fit(torch.from_numpy(samples))
neigh.kneighbors(torch.from_numpy(point))

fit(self, X: Tensor, y = None)

Description

Initialize the class with training sets

Arguments
  • X (torch.Tensor): the training set
  • y (torch.Tensor, default=None): dummy variable used to maintain the scikit-learn API consistency

kneighbors(self, X: Tensor, n_neighbors = None, return_distance = True) -> <built-in function any>

Description

Computes the knearest neighbors and returns those k neighbors

Arguments
  • X (torch.Tensor): the target point
  • n_neighbors (int, default=None): optional argument to respecify the parameter k in k nearest neighbors
  • return_distance (bool, default=True): returns the distances to the neighbors if true

torchml.neighbors.NearestCentroid

Description

Implementation of scikit-learn's Nearest centroid APIs using pytorch. Euclidean metric by default.

References
  1. The scikit-learn [documentation page] (https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestCentroid.html) for nearest centroids.
  2. M. Thulasidas, "Nearest Centroid: A Bridge between Statistics and Machine Learning," 2020 IEEE International Conference on Teaching, Assessment, and Learning for Engineering (TALE), 2020, pp. 9-16, doi: 10.1109/TALE48869.2020.9368396.
Arguments
  • metric (str or callable, default="euclidean"): Metric to use for distance computation. Only Euclidiean metric is supported for now.

  • shrink_threshold (float, default=None): Threshold for shrinking centroids to remove features. Not supported for now.

Example
import numpy as np
import torchml as ml
X = np.array([[3,2],[1,8],[3,5],[9,7],[7,7]])
y = np.array([9,1,9,8,3])
samp = np.array([[6,1],[8,9],[5,3],[8,2],[9,8]])
torchX = torch.from_numpy(X)
torchy = torch.from_numpy(y)
centroid = ml.neighbors.NearestCentroid()
centroid.fit(torchX,torchy)
output = centroid.predict(torch.from_numpy(samp)).numpy()

fit(self, X: Tensor, y: Tensor)

Description

Fit the NearestCentroid model according to the given training data.

Arguments
  • X (torch.Tensor): array-like, sparse matrix of shape (n_samples, n_features) Training vector, where n_samples is the number of samples and n_features is the number of features
  • y (torch.Tensor): array-like of shape (n_samples,) Target values

predict(self, X: <built-in method tensor of type object at 0x10e8643b0>) -> <built-in method tensor of type object at 0x10e8643b0>

Description

Computes the classes of the sample data

Arguments
  • X (torch.Tensor): the sample data, each with n-features
Return
  • (torch.Tensor): the predicted classes

torchml.neighbors.KNeighborsClassifier

Description

Unsupervised learner for implementing KNN Classifier.

References
  1. Fix, E. and Hodges, J.L. (1951) Discriminatory Analysis, Nonparametric Discrimination: Consistency Properties. Technical Report 4, USAF School of Aviation Medicine, Randolph Field.
  2. MIT 6.034 Artificial Intelligence, Fall 2010, 10. Introduction to Learning, Nearest Neighbors
  3. The scikit-learn documentation page for KNeighborsClassifier.
Arguments
  • n_neighbors (int, default=5): Number of neighbors to use by default for kneighbors queries.

  • weights (str {'uniform', 'distance'} or callable, default='uniform'):

    • 'uniform' : uniform weights. All points in each neighborhood are weighted equally.
    • 'distance' : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away.
    • [callable] : not implemented yet
  • algorithm (str, default='auto'): Dummy variable to keep consistency with SKlearn's API, always 'brute' for now.

  • leaf_size (int, default=30) Dummy variable to keep consistency with SKlearn's API.

  • metric (str or callable, default=’minkowski’): No metric supprt right now, dummy variable and always minkowski

  • p (int, default=2): No metric supprt right now, dummy variable and always 2

  • metric_paramsdict (default=None): Dummy variable to mimic the sklearn API

  • n_jobs (int, default=None): Dummy variable to mimic the sklearn API

Example
import numpy as np
import torchml as ml
samples = np.array([[0], [1], [2], [3]])
y = np.array([0, 0, 1, 1])
point = np.array([1.1])
neigh = ml.neighbors.KNeighborsClassifier(n_neighbors=3)
neigh.fit(torch.from_numpy(samples), torch.from_numpy(y))
neigh.predict(torch.from_numpy(point))
neigh.predict_proba(torch.from_numpy(point))

fit(self, X: Tensor, y: Tensor)

Description

Initialize the class with training sets

Arguments
  • X (torch.Tensor): the training set
  • y (torch.Tensor, default=None): dummy variable used to maintain the scikit-learn API consistency

kneighbors(self, X: Tensor = None, n_neighbors: int = None, return_distance: bool = True) -> Any

Description

Computes the knearest neighbors and returns those k neighbors

Arguments
  • X (torch.Tensor): the target point
  • n_neighbors (int, default=None): optional argument to respecify the parameter k in k nearest neighbors
  • return_distance (bool, default=True): returns the distances to the neighbors if true

predict(self, X: Tensor) -> Tensor

Description

Predict the class labels for the provided data.

Arguments
  • X (torch.Tensor): the target point

predict_proba(self, X: Tensor) -> Tensor

Description

Return probability estimates for the test data X.

Arguments
  • X (torch.Tensor): the target point