Python Machine Learning Tutorial #4 - Saving Models & Plotting Data
Key Takeaways
Covers saving and loading models using the Pickle module and plotting data points using Matplotlib in Python
Full Transcript
hey guys and welcome to the fourth video in my machine learning with Python series and in today's video we are gonna be going over saving our models and how we can kind of plot data on a grid and actually visualize some of the stuff that we're doing so essentially what we're gonna do today is we're going to use the pickle knowledgeable that comes built into Python to save our models we're going to be creating multiple models and trying to save the best possible one to use in the future and then we're going to be plotting things like on a graph that we can kind of look at so yeah so let's go ahead and get started so the first thing we actually need to do is we need to import another module or install another module so you can see here I've got my tensor environment already loaded up and activated so again reminder you can activate and then whatever the name of your environment is and just hit enter I'm just doing this again in the little terminal in pycharm now what we need to install is we're just guys I hit install mat plot lib ok now this is just a library for doing like basic graphs and grids and stuff um so do that I already have it installed so I'm not gonna hit enter I'll take a second and I think that's actually the only module we need for today and in the future we probably won't have to install any more modules because I think we have most of them already now okay so we're gonna import matplotlib now so a matplotlib dot pi plot as pi plot that one is always a long import statement ok so import map plot lived up hi plot as pi plot and then we're going to import what he call it pickle and this should actually already be installed with your python if it's not for some reason to leave a comment down below or go to tech with him net and leave a thing on the forum again if you guys want to follow along with all the code here it's gonna be up on tech with tim net so go on there'll be a link in the description where you can see the text-based tutorial and all the code in the video and all that ok now we're actually going to import something from a plot lid so from matt plot lib import style like that and this is just cuz we're gonna change the style of our grid um now what should we do first I think what we're gonna do first is actually save our model and then it'll go into drawing some of this stuff ok so why would we want to save our model well you guys may have noticed already that in our case our model trains really like under a second is how long it takes to train so doesn't really make sense to save it in our instance because well it's so quick that it's training but on a lot of other models that are training off of like hundreds of thousands of pieces of data you don't want to have to retrain your model every time you're gonna use it and if you can find a model that has like a 90 something percent accuracy or like a very high accuracy you probably want to save that model as opposed to risking it in because you can see our model fluctuates between like eighty and ninety percent so essentially we'd want to save the highest-scoring model to use that on our future data sets right so the way that we can do this is really easy and it's using pickle so what we're gonna do is after we score and we fit in the model we can actually just literally just pickle it so we're gonna do is gonna say with open and in here we're just gonna name this whatever we want I'm gonna just name this student model okay dot pickle and then we're gonna open this in WB mode okay and this just who's gonna write the file for us essentially if it doesn't already exist and we're gonna open this as f now this is really easy and all you have to do to actually save this is you do pickle dump and then what you're gonna do is you're gonna put what we're dumping in this case linear which is our model and then into the file F okay so pickle dump linear F so this is essentially gonna save a pickle file for us in our directory and then we can open that and use that don't ask me how pickle works cuz I honestly can't tell you but all I know is it saves the model for us and yeah it works pretty well so now what we're gonna do is we need to read in our pickle right so read in our pickle file so to do this once we've created this file obviously which is gonna happen here we're just gonna say pickle and it'd probably be good if I spelled pickle right so pickle in you can name this whatever you want equals pickle dot load I believe yes the load and then we're just gonna take this so student model dot pickle and throw that in there okay and I think that might be it or no I think I actually am messing this up I think what we have to do is we have to open sorry and then let's do this student model dot pickle and then we're gonna open that in our B mode okay yes that looks correct let me just double check here and yes that is sorry that is how you do it now what we have to do is we can load this pickle into our linear model okay so we call it linear so we're going to say linear is equal to pickle dot load and then pickle again like that okay so now this is going to load our model into the variable called linear okay so what I'm gonna do now is I'm just gonna run this to generate the pickle file and then I'm actually gonna just delete all this and show you that this is actually working just by loading in the file and we don't we're not retraining every time okay so let's run this one and let it go through give it a second okay and now if we go here you can see we have a student model dot pickle file over here all right so now what I'm going to do is I'm just gonna comment out from here so one two three and we will go to one two three there so now all we're doing is we're just loading in our pickle you can see we're skipping the whole training process and the saving of the pickle and we'll just see if this works now okay so run this and you can see that we're getting no errors and everything is working just fine okay so yeah that is essentially how that works okay so now that we've saved our model I want to show you how we can save like a better version of our model so essentially what I want to do is I want to continue to keep what do you call it trying to save a model until we get a certain score so until we get like above 90 or above 95 or something like that okay so the way that we can do this is really easy we're just gonna use a for loop or we could use a while loop actually but um what I'm gonna do is we need this extra X test okay so let's just do this I'm gonna type it out and then we'll kind of talk about what's happening so let's say for underscore cuz we don't actually care about this variable in range and then type whatever range you want I'm just gonna type like 30 and I'm gonna set a variable best up here and this is just gonna keep track of the best score we've gone so far okay and let's do that okay now what I'm gonna say is going to say if ACC which is our accuracy okay is greater than our current best score what we're gonna do is we're going to write to the file about new model that we've just trained okay because we did the linear model and then we fit it right and we're gonna set our best equal to ACC so now essentially we're only gonna save a new model if this current score for that model is better than any previous one that we've seen and we're gonna do this 30 times now you guys could technically do this like 10000 times and it probably wouldn't take that long but for the purpose of the tutorial I'm just gonna do it 30 times if you guys can find a model that gets like 95% and you like messed with all these and stuff just let me know because that's I don't know that's kind of cool all right so we'll do that and what I'm gonna do is we'll just say we'll just print out the accuracy each time just so we get some kind of output so we can see what it looks like essentially all right and what we're also gonna do is I'm gonna take this and put it up here and I'll talk about why I'm doing this in just one sec okay so let's run the file and let's see what we get okay so you can see that happened like almost instantly and we can run through we can see here our highest accuracy was probably 95 unless there was a higher one here so that would mean the model now that we're training off of would be that 95 accuracy model okay so what we can do now is we actually get rid of this for loop just by simply commenting it out so we're not gonna be training any more models now unless we wanted to keep going and what we'll do here is we'll just load in the model that we just created and use that now the reason that I've redefined this up here is for this exact purpose so when we stopped like retraining the model then we still have this extra annexed sy train Y test because we do some stuff with that down here right and essentially you might be asking why are we getting different scores each time we run this model well that's because every time we do this like this test size equals 0.1 we're getting different training data right so you would think if we have the same training data the model would be the same every time but essentially since we're splitting this and 90% is going to one and 10% is going to the other it just randomly selects them so that our model is gonna be different each time because we're training off of different data right okay so now we have that this should be working but I've already ran this enough times you guys already know what this looks like so let's actually get into plottings things on a grid so we can see what they look like so we're gonna use matplotlib to do this and the first thing we're gonna do is I'm just gonna cut this in because it's kind of a weird name and I was you know to spell it but we're just gonna do style use and then ggplot okay essentially this is just gonna make our grid look like half-decent okay in matplotlib and then what we're gonna do is we're going to plot a few pieces of information so to plot our information what we're going to start off by doing is we're just gonna set up a scatter plot now we could do like a line graph Kanna or like a connected graph but there's a lot of points and they end up getting that kind of messy together and they don't connect ya proper way I tried this earlier so what we're gonna do is really use a scatter plot so essentially to do this we're gonna do pipe lot dot scatter and then we need to give an x and a y value so for our x value what we're actually gonna do here is a data and then what we're going to do for this is I'm gonna set a variable and I'm gonna say P equals and in this case I'm gonna do G 1 now P this is gonna be one of your attributes okay so you can pick G 1 G 2 study time failures absences so one of the features pretty well and then our Y value is always going to be the label which is going to be G 3 so we're in new data and then and here were just gonna type P so it's more like dynamic and we can just change it here and then what we're gonna do is read your data and what do you call it G 3 okay now what we're gonna do is we're just gonna set up some labels for our axes so we know what is once we escape pipe lot dot X label and our X label is gonna be P okay and then pipe lot dot Y label and this is gonna be what do you call it final grades and so G 3 I'm actually gonna say final grade because we already know what this represents now to show the grade will do pipe lot dot show and this is now essentially just gonna show us a grid so actually let's run this and see what we get so we still get the same output and now you can actually see what our data points look like so this is our grade ones like the first semester grade and this is the final grade and you can see the correlation between them now obviously we have some outliers out here but for a majority of them you can see like that a best foot line would look like something like that right like I was kind of demonstrating earlier in the last video now if we want to see the correlation between other features and our final grade what we only have to do is just change this so I could say put G 2 here instead and we should get something similar so we can see it just shifted over a little bit but it's similar data points now you guys may notice well you're like there's 600 students why is Emily showing this many points well essentially a lot of the points are overlapping so you can kind of see on some of them how they're not perfect circles but that's essentially why because the grade points were rounded to to be out of 20 so not like perfect percentages so that's why we're not seeing 600 points here we're only seeing like I don't know what like 100 or something so yeah so anyways we can also look at other ones with some other ones here study time failures absences and this is a good way to kind of see what correlation we're getting between different points right so for study time here you can see we're getting this very like linear line going up and I mean you guys can look here and see if study time really is affecting the final grade or not right so this is a good way to look at a graphical representation of all these things so failures so let's type this one in here and see what this looks like again very similar thing to what do you call it the study time sorry what am i saying here we can see that people with three failures typically had a lower grade than people with zero failures right so there is some kind of correlation here and that's what I'm trying to get at with this linear regression and then obviously for the last one we could do absences like this and see what this has to do with final grade if there's much correlation yep so again we see if someone had 78 absences typically their lower right so we can see kind of some correlation here right in how that works so anyways that is how you can kind of plot different points on a graph if you're using a scatterplot literally just pick like what two points from data you want to plot and then you can give an X label Y label show it and just use this style if you want it to look half decent so anyways that has been it for this video in the next video we're gonna be moving into classification algorithms so it's it's K nearest neighbors some of you may have heard of it before really interesting it's going to allow us to classify things into different kind of categories whereas this one was just giving us like some kind of linear prediction or number prediction so as always if you guys enjoyed the video please make sure you leave like all of the code is up on tech with Tim net and I will see you in the next video [Music]
Original Description
In this machine learning python tutorial I will be covering saving and loading models using the pickle module. I will also be going over plotting data points using the matplotlib library for python.
⭐ Kite is a free AI-powered coding assistant for Python that will help you code smarter and faster. Integrates with Atom, PyCharm, VS Code, Sublime, Vim, and Spyder. I've been using Kite for 6 months and I love it! https://kite.com/download/?utm_medium=referral&utm_source=youtube&utm_campaign=techwithtim&utm_content=description-only
Text-Based Tutorial & Code: https://techwithtim.net/tutorials/machine-learning-python/saving-models/
**************************************************************
WEBSITE: https://techwithtim.net
proXPN VPN: https://secure.proxpn.com/?a_aid=5c34b30d44d9d
Use the Code "SAVE6144" For 50% Off!
One-Time Donations: https://goo.gl/pbCE9J
Support the Channel: https://www.patreon.com/techwithtim
Twitter: https://twitter.com/TechWithTimm
Join my discord server: https://discord.gg/pr2k55t
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python Tutorials
- Machine learning tutorial
- Python machine learning
- Python machine learning tutorial
- Pickle python
- Saving models
- Matplotlib tutorial
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
Related Reads
📰
📰
📰
📰
I Don’t Want to Build Another App. I Want to Find Out Where the Models Break.
Medium · Machine Learning
A Graph Neural Network Model for Real-Time Gesture Recognition Based on sEMG Signals
ArXiv cs.AI
Evaluating the Effect of Frame Rate in Sequence-Based Classification of Autism-Related Self-Stimulatory Hand Idiosyncrasies
ArXiv cs.AI
Measure, Don't Estimate: Labeling Speakers Without a Gated Model
Dev.to · Dima Statz
🎓
Tutor Explanation
DeepCamp AI