Deploying Machine Learning Models with Watson Machine Learning || Python Machine Learning PT.4
Skills:
ML Pipelines90%
Key Takeaways
Deploys a machine learning model as a REST API using Watson Machine Learning and Python
Full Transcript
ever wanted to use your machine learning model inside another application that's where deployment comes in today we're going to be taking a look at how to deploy your scikit-loan or python machine learning model so that you can use it in a whole wide range of use cases so up until now in this series we've covered how to build a machine learning model using pandas python and scikit-learn but now we're getting into the nitty-gritty and we're taking a look at how we can deploy our machine learning model to use it in other applications so deployment is really important because it allows you to hook into other things so say we wanted to build a react app or use node.js to interact with our api that's where deployment comes in so specifically today we're going to be taking a look at how we can deploy our machine learning model using watson machine learning so we'll cover how to set up what's in machine learning really really easily how we can deploy it with just a couple of lines of python code and last but not least how we can perform scoring using that deployed rest api endpoint so this is going to mean that later on when we get into the next part of the series where we start building our machine learning app we're going to be able to use this deployed endpoint in virtually any application so when we build our machine learning app we'll be building it using node.js and react and even though we've deployed our machine learning model in python we're going to be able to work with it in virtually any other language ready to get to it let's do it alright so we finished our last session by building our machine learning model but it doesn't end there oh no no no in this video we're going to be going through how to deploy our machine learning model so that we can start to use it to integrate with other applications so today we're going to be using watson machine learning to deploy our model as a rest api this basically means that we're going to then be able to use it inside of our node and react app and you can start to hook it into anything that uses apis so let's go on ahead and get started now as i said we're going to be using watson machine learning so first up we need to create a watson machine learning service to do that all we need to do is go head on over to ibm cloud so you can access that from cloud.ibm.com select catalog go to services and then choose ai and machine learning so there's a whole bunch of ai services here what we're really focused on is machine learning so this is the service that's going to allow us to deploy our machine learning model as a rest api now there's a bunch of plans here but just stick with light there's more than enough capacity there if you're just playing around and getting started so i think you get up to 5 000 predictions per month and you can deploy about five machine learning models so we're just going to choose light and then hit create perfect that's all done so now what we actually need to do in order to use our machine learning service is generate some credentials so these credentials are going to allow us to access our watson machine learning service from our local jupyter notebook so what we can do is just go to service credentials create new credential and we're just going to call it python ml then what we need to do is change the role from writer to manager and hit add so this will create a new credential and you can see that's set up now all we need to do is open that up and copy these details perfect so that's really the machine learning service done from a ibm cloud perspective now what we can do is start to work in our jupyter notebook again so in this particular case what we're going to need to do is just create a dictionary to store these credentials so we're just going to call it creds and we're going to paste in our credentials there perfect alrighty cool so what we'll do now is start to install some of our dependencies there's really only one key dependency that you need and that's the watson machine learning api client so we can install this from our notebooks using the pip install command so let's go and do that all right so it looks like i've already got it installed it might take a little bit longer if you're installing it for the first time but remember all you need to do is put an exclamation mark and then run pip install and whatever the dependency that you need inside of a notebook to install from a notebook so here we've just installed the watson machine learning client dependency or library now what we're going to do is actually import that into our notebook so we can import the watson machine learning api client from this particular package using our regular import commands so let's do that alrighty cool so we've now imported our dependencies so we've imported what's in machine learning api client in order to use that client and specifically to use our what's a machine learning service we need to create a new instance or a connection to this client so what we can actually do is create a new instance of our watson machine learning api class and pass through our credentials to connect up to it so let's go and do that so we're going to store our connection to the watson machine learning api client inside of another variable called wml client so this just stands for what's a machine learning client so we've created a or we've started to create a new instance now what we're going to do is just pass our credentials to our watson machine learning api client and run that perfect so we've got our client now what we're going to do is set up some constants for our machine learning model so specifically we're going to create a variable for our model name our deployment name and we're also going to specify which of our models is our best model we've already specified that up here but we're going to put it in all caps just so we're consistent with our naming conventions perfect so we've set up our model constants so you can see that we've named our model transaction forecast we've named our deployment transaction forecast deployment we're getting verbosia and we've also specified our best model variable to equal our best model which is our random forest model that was the one that performed best when we trained it last time now what we need to do is start to specify our model schema so this is just the format that watson machine learning needs your machine learning model in or some of the schema details in in order to load it up into our watson machine learning service so let's go on ahead and do that perfect so that's our model schema setup so we've just specified the model meta name and specifically the name component of that there's a bunch of other details you can pass through for now we're just doing sort of the bare minimum right now that that's done we can go and save our model so there's two key parts to deploying a machine learning model using what's in machine learning so we actually first save our model into what's in machine learning so this is actually similar to taking a random forest model and just putting it up there and then second we actually take that model and we deploy it as a rest api so that's the second phase so we're about to do that first phase where we take our model and store it into what's in machine learning now the output of this is we'll get a unique identifier or uid this is going to allow us to reference our deployed or our saved model so in order to save our model into what's in machine learning we're going to be using the store model method this takes a couple of parameters specifically the model that we want to pass up our training and our target data as well as our model pipeline so let's go ahead and push those details in perfect so we've now gone and run that cell so that's basically gone and taken our model and actually published it up into what's in machine learning so now what we're going to do is extract that unique identifier for our specific machine learning model so we can grab that from our published model details all right so we've grabbed our model uid so we can actually print that out and you can see that that's our unique identifier that represents our machine learning model that we've just gone and saved up so remember i said there was the two parts to actually deploying this so we've now gone and done the first part the second part is actually deploying this as a rest api so let's go on ahead and deploy it now in order to go and deploy it what we do is we take our model uid and we create a new deployment so let's go ahead and do that alrighty awesome so you can see our model is now deploying so we've passed through a couple of key keyword arguments so we've passed through our artifact uid as our model uid this is basically just asking or what's the number for your machine learning model so we've just passed through that what's the deployment name so we specified our deployment name up here so it was transaction forecast dash deployment and whether or not we want to deploy it synchronously or asynchronously so you can see that we've also got a status and we've successfully finished the deployment and we've also got our deployment uid here as well so we've actually just gone and completed those first two steps so we've now saved our model up and we've also deployed it now what we probably want to do is actually go and test it out and see how well our model is performing and just to make sure that it all works so let's go on ahead and test this model out now let's create a new section so we can start to separate out our segments so i'm just going to create some markdown and we're just going to call this deployment review now in order to test out our deployment we're going to need a couple of key things so specifically we're going to need our machine learning token we're also going to need a machine learning instance id and last but not least we're going to need our scoring url we've actually got all of this available to us we just need to pull it out so let's take a look at how we can get that perfect so we've now got our deployment stuff so specifically we've got our token so this is going to allow us to authenticate when we go to make a prediction we've also got our machine learning instance id so this is just coming from our credentials that we published up here and last but not least we're also grabbing our scoring url so this is part of our deployment so when we go and deploy it actually creates a scoring url so we can then go and use that to integrate into other applications so this is actually just coming from our deployment details so if we take a look at our deployment you can see that we've got our scoring url here we're just using the dictionary to extract that url specifically okay so now that that's done what we can actually do is start to score so let's go on ahead and do that now again we're going to need a couple of dependencies to actually make a prediction so specifically we're going to need a couple of libraries that allow us to make requests to an api and then we're going to format our api requests and build that prediction so let's first go ahead and import our dependencies alrighty so we've imported our dependencies so we've imported url lib3 request and json so url lib3 just helps us format our requests in a nicer manner before we send it to our api requests actually allows us to make api requests and json basically allows us to format our results so now what we're going to do is set up our api request headers and our payload and then we can make that request all right so we've set up our header so in this case our header contains three key pieces of information so the type of information that we're going to be sending so we're going to be sending json to our api it also contains our iam token so this allows us to authorize when we're making our api calls and last but not least it also contains our ml instance id so this references our watson machine learning service that we set up right at the start of this video now the next thing that we need to do is set up our payload so our payload actually represents the data or the scoring data that we're actually going to send to our api so in terms of setting up our payload we need two key things so we need the fields and the order that the fields are in so these represent our columns from our testing data frame and we also need our data itself now in order to send this data through to our api we need to convert it into a format that's json serializable i always have trouble saying that that's actually relatively easy to do we just need to use numpy and convert it to a list so let's go on ahead and create our payload alrighty cool so that's done let's take a look at our payload quickly so this should be actually quite big so you can see that we've got two keys so we've got our fields and our values our fields just represent all of our columns and our values represent the data that we're going to be sending so that's all well and good now what we're going to do is go and make our request to our api so in this case we're going to be using the request library to make that call so let's go ahead and make that request okay so before i run that let's just quickly take a look at what we're doing so we're making a new post request and we're passing through our scoring url which we set up here to that we're also passing through our payload as a json object and we're also attaching our headers so ideally what should happen is when we run this we get a response code of 200 which means it's been a successful scoring request then we can actually access our predictions from the text property of our response scoring object so let's go ahead and run our predictions all right now what we can go and do is take a look to see if we've scored successfully so if we take a look at our response scoring object you can see that we've got a response status of 200 so it sounds like everything has gone okay to see our actual predictions all we need to do is access the text component all right and you can see we've got some predictions back so what we can do is relatively easily format that back into a format that's a little bit more comfortable so we can push it into our pandas data frame so in this case what we're actually going to do is take our scored results attach it to our testing data frame and actually calculate the difference then we can save down that document so we've now actually gone and saved our model successfully deployed it successfully and scored successfully so if you wanted to stop here you can so we've now successfully deployed it we're ready for our application i tend to like to take a look at the scoring results just to make sure they seem okay and i like to save that down as well so in order to do that let's reformat these results and attach it to a data frame so what we can do is extract our predictions so now we've got our predictions as a dictionary which we can now traverse in order to grab our prediction values we can use the numpy squeeze function let's make sure we've got numpy imported so the squeeze function basically allows us to extract our individual values from this particular values array here so you can see that we've got an array within an array we just want to extract those values so we can do that relatively easy using mp squeeze perfect now if we take a look you can see that we've just got an array with our prediction values so because we've got an array with our prediction values we can attach this to our data frame so let's go ahead and create a prediction data frame cool so we've created a prediction data frame so this is just our testing data so this is going to allow us to compare our values now what we can do is create a new column to store our scoring results from our api perfect so we've now grabbed our prediction values and we've stored it in a new column called scores so now we're going to be able to see our actual values and our scoring values so if we take a look at our pred df data frame and we scroll all the way over to the right you can see that we've got our actual values as well as our scoring values now we can create a difference column to take a look at the difference between our actual values as well as our scoring values cool so what we've gone and done is we've created a new column called diff and that's the difference between our amount column and our scores column so we should be able to see that now awesome so you can see the difference between our actual values and our scoring values now we're not going to spend too long analyzing those i just wanted to show you how to calculate those differences and see the results of your scoring endpoint if you want you can also save this to a csv if you wanted to use it for something else so we can use do that using the two csv function perfect so now your results are now saved to a csv it'll be in the same folder that your jupyter notebook is in but that about wraps up this video so just to recap so we created a watson machine learning service we set up our service credentials we then imported all of those credentials into our jupyter notebook saved our model deployed a rest api and then actually performed some scoring so this puts us in a really good position to go and build our machine learning app thanks so much for tuning in guys hopefully you found this video useful if you did be sure to give it a thumbs up hit subscribe and tick that bell so you get notified of when i release new videos if you've got any questions at all i mean anything be sure to drop a mention in the comments below and i'll get right back to you as soon as possible thanks again for tuning in peace [Music] you
Original Description
So…you’ve built a machine learning model.
Now, how do you ‘get it’ into your app?
This is where deployment comes in. Deploying your machine learning model as a REST API allows you to use your model across different environments and for different applications regardless of language. Its language agnostic because virtually every language allows you to work with APIs. Watson Machine Learning helps you deploy your model as a REST API with only 9 lines of code. This means it’s quick and easy to start building up applications with your awesome ML models.
Want to learn how?
Well, in this video you'll learn how to:
- Setup a Watson Machine Learning Service
- Deploy your machine learning model with only 9 lines of code
- Send scoring requests to your deployed machine learning model via REST API
Stuff mentioned in the video:
- IBM Cloud: https://cloud.ibm.com/
- Watson Machine Learning Python Client: https://wml-api-pyclient.mybluemix.net/
Oh, and don't forget to connect with me!
LinkedIn: https://www.linkedin.com/in/nicholasrenotte/
Facebook: https://www.facebook.com/nickrenotte/
GitHub: https://github.com/nicknochnack
Happy coding!
Nick
P.s. Let me know how you go and drop a comment if you need a hand!
Music by Lakey Inspired
Chill Day - https://www.youtube.com/watch?v=vtHGESuQ22s
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Nicholas Renotte · Nicholas Renotte · 45 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
▶
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Face Detection - Build An Image Classifier with IBM Watson - Part 7
Nicholas Renotte
Food Image Classification - Build An Image Classifier with IBM Watson - Part 6
Nicholas Renotte
General Image Classification - Build An Image Classifier with IBM Watson - Part 5
Nicholas Renotte
Installing Watson Developer Cloud - Build An Image Classifier with IBM Watson - Part 4
Nicholas Renotte
Generating Credentials - Build An Image Classifier with IBM Watson - Part 3
Nicholas Renotte
Creating A Service - Build An Image Classifier with IBM Watson - Part 2
Nicholas Renotte
Getting an IBMid - Build An Image Classifier with IBM Watson - Part 1
Nicholas Renotte
How to Analyse Review Data - Part 2 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Lemmatize Text - Part 4 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Calculate Sentiment Using TextBlob - Part 5 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Collect Business Reviews Using Python - Part 1 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Clean Text Based Data for NLP - Part 3 - Python Yelp Sentiment Analysis
Nicholas Renotte
How to Setup a IBM Watson Personality Insights Service - Part 1 - Watson Personality Insights
Nicholas Renotte
How to Create a Customer Profile with IBM Watson - Part 2 - Watson Personality Insights
Nicholas Renotte
Visualising The Profile Part 3 Watson Personality Insights
Nicholas Renotte
How to Plot Personality Insights Features at Lightspeed - Part 4 - IBM Watson Personality Insights
Nicholas Renotte
Getting Started With IBM Watson Studio Machine Learning - Part 1 - Predicting Used Car Prices
Nicholas Renotte
Upload and Visualize Data In IBM Watson Studio - Part 2 - Predicting Used Car Prices
Nicholas Renotte
Clean Data and Feature Engineer in IBM Watson Studio - Part 3 - Predict Used Car Prices
Nicholas Renotte
Using Watson Model Builder to Predict Car Prices - Part 4 - Predicting Used Car Prices
Nicholas Renotte
Deploy and Make Predictions With Watson Studio - Part 5 - Predicting Used Car Prices
Nicholas Renotte
Getting Started With IBM Watson Discovery - Part 1 - Stock News Crawler
Nicholas Renotte
How to Run Advanced Queries with Watson Discovery - Part 5 - Stock News Crawler
Nicholas Renotte
How to Run Search Queries with IBM Watson Discovery - Part 4 - Stock News Crawler
Nicholas Renotte
How to Understand the Watson Discovery Data Schema - Part 3 - Stock News Crawler
Nicholas Renotte
How to Build a Watson Discovery Web Crawler - Part 2 - Stock News Crawler
Nicholas Renotte
AI learns what to do next using Tensorflow and Python
Nicholas Renotte
Chatbot Crash Course for Absolute Beginners - Full 20 Minute Tutorial
Nicholas Renotte
Shopify Customer Service Chatbot using Python Automation
Nicholas Renotte
Building a Reddit Keyword Research Chatbot
Nicholas Renotte
Chatbot App Tutorial with Javascript Node.js [Part 1]
Nicholas Renotte
Javascript Chatbot From Scratch with React.Js [Part 2]
Nicholas Renotte
Predicting Churn with Automated Python Machine Learning
Nicholas Renotte
Sales Forecasting in Excel with Machine Learning and Python Automation
Nicholas Renotte
Automate Budgeting with Python and Planning Analytics
Nicholas Renotte
AI vs Machine Learning vs Deep Learning vs Data Science
Nicholas Renotte
Optimizing Marketing Spend using Linear Programming || Marketing Opt PT.1
Nicholas Renotte
Solving Optimization Problems with Python Linear Programming
Nicholas Renotte
Loading Data into Planning Analytics with Python || Marketing Opt PT.2
Nicholas Renotte
Building Marketing Dashboards with Planning Analytics Workspace || Marketing Opt PT.3
Nicholas Renotte
Optimizing Resource Allocation with Docplex and Planning Analytics || Marketing Opt PT.4
Nicholas Renotte
Exploratory Data Analysis With Pandas || Python Machine Learning PT.1
Nicholas Renotte
Preparing Pandas Dataframes for Machine Learning || Python Machine Learning PT.2
Nicholas Renotte
Python Machine Learning with Scikit Learn - Regression || Python Machine Learning PT.3
Nicholas Renotte
Deploying Machine Learning Models with Watson Machine Learning || Python Machine Learning PT.4
Nicholas Renotte
Mind Blowing Machine Learning Apps with Node.JS and Watson Machine Learning || Python ML PT.5
Nicholas Renotte
Build FAST Machine Learning Apps with Javascript React.Js and Watson || Python ML PT.6
Nicholas Renotte
Analyzing Twitter Accounts with Python and Personality Insights
Nicholas Renotte
Converting Speech to Text in 10 Minutes with Python and Watson
Nicholas Renotte
Build a Face Mask Detector in 20 Minutes with Watson and Python
Nicholas Renotte
AI Text to Speech in 10 Minutes with Python and Watson TTS
Nicholas Renotte
Pandas for Data Science in 20 Minutes | Python Crash Course
Nicholas Renotte
Language Translation and Identification in 10 Minutes with Python and Watson AI
Nicholas Renotte
Analyse ANY Conversation in 10 Minutes with Python and Watson Tone Analyser
Nicholas Renotte
Deep Reinforcement Learning Tutorial for Python in 20 Minutes
Nicholas Renotte
NumPy for Beginners in 15 minutes | Python Crash Course
Nicholas Renotte
Real Time Pose Estimation with Tensorflow.Js and Javascript
Nicholas Renotte
Transcribe Video to Text with Python and Watson in 15 Minutes
Nicholas Renotte
Serverless Functions for TM1/Planning Analytics in 20 Minutes
Nicholas Renotte
Building a AI Budget Bot for Planning Analytics with Watson Assistant in 20 Minutes
Nicholas Renotte
More on: ML Pipelines
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Common Next.js Errors (and How I Solved Them)
Dev.to · gary killen
Applying Scalability in Backend (CodeBuddy)
Medium · LLM
Why Every Backend Developer Should Learn Nginx Before Going to Production
Medium · DevOps
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI