In this article,

mentioned the use of the pickle Library in the article "understand what machine learning through a simple example". Why do

need serialization and anti serialization?

1. is easy to store. The serialization process transforms the text information into a binary data stream. In this way, information can be stored in the hard disk. When we need to read the file, we can read the data from the hard disk, and then serialize it, then we can get the original data. Some strings, lists, dictionaries and other data are obtained in the operation of Python program. They want to be preserved for long time, and convenient for future use, rather than simply put into memory, and turn off power and lose data. The Pickle module in the python module encyclopedia is used to convert objects into a format that can be transmitted or stored.

2. is convenient for transmission. When two processes are communicating remotely, each type of data can be sent to each other. No matter what type of data, it is transmitted on the network in the form of binary sequence. The sender needs to convert an object into a byte sequence, which can be transmitted over the network; the receiver needs to restore the byte sequence to the object.

understands machine learning

pickle by a simple example. It is a standard module of Python language. After installing python, it contains pickle library, and no need to install it individually. The

pickle module implements basic data serialization and anti serialization. Through the serialization operation of the pickle module, we can save the object information stored in the program to the file and store it permanently. Through the anti serialization operation of the pickle module, we can create the object saved from the last program from the file. In the official introduction of

, there are several words in the English serialization operation, such as serializing, pickling, serialization, marshalling or flattening, all of which represent serialization. Accordingly, there are many English words in the anti serialization operation, such as "de-serializing", "unpickling", "deserailization" and so on. To avoid confusion, you usually use "pickling" / "unpickling", or "serialization" / "deserailization".

pickle module is serialized in binary form and saved to files (the suffix of saved files is ".Pkl"), which cannot be opened directly for preview. The other serialization standard module JSON of python, which is human-readable, can open the view directly (for example, in notepad++). The

pickle module has two main types of interfaces, serialization and anti serialization.

the serialization operation include:

()

 pickle.dump Pickler (file, protocol).Dump (obj) 

 deserialization operations include: (pickle.load) Unpickler (file).Load (pickle.dump) 

2.1 serialization serialization method (

method) for

serialization (pickle.dump), following the relevant parameters of the method:

 pickle.dump (obj, file, protocol=None, fix_imports=True *, 

) of this method is that the serialized object obj in binary form written in file files, save. Its functionality is equivalent to Pickler (file, protocol).Dump (obj).

on parameter file, one point to be noted, must be in the form of binary operation (write). The case of

is referred to as the following:

 import picklewith open ('svm_model_iris.pkl','wb') as f: f:, import is written in binary form. 

, on parameter protocol, has a total of 5 different types, that is (0,1,2,3,4). (0,1,2) corresponds to the earlier version of the python, and (3,4) is a version after the python3.

in addition, the parameters are optional pickle.HIGHEST_PROTOCOL and pickle.DEFAULT_PROTOCOL. Currently, in the python3.5 version, the value of the pickle.HIGHEST_PROTOCOL is 4, and the value of the pickle.DEFAULT_PROTOCOL is 3. When the protocol parameter is negative, the selected parameter is pickle.HIGHEST_PROTOCOL.

parameters of protocol are introduced in detail, the official

"text-align: center >

3

3.1 deserialization deserialize operation method (

method) for pickle.load (

), serialization of the following parameters of the method:

 pickle.load (file, fix_imports=True, encoding= *, "ASCII". Errors= "strict" 

) implementation of the method is to the serialized object is read from the file file. Its functionality is equivalent to Unpickler (file).Load ().

on parameter file, one point to be noted, must be in the form of binary operation (read). The case of

is referred to as the following example:

 import picklewith open ('svm_model_iris.pkl','rb') as f:, the model = = (or)), and it is read in binary form. When 

reads, the parameter protocol is automatically selected, and there is no this parameter in the load () method. Pickle.loads

3.2 (

)

pickle.loads deserialization () the following method parameters:

 pickle.loads (bytes_object, fix_imports=True, encoding= *, "ASCII". Errors= "strict" 

pickle.loads (

)) (pickle.load) method with the method of difference is that the pickle.loads () method is read directly from the bytes object serialization in the information, rather than read from the file. Unpickler

3.3 (file).Load deserialization (

)

pickle module provides the methods of object deserialization, namely class pickle.Unpickler (file, fix_imports=True *, encoding=, "ASCII". Errors= "strict"), Pickler (load) method with. The function of

Unpickler (file).Load () is the same as pickle.load ().

for other method of the Unpickler class, please refer to the official API.

4, those types can be serialized and anti serialized operation.

official document is so introduced, I will not further describe it here.

"written on the back of

pickle module is quite practical, of course, on the pickle module, there are plenty of information to understand, want to know more information about children's shoes, can read Python official advice the API document (library file).

above is the detailed introduction of the Pickle Library in Python, which is introduced by Xiaobian. We hope it can help you. If you have any questions, please leave a message for me, Xiaobian will reply to you in time. Thank you for your support for the home website of the script!

This paper fixed link:http://www.script-home.com/use-of-pickle-library-in-python.html | Script Home | +Copy Link

Article reprint please specify:Use of Pickle Library in Python | Script Home

You may also be interested in these articles!