View Source Scholar.Neighbors.RadiusNearestNeighbors (Scholar v0.3.0)

The Radius Nearest Neighbors.

It implements both classification and regression.

Summary

Functions

Fit the Radius nearest neighbors classifier from the training data set.

Makes predictions with the given model on inputs x.

Return probability estimates for the test data x.

Find the Radius neighbors of a point.

Functions

Fit the Radius nearest neighbors classifier from the training data set.

For classification, provided labels need to be consecutive non-negative integers. If your labels does not meet this condition please use Scholar.Preprocessing.ordinal_encode

Currently 2D labels are only supported for regression tasks.

Options

  • :radius - Radius of neighborhood The default value is 1.0.

  • :num_classes (pos_integer/0) - Number of classes in provided labels

  • :weights - Weight function used in prediction. Possible values:

    • :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.

    The default value is :uniform.

  • :metric - The function that measures the pairwise distance between two points. Possible values:

    • {:minkowski, p} - Minkowski metric. By changing value of p parameter (a positive number or :infinity) we can set Manhattan (1), Euclidean (2), Chebyshev (:infinity), or any arbitrary $L_p$ metric.

    • :cosine - Cosine metric.

    • Anonymous function of arity 2 that takes two rank-2 tensors.

    The default value is &Scholar.Metrics.Distance.pairwise_minkowski/2.

  • :task - Task that will be performed using Radius Nearest Neighbors. Possible values:

    • :classification - Classifier implementing the Radius Nearest Neighbors vote.

    • :regression - Regression based on Radius Nearest Neighbors. The target is predicted by local interpolation of the targets associated of the nearest neighbors in the training set.

    The default value is :classification.

Return Values

The function returns a struct with the following parameters:

  • :data - Training data.

  • :labels - Labels of each point.

  • :weights - Weight function used in prediction.

  • :num_classes - Number of classes in provided labels.

  • :task - Task that will be performed using Radius Nearest Neighbors. For :classification task, model will be a classifier implementing the Radius Nearest Neighbors vote. For :regression task, model is a regressor based on Radius Nearest Neighbors.

  • :metric - The metric function used.

  • :radius - Radius of neighborhood.

Examples

iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> Scholar.Neighbors.RadiusNearestNeighbors.fit(x, y, num_classes: 2)
%Scholar.Neighbors.RadiusNearestNeighbors{
  data: Nx.tensor(
    [
      [1, 2],
      [2, 4],
      [1, 3],
      [2, 5]
    ]
  ),
  labels: Nx.tensor(
    [1, 0, 1, 1]
  ),
  weights: :uniform,
  num_classes: 2,
  task: :classification,
  metric: &Scholar.Metrics.Distance.pairwise_minkowski/2,
  radius: 1.0
}

Makes predictions with the given model on inputs x.

Return Values

It returns a tensor with predicted class labels.

Examples

iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RadiusNearestNeighbors.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RadiusNearestNeighbors.predict(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
Nx.tensor(
  [0, 1]
)
Link to this function

predict_probability(model, x)

View Source

Return probability estimates for the test data x.

Return Values

It returns a typle with tensor with probabilities of classes and mask of outliers. They are arranged in lexicographic order.

Examples

iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RadiusNearestNeighbors.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RadiusNearestNeighbors.predict_probability(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
{Nx.tensor(
  [
    [0.5, 0.5],
    [0.0, 1.0]
  ]
),
Nx.tensor(
  [0, 0], type: :u8
)}
Link to this function

radius_neighbors(arg1, x)

View Source

Find the Radius neighbors of a point.

Return Values

Returns indices of the selected neighbor points as a mask (1 if a point is a neighbor, 0 otherwise) and their respective distances.

Examples

iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> model = Scholar.Neighbors.RadiusNearestNeighbors.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RadiusNearestNeighbors.radius_neighbors(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
{Nx.tensor(
  [
    [2.469818353652954, 0.3162313997745514, 1.5811394453048706, 0.7071067690849304],
    [0.10000114142894745, 2.1931710243225098, 1.0049877166748047, 3.132091760635376]
  ]
),
Nx.tensor(
  [
    [0, 1, 0, 1],
    [1, 0, 0, 0]
  ], type: :u8
)}