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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

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

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data