KNeighborsClassifier
public class KNeighborsClassifier
K-neighbors classifier.
Classifier implementing the k-nearest neighbors vote.
-
The order of the norm of the difference:
||a - b||_p.Declaration
Swift
var p: Int -
Weight function used in prediction.
Declaration
Swift
public var weights: String -
Number of neighbors.
Declaration
Swift
var neighborCount: Int -
The training data.
Declaration
Swift
var data: Tensor<Float> -
The target class correspoing to training data.
Declaration
Swift
var labels: Tensor<Int32> -
Create a K neighbors classifier model.
Declaration
Swift
public init( neighborCount: Int = 5, weights: String = "distance", p: Int = 2 )Parameters
neighborCountNumber of neighbors to use, default to
5.weightsWeight function used in prediction. Possible values
uniform- uniform weighted,distance- weight point by inverse of their distance. Default set todistance.pThe order of the norm of the difference:
||a - b||_p, default set to2. -
Fit a K-neighbors classifier model.
Declaration
Swift
public func fit(data: Tensor<Float>, labels: Tensor<Int32>)Parameters
dataTraining data with shape
[sample count, feature count].labelsTarget value with shape
[sample count]. -
Returns the weights of each neighbor.
Declaration
Swift
internal func computeWeights( distances: Tensor<Float>, labels: Tensor<Float> ) -> Tensor<Float>Parameters
distancesContains the distance between test data and top neighbors.
labelsContains the classes of neighbors.
-
Returns the predicted classification.
Declaration
Swift
internal func predictSingleSample(_ test: Tensor<Float>) -> Tensor<Int32>Parameters
testInput data to be classified.
Return Value
Predicted classification.
-
Returns classification.
Declaration
Swift
public func prediction(for data: Tensor<Float>) -> Tensor<Int32>Parameters
dataPrediction data with shape
[sample count, feature count].Return Value
Classification for test data.
-
Returns the mean accuracy.
Declaration
Swift
public func score(data: Tensor<Float>, labels: Tensor<Int32>) -> FloatParameters
dataSample data with shape
[sample count, feature count].labelsTarget label with shape
[sample count].Return Value
Returns the mean accuracy on the given test data and labels.
KNeighborsClassifier Class Reference