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

K-Nearest Neighbors Regressor.

Performs regression by computing the (weighted) mean of k-nearest neighbor labels.

Summary

Functions

Fits a k-NN regressor model.

Predicts labels using a k-NN regressor model.

Functions

Fits a k-NN regressor model.

Options

  • :algorithm (atom/0) - Algorithm used to compute the k-nearest neighbors. Possible values:

    • :brute - Brute-force search. See Scholar.Neighbors.BruteKNN for more details.

    • :kd_tree - k-d tree. See Scholar.Neighbors.KDTree for more details.

    • :random_projection_forest - Random projection forest. See Scholar.Neighbors.RandomProjectionForest for more details.

    • Module implementing fit(data, opts) and predict(model, query). predict/2 must return tuple containing indices of k-nearest neighbors of query points as well as distances between query points and their k-nearest neighbors.

    The default value is :brute.

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

Algorithm-specific options (e.g. :num_neighbors, :metric) should be provided together with the regressor options.

Examples

iex> x = Nx.tensor([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])
iex> y = Nx.tensor([[1], [2], [3], [4], [5]])
iex> model = Scholar.Neighbors.KNNRegressor.fit(x, y, num_neighbors: 3)
iex> model.algorithm
Scholar.Neighbors.BruteKNN.fit(x, num_neighbors: 3)
iex> model.labels
Nx.tensor([[1], [2], [3], [4], [5]])

iex> x = Nx.tensor([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])
iex> y = Nx.tensor([[1], [2], [3], [4], [5]])
iex> model = Scholar.Neighbors.KNNRegressor.fit(x, y, algorithm: :kd_tree, num_neighbors: 3, metric: {:minkowski, 1})
iex> model.algorithm
Scholar.Neighbors.KDTree.fit(x, num_neighbors: 3, metric: {:minkowski, 1})
iex> model.labels
Nx.tensor([[1], [2], [3], [4], [5]])

iex> x = Nx.tensor([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])
iex> y = Nx.tensor([[1], [2], [3], [4], [5]])
iex> key = Nx.Random.key(12)
iex> model = Scholar.Neighbors.KNNRegressor.fit(x, y, algorithm: :random_projection_forest, num_neighbors: 2, num_trees: 4, key: key)
iex> model.algorithm
Scholar.Neighbors.RandomProjectionForest.fit(x, num_neighbors: 2, num_trees: 4, key: key)
iex> model.labels
Nx.tensor([[1], [2], [3], [4], [5]])

Predicts labels using a k-NN regressor model.

Examples

iex> x_train = Nx.tensor([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])
iex> y_train = Nx.tensor([[1], [2], [3], [4], [5]])
iex> model = Scholar.Neighbors.KNNRegressor.fit(x_train, y_train, num_neighbors: 3)
iex> x = Nx.tensor([[1, 3], [4, 2], [3, 6]])
iex> Scholar.Neighbors.KNNRegressor.predict(model, x)
Nx.tensor([[2.0], [2.0], [4.0]])