bake() Function

The bake() function in R is used to apply the transformations from a recipe object to new data. It is a part of the recipes package.

A recipe object is a data structure that represents a pre-processing pipeline. It can be used to transform data in a consistent way. The bake() function can be used to apply the transformations from a recipe object to new data, such as a test set.

The syntax for the bake() function is as follows:

bake(object, newdata, ...)

where object is a recipe object, newdata is a data frame with new data, and ... are any additional arguments.

The bake() function returns a data frame that contains the new data with the transformations applied. The data frame will have the same columns as the new data, but the values in the columns will be the transformed values.

For example, if you have a recipe object that normalizes the data, the bake() function will return a data frame with the normalized values from the new data.

The bake() function is a useful tool for applying transformations to new data. It can be used to ensure that new data is processed in the same way as the training data.

Here is an example of how to use the bake() function:

R




library(recipes)
 
cars_train <- mtcars[1:16,]
cars_test <- mtcars[17:32,]
 
cars_rec <- recipe(mpg ~ ., data = cars_train) %>%
  step_log(disp) %>%
  step_center(all_predictors())
cars_rec
 
# Prep the Recipe object
cars_prep <- prep(cars_rec)
cars_prep
 
# Bake the recipe object on the new data frame that is the testing data
bake(cars_prep, new_data = cars_test)


Output:

# A tibble: 3 x 11
   mpg cyl disp hp drat    wt  qsec vs am gear carb
   <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1  0.0  6  1.60  1.10  3.90  2.58  16.46  0  1  4  4
2  0.0  6  1.60  1.10  3.90  2.76  17.02  0  1  4  4
3  0.0  6  1.60  1.10  3.90  3.15  19.44  1  0  4  4

The output is a data frame with the first few rows of the baked data. The baked data is the data from the new data after it has been normalized using the transformations from the recipe object. The normalization process has transformed the values in the numeric columns to have a mean of 0 and a standard deviation of 1.

Model predictions to find the best model fit using the juice() and bake() functions in R

The juice() function in R Programming Language is used to extract the data from a recipe object. It is a part of the recipes package.

Similar Reads

juice() Function

A recipe object is a data structure that represents a pre-processing pipeline. It can be used to transform data in a consistent way. The juice() function can be used to extract the data from a recipe object so that it can be used for other purposes, such as modeling or visualization....

bake() Function:

...

Model predictions to find the best model fit using the juice() and bake() functions in R

The bake() function in R is used to apply the transformations from a recipe object to new data. It is a part of the recipes package....