View Source Scholar.Neighbors.KNearestNeighbors (Scholar v0.2.1)
The K-Nearest Neighbors. It implements both classification and regression.
Summary
Functions
Fit the K-nearest neighbors classifier from the training data set.
Find the K-neighbors of a point.
Makes predictions with the given model
on inputs x
.
Return probability estimates for the test data x
.
Functions
Fit the K-nearest neighbors classifier from the training data set.
For classification, provided labels needs to be consecutive natural numbers. If your labels does
not meet this condition please use Scholar.Preprocessing.ordinal_encode
Currently 2D labels only supported for regression task.
Options
:num_neighbors
(pos_integer/0
) - The number of neighbors to use by default fork_neighbors
queries The default value is5
.: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
- Name of the metric. Possible values:{:minkowski, p}
- Minkowski metric. By changing value ofp
parameter (a positive number or:infinity
) we can set Manhattan (1
), Euclidean (2
), Chebyshev (:infinity
), or any arbitrary $L_p$ metric.:cosine
- Cosine metric.
The default value is
{:minkowski, 2}
.:task
- Task that will be performed using K Nearest Neighbors. Possible values::classification
- Classifier implementing the k-nearest neighbors vote.:regression
- Regression based on K-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.:default_num_neighbors
- The number of neighbors to use by default fork_neighbors
queries.:weights
- Weight function used in prediction.:num_classes
- Number of classes in provided labels.:task
- Task that will be performed using K-Nearest Neighbors. For:classification
task, model will be a classifier implementing the K-Nearest Neighbors vote. For:regression
task, model is a regressor based on K-Nearest Neighbors.:metric
- Name of the metric.
Examples
iex> x = Nx.tensor([[1, 2], [2, 4], [1, 3], [2, 5]])
iex> y = Nx.tensor([1, 0, 1, 1])
iex> Scholar.Neighbors.KNearestNeighbors.fit(x, y, num_classes: 2)
%Scholar.Neighbors.KNearestNeighbors{
data: Nx.tensor(
[
[1, 2],
[2, 4],
[1, 3],
[2, 5]
]
),
labels: Nx.tensor(
[1, 0, 1, 1]
),
default_num_neighbors: 4,
weights: :uniform,
num_classes: 2,
task: :classification,
metric: {:minkowski, 2}
}
Find the K-neighbors of a point.
Return Values
Returns indices of and distances to the neighbors of each point.
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.KNearestNeighbors.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.KNearestNeighbors.k_neighbors(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
{Nx.tensor(
[
[0.3162279427051544, 0.7071065902709961, 1.5811389684677124, 2.469817876815796],
[0.10000002384185791, 1.0049875974655151, 2.193171262741089, 3.132091760635376]
]
),
Nx.tensor(
[
[1, 3, 2, 0],
[0, 2, 1, 3]
]
)}
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.KNearestNeighbors.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.KNearestNeighbors.predict(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
Nx.tensor(
[1, 1], type: :s64
)
Return probability estimates for the test data x
.
Return Values
It returns a tensor with probabilities of classes. 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.KNearestNeighbors.fit(x, y, num_classes: 2)
iex> Scholar.Neighbors.KNearestNeighbors.predict_proba(model, Nx.tensor([[1.9, 4.3], [1.1, 2.0]]))
Nx.tensor(
[
[0.75, 0.25],
[0.75, 0.25]
]
)