Serialize Python Objects With Pickle

NeuralNine · Beginner ·💻 AI-Assisted Coding ·4y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches the basics of Pickle and how to use it to serialize and deserialize Python objects. It is a fundamental concept in AI coding and Python programming. By watching this video, viewers can learn how to use Pickle to save and load Python objects.

Key Takeaways
  1. Import the Pickle module
  2. Use the dump() function to serialize an object
  3. Use the load() function to deserialize an object
  4. Handle exceptions and errors
💡 Pickle is a built-in Python module that allows for easy serialization and deserialization of Python objects.

Related Reads

📰
AI CLI Tools Are Eating Each Other's Lunch
AI CLI tools are becoming increasingly similar, making it difficult to choose between them, and their failure patterns are identical, highlighting the need for more transparency and accountability in AI development
Dev.to · Tracepilot
📰
Loop Engineering: The Skill That Just Made Your AI Workflow Obsolete
Learn how loop engineering can automate AI workflows, making traditional prompting methods obsolete
Medium · Machine Learning
📰
We built a real-time Pokémon TCG AR overlay for live streams and open-sourced everything
Learn how to build a real-time Pokémon TCG AR overlay using a webcam, gaming PC, and open-source AI
Medium · Deep Learning
📰
I tested the new Claude Desktop on Linux - here's how it compares to rival apps
Learn how Claude Desktop on Linux compares to rival apps and its limitations with local AI
ZDNet
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →