Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Applied Supervised Learning with R

You're reading from   Applied Supervised Learning with R Use machine learning libraries of R to build models that solve business problems and predict future trends

Arrow left icon
Product type Paperback
Published in May 2019
Publisher
ISBN-13 9781838556334
Length 502 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Jojo Moolayil Jojo Moolayil
Author Profile Icon Jojo Moolayil
Jojo Moolayil
Karthik Ramasubramanian Karthik Ramasubramanian
Author Profile Icon Karthik Ramasubramanian
Karthik Ramasubramanian
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Applied Supervised Learning with R
Preface
1. R for Advanced Analytics FREE CHAPTER 2. Exploratory Analysis of Data 3. Introduction to Supervised Learning 4. Regression 5. Classification 6. Feature Selection and Dimensionality Reduction 7. Model Improvements 8. Model Deployment 9. Capstone Project - Based on Research Papers Appendix

Chapter 8: Model Deployment


Activity 13: Deploy an R Model Using Plumber

  1. Create a model.r script that will load the required libraries, data, fit a regression model and necessary function to predict on unseen data.

  2. Load the mlbench library that has the data for this activity:

    library(mlbench)
  3. Load BostonHousing data into a DataFrame df:

    data(BostonHousing)
    df<-BostonHousing
  4. Create train dataset using the first 400 rows of df and test with the remaining:

    train <- df[1:400,]
    test <- df[401:dim(df)[1],]
  5. Fit a logistic regression model using the lm function with dependent variable as medv (median value) and 10 independent variables, such as, crim, zn, indus, chas, nox, rm, age, dis, rad, and tax.

    model <- lm(medv~crim+zn+indus+chas+
     nox+rm+age+dis+rad+tax,data=train)
  6. Define a model endpoint as predict_data; this will be used as the API endpoint for Plumber:

    #' @get /predict_data
    function(crim,zn,indus,chas,nox,rm,age,dis,rad,tax){
  7. Within the function, convert the parameters to numeric and factor (since the API call will pass them as string only):

      crim <- as.numeric(crim)
      zn <- as.numeric(zn)
      indus <- as.numeric(indus)
      chas <- as.factor(chas)
      nox <- as.numeric(nox)
      rm <- as.numeric(rm)
      age <- as.numeric(age)
      dis <- as.numeric(dis)
      rad <- as.numeric(rad)
      tax <- as.numeric(tax)
  8. Wrap the 10 independent features for the model as a DataFrame named sample, with the same name for the columns:

      sample <- data.frame(crim  = crim,  zn  = zn,  indus  = indus,  
                           chas  = chas,  nox  = nox,  rm  = rm,  
                           age  = age,  dis  = dis,  rad  = rad,  
                           tax  = tax )
  9. Pass the sample DataFrame to the predict function with the model (created in the 4th step) and return predictions:

      y_pred<-predict(model,newdata=sample)
      
      list(Answer=y_pred)
    }

    The entire model.r file will look like this:

    library(mlbench)
    data(BostonHousing)
    df<-BostonHousing
    train <- df[1:400,]
    test <- df[401:dim(df)[1],]
    
    model <- lm(medv~crim+zn+indus+chas+nox+rm+age+dis+rad+tax,data=train)
    
    #' @get /predict_data
    function(crim,zn,indus,chas,nox,rm,age,dis,rad,tax){
      
      crim <- as.numeric(crim)
      zn <- as.numeric(zn)
      indus <- as.numeric(indus)
      chas <- as.factor(chas)
      nox <- as.numeric(nox)
      rm <- as.numeric(rm)
      age <- as.numeric(age)
      dis <- as.numeric(dis)
      rad <- as.numeric(rad)
      tax <- as.numeric(tax)
      
      sample <- data.frame(crim  = crim,  zn  = zn,  indus  = indus,  
                           chas  = chas,  nox  = nox,  rm  = rm,  
                           age  = age,  dis  = dis,  rad  = rad,  
                           tax  = tax )
      
      y_pred<-predict(model,newdata=sample)
      
      list(Answer=y_pred)
    }
  10. Load the plumber library.

    library(plumber)
  11. Create a plumber object using the plumb function and pass the model.r file (created in part 1).

    r <- plumb(model.r)
  12. Run the plumber object by passing the hostname as localhost or 127.0.0.1 and a port, say 8080.

    http://127.0.0.1:8080/
  13. Test the deployed model using the browser or Postman and invoke the API.

    API invoke:

    http://127.0.0.1:8080/predict_

    ata?crim=0.01&zn=18&indus=2.3&chas=0&nox=0.5&rm=6&

    age=65&dis=4&rad=1&tax=242

    {"Answer":[22.5813]}
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image