RandomForestClassifier {less}R Documentation

RandomForestClassifier

Description

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

Value

R6 Class of RandomForestClassifier

Super classes

less::BaseEstimator -> less::SklearnEstimator -> RandomForestClassifier

Methods

Public methods

Inherited methods

Method new()

Creates a new instance of R6 Class of RandomForestClassifier

Usage
RandomForestClassifier$new(
  n_estimators = 100,
  random_state = NULL,
  min_samples_leaf = 1,
  max_leaf_nodes = NULL
)
Arguments
n_estimators

Number of trees to grow. This should not be set to too small a number, to ensure that every input row gets predicted at least a few times (defaults to 100).

random_state

Seed number to be used for fixing the randomness (default to NULL).

min_samples_leaf

Minimum size of terminal nodes. Setting this number larger causes smaller trees to be grown (and thus take less time) (defaults to 1)

max_leaf_nodes

Maximum number of terminal nodes trees in the forest can have. If not given, trees are grown to the maximum possible (subject to limits by nodesize). If set larger than maximum possible, a warning is issued. (defaults to NULL)

Examples
rf <- RandomForestClassifier$new()
rf <- RandomForestClassifier$new(n_estimators = 500)
rf <- RandomForestClassifier$new(n_estimators = 500, random_state = 100)

Method fit()

Builds a random forest regressor from the training set (X, y).

Usage
RandomForestClassifier$fit(X, y)
Arguments
X

2D matrix or dataframe that includes predictors

y

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

Returns

Fitted R6 Class of RandomForestClassifier

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

rf <- RandomForestClassifier$new()
rf$fit(X_train, y_train)

Method predict()

Predict regression value for X0.

Usage
RandomForestClassifier$predict(X0)
Arguments
X0

2D matrix or dataframe that includes predictors

Returns

Factor of the predict classes.

Examples
rf <- RandomForestClassifier$new()
rf$fit(X_train, y_train)
preds <- rf$predict(X_test)

rf <- RandomForestClassifier$new()
preds <- rf$fit(X_train, y_train)$predict(X_test)

preds <- RandomForestClassifier$new()$fit(X_train, y_train)$predict(X_test)
print(caret::confusionMatrix(data=preds, reference = factor(y_test)))

Method get_estimator_type()

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

Usage
RandomForestClassifier$get_estimator_type()
Examples
rf$get_estimator_type()

Method clone()

The objects of this class are cloneable with this method.

Usage
RandomForestClassifier$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

See Also

randomForest::randomForest()

Examples


## ------------------------------------------------
## Method `RandomForestClassifier$new`
## ------------------------------------------------

rf <- RandomForestClassifier$new()
rf <- RandomForestClassifier$new(n_estimators = 500)
rf <- RandomForestClassifier$new(n_estimators = 500, random_state = 100)

## ------------------------------------------------
## Method `RandomForestClassifier$fit`
## ------------------------------------------------

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

rf <- RandomForestClassifier$new()
rf$fit(X_train, y_train)

## ------------------------------------------------
## Method `RandomForestClassifier$predict`
## ------------------------------------------------

rf <- RandomForestClassifier$new()
rf$fit(X_train, y_train)
preds <- rf$predict(X_test)

rf <- RandomForestClassifier$new()
preds <- rf$fit(X_train, y_train)$predict(X_test)

preds <- RandomForestClassifier$new()$fit(X_train, y_train)$predict(X_test)
print(caret::confusionMatrix(data=preds, reference = factor(y_test)))

## ------------------------------------------------
## Method `RandomForestClassifier$get_estimator_type`
## ------------------------------------------------

rf$get_estimator_type()

[Package less version 0.1.0 Index]