KNeighborsRegressor
public class KNeighborsRegressor
K neighbors regressor.
The target is predicted by local interpolation of the targets associated of the nearest neighbors in the training set.
-
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 value correspoing to training data.
Declaration
Swift
var labels: Tensor<Float> -
Create a K neighbors regressor 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 regressor model.
Declaration
Swift
public func fit(data: Tensor<Float>, labels: Tensor<Float>)Parameters
dataTraining data with shape
[sample count, feature count].labelsTarget value with shape
[sample count]. -
Returns the average value of target.
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 value of target.
-
Returns the individual predicted value.
Declaration
Swift
internal func predictSingleSample(_ test: Tensor<Float>) -> Tensor<Float>Parameters
dataInput data to be regressed.
Return Value
Predicted target value.
-
Returns predicted values.
Declaration
Swift
public func prediction(for data: Tensor<Float>) -> Tensor<Float>Parameters
dataTest data with shape
[sample count, feature count].Return Value
Predicted value tensor.
-
Returns the coefficient of determination (
R^2) of the prediction.Declaration
Swift
public func score(data: Tensor<Float>, labels: Tensor<Float>) -> FloatParameters
dataSample data with shape
[sample count, feature count].labelsTarget values with shape
[sample count].Return Value
The coefficient of determination (
R^2) of the prediction.
KNeighborsRegressor Class Reference