How to load a R model?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to load a R model?

How to load a R model?

This recipe helps you load a R model

0

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 ​

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

Relevant Projects

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.