Learn R Programming

less (version 0.1.0)

KNeighborsRegressor: KNeighborsRegressor

Description

Wrapper R6 Class of caret::knnreg function that can be used for LESSRegressor and LESSClassifier

Arguments

Value

R6 Class of KNeighborsRegressor

Super classes

less::BaseEstimator -> less::SklearnEstimator -> KNeighborsRegressor

Methods

Inherited methods


Method new()

Creates a new instance of R6 Class of KNeighborsRegressor

Usage

KNeighborsRegressor$new(k = 5)

Arguments

k

Number of neighbors considered (defaults to 5).

Examples

knr <- KNeighborsRegressor$new()
knr <- KNeighborsRegressor$new(k = 5)


Method fit()

Fit the k-nearest neighbors regressor from the training set (X, y).

Usage

KNeighborsRegressor$fit(X, y)

Arguments

X

2D matrix or dataframe that includes predictors

y

1D vector or (n,1) dimensional matrix/dataframe that includes response variables

Returns

Fitted R6 Class of KNeighborsRegressor

Examples

data(abalone)
split_list <- train_test_split(abalone[1:100,], test_size =  0.3)
X_train <- split_list[[1]]
X_test <- split_list[[2]]
y_train <- split_list[[3]]
y_test <- split_list[[4]]

knr <- KNeighborsRegressor$new() knr$fit(X_train, y_train)


Method predict()

Predict regression value for X0.

Usage

KNeighborsRegressor$predict(X0)

Arguments

X0

2D matrix or dataframe that includes predictors

Returns

The predict values.

Examples

knr <- KNeighborsRegressor$new()
knr$fit(X_train, y_train)
preds <- knr$predict(X_test)

knr <- KNeighborsRegressor$new() preds <- knr$fit(X_train, y_train)$predict(X_test)

preds <- KNeighborsRegressor$new()$fit(X_train, y_train)$predict(X_test) print(head(matrix(c(y_test, preds), ncol = 2, dimnames = (list(NULL, c("True", "Prediction"))))))


Method get_estimator_type()

Auxiliary function returning the estimator type e.g 'regressor', 'classifier'

Usage

KNeighborsRegressor$get_estimator_type()

Examples

knr$get_estimator_type()


Method clone()

The objects of this class are cloneable with this method.

Usage

KNeighborsRegressor$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

See Also

Examples

Run this code

## ------------------------------------------------
## Method `KNeighborsRegressor$new`
## ------------------------------------------------

knr <- KNeighborsRegressor$new()
knr <- KNeighborsRegressor$new(k = 5)

## ------------------------------------------------
## Method `KNeighborsRegressor$fit`
## ------------------------------------------------

data(abalone)
split_list <- train_test_split(abalone[1:100,], test_size =  0.3)
X_train <- split_list[[1]]
X_test <- split_list[[2]]
y_train <- split_list[[3]]
y_test <- split_list[[4]]

knr <- KNeighborsRegressor$new()
knr$fit(X_train, y_train)

## ------------------------------------------------
## Method `KNeighborsRegressor$predict`
## ------------------------------------------------

knr <- KNeighborsRegressor$new()
knr$fit(X_train, y_train)
preds <- knr$predict(X_test)

knr <- KNeighborsRegressor$new()
preds <- knr$fit(X_train, y_train)$predict(X_test)

preds <- KNeighborsRegressor$new()$fit(X_train, y_train)$predict(X_test)
print(head(matrix(c(y_test, preds), ncol = 2, dimnames = (list(NULL, c("True", "Prediction"))))))

## ------------------------------------------------
## Method `KNeighborsRegressor$get_estimator_type`
## ------------------------------------------------

knr$get_estimator_type()

Run the code above in your browser using DataLab