How to load a R model?

This recipe helps you load a R model

Recipe Objective

Once we have trained a model and tested it's performance to be satisfactory, we should save the model. The trained model is lost as soon as we close the session. Additionally, with large dataset, training a model is quite time-consuming since you have to run the algorithm again and again. Hence, it is ideal to train and save the model which can be loaded later to predict the outcome on the new dataset. ​

In this recipe, we will demonstrate how to load an existing Regression Tree model and predict the test values ​

Learn About the Application of ARCH and GARCH models in Real-World

STEP 1: Loading the model

There are two ways to save and load the model: ​

  1. using save(), load(): When we use save(), we will have to load it using the same name.
  2. using saveRDS(), loadRDS(): saveRDS() does not save the model name and we have the flexibilty to load the model in any other name. Bur saveRDS() can only save one object at a time as it is lower-level function.

Most people prefer saveRDS() over save() as it is serialise the object. ​

Syntax: readRDS(file =) ​

where: file = path with the file extension .rda ​

#loading the model model_regression_Tree = readRDS("C:/Users/Divit/Desktop/Internship/R-recipes_Jan/R_160 onwards/Decision Tree_classifier/model.rda") #checking whether the model has been loaded with different name ls()

STEP 2: Load the test dataset

# For data manipulation library(tidyverse) # Install readxl R package for reading excel sheets install.packages("readxl") library("readxl") #reading the dataset test = read_excel('R_254_df_test_regression.xlsx') # scaling the dataset as the model was built on scaled data test_scaled = scale(test[2:6]) # using cbind() function to add a new column Outcome to the scaled independent values test_scaled = data.frame(cbind(test_scaled, Outcome = test$Cost)) glimpse(test_scaled)

Rows: 32
Columns: 6
$ Weight   0.72483012, 0.07204194, 0.17201851, 0.23082825, 0.35432872,...
$ Weight1  0.72445274, 0.08459639, 0.17756697, 0.23225555, 0.35803927,...
$ Length   0.69959684, 0.09077507, 0.24556027, 0.29715533, 0.34875040,...
$ Height   2.15715925, 0.03471101, 0.07758442, 0.14769072, 0.25564092,...
$ Width    1.87080937, -0.06904068, 0.29059599, 0.39466263, 0.22707121...
$ Outcome  1000.0, 200.0, 300.0, 300.0, 300.0, 430.0, 345.0, 456.0, 51...

STEP 3: Predict cost from test dataset

We use Predict() function to do the same.

Syntax: predict(fitted_model, df, type = '')

where:

  1. fitted_model = model fitted by train dataset
  2. df = test dataset

predict_test = predict(model_regression_Tree, test_scaled) predict_test %>% head()

1 700.909090909091
2 316.625
3 316.625
4 316.625
5 495.9
6 495.9

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Learn How to Build a Logistic Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple logistic regression model in PyTorch for customer churn prediction.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Multi-Class Text Classification with Deep Learning using BERT
In this deep learning project, you will implement one of the most popular state of the art Transformer models, BERT for Multi-Class Text Classification