Serialize Python Objects With Pickle
Skills:
AI Pair Programming80%
Key Takeaways
This video demonstrates how to use the Pickle module in Python to serialize objects, a fundamental concept in AI coding. The video covers the basics of Pickle and its application in Python programming.
Full Transcript
[Music] what is going on guys welcome back in today's video we're going to talk about pickle and serialization in python so let us get right into it all right so this video is going to be a very fundamental one a very basic one because pickle is something that we have used in a lot of different videos on this channel be it to serialize a uh machine learning model of a chatbot or often rnn generating text or be it's serializing some financial data or some financial objects we have used pickle a couple of times here but i never explained uh on a very fundamental level what pickle is and how to use it or what serialization is why are we actually using pickle why are we not just writing into some files so this is a video for beginners mainly if you have used pickle before and you know why you use it then you don't need this video this video is for beginners that want to understand why we actually use pickle so let's get started with a very basic example let's say we have some variables for example my text equals hello world and i have my int equals 18 and i have i don't know my float equals 7.657 and i want to store these values uh externally so i want to run the script and let's say those are not just values that i assign but those are values that are results of calculations or this is a result of a string function so this is something that is the result of a script running and what i want to do is i want to run the script once i want to get those values and then i want to store those values into a file so that next time when i run the script i don't have to actually calculate all those values again or i don't want to execute some code to get those values again i just want to load them from the file directly and we have done this a couple of times with the yahoo finance api where we get some data and we want to store it instead of every time we run the script requesting the data again and again so let's say we have those values result of a calculation we want to save them what we can do of course is we can go ahead and say with open it's just a basic file stream here with openmydata.txt in writingmode sf and we're just going to say f.right my text f.write my int and f dot write my float come on pycharm is a little bit laggy today i'm not sure why and of course i think we need to type cast to a string before we can write so type cast integer into a string type cast a float into a string and then we can just run this script here and we can see that this works fine so we have this my data txt oh i forgot to append a backslash n for a line break so we're going to do that there you go and you can see that we have the data now here in the file so what we can do is we can delete all that and we can delete all that and we can just say with open my data dot txt in reading mode sf my text or actually let's say data equals f dot read dot split lines and then i can say my text oh read as a function obviously my text equals data zero my int equals data one and myfloat equals data2 here we can also do [Music] the typecasting so that we actually get floats and integers and then we can go ahead and print that so i'm just showing you that you can do it like that without pickle i'm going to explain in a second why this is possible so easily i mean you probably already know it um but as you can see we don't have to define these values you just have to load them from a file and then you have them back in your program and you can work with them so this works so well without pickle because those are primitive data types all afloat is is just a float all an integer is is just a number an integer number and all a text is is just a string now of course in python we have classes and objects for everything so we have methods and so on so it's not completely primitive but the basic idea is that we have a string we have an integer we have a float that's what we care about we care about the value of those things we don't care really about the state of uh an integer we want to know okay this integer is 18 we don't want to know case some certain state like attributes or anything of that integer we have the value 18 that's what we care about so we can just store that value 18 in a text file and that's it now still you can see that it's a little bit more complicated because we need to remember okay where is what and what's data type because who tells me that this is actually 18 as an integer and not 18 as a string somewhere um and so on so it is it is possible to do it like that and it works for primitive data types but it is quite complicated and the moment i try something more complicated like let's say my dictionary equals and then i have a and a has the value 18 and b points to the value 99 and then i have h for example which is 76. if i want to store a dictionary it's a little bit more complicated of course i can go through all the key value pairs and store them in a file and then load them in the same order but it's complicated i need to probably deal with with string formatting with string functions and so on um so i'm not actually storing the object and what i can do in python to store the actual object into a file is i can use pickle so pickle is for serializing objects so i can serialize and deserialize objects i can store objects i can dump objects into files and i can load them again from those files in the exact same state that there were when i dumped them into the file and this is especially useful if we have something like dictionaries or even if we have something more complicated like a person class so let's say i have or a person object actually let's say i have a person class with a constructor and i pass a name an age and a weight i say self.name equals name self.h equals h and self.weight equals weight uh and then i have a function print info or a method print info where i just say print self.name printself.edu and print self.wait and then i have get older for example where i just say self.h plus equals i need a parameter years plus equals years and that's it that's the person class that i have very basic one and now let's have p1 equals um a person the name of the person is mike and mike starts with 25 years of age and mike has a weight of 89 kilograms uh and now what i can do is of course i can go and say p1 dot print info then i can say p1.get older i can increase the h by 6 for example so he's 31 after that and i can print the info again so we can see that this works if i run this mike is now 31 and still has the same weight now let's say i want to store mike into a file the way that he is after this uh get older statement uh what i can do is i can use pickle to serialize the whole mic object with all the state with the whole state with all the attributes with all the connections with everything that is relevant for the p1 object i can store it into a file and later on just load it without having to make all this definition here so in order to do that i need to import pickle so i need to say import pickle and then i can just go ahead and say with open mic dot pickle now you don't need to call it dot pickle you can also call it whatever you want um you can you can call this abc.xyz it doesn't matter but i like to call pickleobjects.pickle so that i know that they're pickle objects you don't have to do that if you don't want to uh and the important thing is that we write bytes so we want to open this file in the wb mode writing bytes mode sf again and then what we do is we say pickle dot dump the p1 object into the file into the file stream so if we do that we can run this and you can see that we have this mic.pickle object here this is a binary object it's not readable some things are readable but all in all it's a binary file so you're not going to be able to read it and what we can do now is we can get rid of all this we still need the class definition because we need to know the blueprint of a person but i can just go ahead now and say with open mic.pickle this time in reading bytes mode sf and i can just say p1 equals pickle.load from f then print or not print sorry then just p1 dot print info like that and you're going to see that it still works so i have not defined p1 anywhere i just loaded it from this pickle file and you can see that i have all the relevant information so if you want to store uh actual objects more complicated things like dictionaries or complicated objects or whole data frames from pandas and basically everything you want to store that is a little bit more complicated than just a primitive data type you can do that with pickle easily you just say pickle dot dump and you dump the whole object with its state into a file that you can load later on and this is very useful because sometimes when you train a machine learning model to recognize handwritten digits or to chat with you because it's a chat bot or if you i don't know have some text generating rnn or if you have a big data frame with financial data that you have already processed and you don't want to do this all the time every time you run the script you want to save the result uh then you can just do that with pickle with one line and then you can load the same thing into the script without having to go through all the processes again and this makes pickle quite useful this is serialization when you uh when you dump it into file it's called serialization when you get it from the file when you load it it's called deserialization so a very basic concept but a very important concept all right so that's it for today's video hope you enjoyed i hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Original Description
In this fundamental video, we learn how to use Pickle in order to serialize objects in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 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
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
AI CLI Tools Are Eating Each Other's Lunch
Dev.to · Tracepilot
Loop Engineering: The Skill That Just Made Your AI Workflow Obsolete
Medium · Machine Learning
We built a real-time Pokémon TCG AR overlay for live streams and open-sourced everything
Medium · Deep Learning
I tested the new Claude Desktop on Linux - here's how it compares to rival apps
ZDNet
🎓
Tutor Explanation
DeepCamp AI