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.

  • p

    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

    neighborCount

    Number of neighbors to use, default to 5.

    weights

    Weight function used in prediction. Possible values uniform - uniform weighted, distance - weight point by inverse of their distance. Default set to distance.

    p

    The order of the norm of the difference: ||a - b||_p, default set to 2.

  • Fit a K-neighbors regressor model.

    Declaration

    Swift

    public func fit(data: Tensor<Float>, labels: Tensor<Float>)

    Parameters

    data

    Training data with shape [sample count, feature count].

    labels

    Target 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

    distances

    Contains the distance between test data and top neighbors.

    labels

    Contains the value of target.

  • Returns the individual predicted value.

    Declaration

    Swift

    internal func predictSingleSample(_ test: Tensor<Float>) -> Tensor<Float>

    Parameters

    data

    Input data to be regressed.

    Return Value

    Predicted target value.

  • Returns predicted values.

    Declaration

    Swift

    public func prediction(for data: Tensor<Float>) -> Tensor<Float>

    Parameters

    data

    Test 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>) -> Float

    Parameters

    data

    Sample data with shape [sample count, feature count].

    labels

    Target values with shape [sample count].

    Return Value

    The coefficient of determination (R^2) of the prediction.