View Source Scholar.Neighbors.RadiusNNRegressor (Scholar v0.4.0)

The Radius Nearest Neighbors.

It implements regression.

Summary

Functions

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

Makes predictions with the given model on inputs x.

Find the Radius neighbors of a point.

Functions

fit(x, y, opts \\ [])

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

Currently 2D labels are only supported.

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 the value of p parameter (a positive number or :infinity) we can set Manhattan (1), Euclidean (2), Chebyshev (:infinity), or any arbitrary LpL_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.

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.

  • :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.RadiusNNRegressor.fit(x, y, num_classes: 2)
%Scholar.Neighbors.RadiusNNRegressor{
  data: Nx.tensor(
    [
      [1, 2],
      [2, 4],
      [1, 3],
      [2, 5]
    ]
  ),
  labels: Nx.tensor([1, 0, 1, 1]),
  weights: :uniform,
  num_classes: 2,
  metric: &Scholar.Metrics.Distance.pairwise_minkowski/2,
  radius: 1.0
}

predict(model, x)

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.RadiusNNRegressor.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.RadiusNNRegressor.predict(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
#Nx.Tensor<
  f32[2]
  [0.5, 1.0]
>

radius_neighbors(arg1, x)

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.RadiusNNRegressor.fit(x, y, num_classes: 2)
iex> {distances, mask} = Scholar.Neighbors.RadiusNNRegressor.radius_neighbors(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
iex> distances
#Nx.Tensor<
  f32[2][4]
  [
    [2.469818353652954, 0.3162313997745514, 1.5811394453048706, 0.7071067690849304],
    [0.10000114142894745, 2.1931710243225098, 1.0049877166748047, 3.132091760635376]
  ]
>
iex> mask
#Nx.Tensor<
  u8[2][4]
  [
    [0, 1, 0, 1],
    [1, 0, 0, 0]
  ]
>