铅酸蓄电池的等效数学模型_酸洗机学习模型

铅酸蓄电池的等效数学模型

After you create a machine learning model, you train it. The training process usually takes some time. After the model is trained, what you get is the model object. To use the model, you can either somehow save that model or you can retrain the model every time you use it, which is inefficient).

创建机器学习模型后,您将对其进行训练。 培训过程通常需要一些时间。 训练模型后,您将获得模型对象。 要使用模型,可以以某种方式保存该模型,也可以在每次使用模型时对其进行重新训练,这效率很低)。

If you have terabytes of training data for a model, every time you want to make a prediction, it would be inefficient to train the model each time. Instead, after training the model, you can save it. You should ideally retrain the model once you have new data, but you don’t have to retrain it every time you want to use it.

如果您有TB的模型训练数据,则每次要进行预测时,每次训练模型的效率都会很低。 相反,在训练模型后,您可以保存它。 理想情况下,一旦有了新数据,就应该对模型进行重新训练,但不必每次都想使用它时就对其进行重新训练。

To save a machine learning model, use pickle.

要保存机器学习模型,请使用pickle 。

什么是酸洗? (What Is Pickling?)

Pickle is a module in Python used for serializing and de-serializing Python objects. This converts Python objects like lists, dictionaries, etc. into byte streams (zeroes and ones). You can convert the byte streams back into Python objects through a process called unpickling. Pickling is also known as serialization, flattening, or marshalling.

Pickle是Python中的一个模块,用于对Python对象进行序列化和反序列化。 这会将列表,字典等Python对象转换为字节流(零和一)。 您可以通过一个称为解包的过程将字节流转换回Python对象。 酸洗也称为序列化,展平或编组。

如何腌制 (How to Pickle)

The pickle module has two methods.

泡菜模块有两种方法。

泡菜。 倾倒() (pickle.dump())

The pickle.dump() method dumps the Python object in the pickle file. This creates a .pickle file in your current working directory (pickle.dump(what_are_we_dumping, where_are_we_dumping_it)):

pickle.dump()方法将Python对象转储到pickle文件中。 这会在您当前的工作目录( pickle.dump(what_are_we_dumping, where_are_we_dumping_it) )中创建一个.pickle文件:

import pickle


example_dict = {1:"Australia", 2:"Belgium", 3:"Canada", 4:"Denmark"}


#statement 1
with open('example_dict.pickle', 'wb') as pickle_out:
  pickle.dump(example_dict, pickle_out)
  
#statement 2 
pickle_out = open("example_dict.pickle", "wb")
pickle.dump(example_dict, pickle_out)
pickle_out.close()

In the code snippet above, we are creating an example_dict.pickle file from an example_dict dictionary. Statements 1 and 2 perform the same task of converting the dictionary into a pickle file. Using the with statement ensures that open file descriptors are closed automatically after the program execution leaves the context of the with statement. The 'wb' in the open statement means we are writing bytes to file.

在上面的代码段中,我们从example_dict字典创建了example_dict.pickle文件。 语句1和2执行将字典转换为pickle文件的相同任务。 使用with语句可确保在程序执行离开with语句的上下文之后自动关闭打开的文件描述符。 open语句中的'wb'表示我们正在将字节写入文件。

泡菜。 加载() (pickle.load())

The pickle.load() method lets you use the .pickle file (pickle.load(what_do_we_want_to_load)) by loading it in the memory:

pickle.load()方法允许您通过将.pickle文件( pickle.load(what_do_we_want_to_load) )加载到内存中来使用它:

#statement 1
pickle_in = open("example_dict.pickle", "rb")
example_dict = pickle.load(pickle_in)


# statement 2
example_dict = pickle.load(open("example_dict.pickle", "rb"))

In the code snippet above, we are creating an example_dict dictionary from an example_dict.pickle file. Statements 1 and 2 perform the same task of reading the pickle file, which is a dictionary. The 'rb' in the open statement means that we are reading byte data from the file.

在上面的代码段中,我们从example_dict.pickle文件创建了example_dict字典。 语句1和2执行读取泡菜文件(字典)的相同任务。 open语句中的'rb'表示我们正在从文件中读取字节数据

腌制机器学习分类器 (Pickling a Machine Learning Classifier)

import pickle
import numpy as np
import pandas as pd
from sklearn import preprocessing, model_selection, linear_model


# Read data
df = pd.read_csv('sample_data.csv')
X = np.array(df['X'])
y = np.array(df['y'])
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.2)
X_train = X_train.reshape(-1, 1)
X_test = X_test.reshape(-1, 1)


# Train Classifier
clf = linear_model.LinearRegression()
clf.fit(X_train, y_train)


# Create Pickle file from the Linear Regression Classifier(clf)
with open('lineraregression.pickle', 'wb') as dump_var:
    pickle.dump(clf, dump_var)


# Load the Pickle fule in the memory
pickle_in = open('lineraregression.pickle', 'rb')
pickle_clf = pickle.load(pickle_in)


# Use the Pickle file insted of clf
accuracy_pkl = pickle_clf.score(X_test, y_test)
accuracy_clf = clf.score(X_test, y_test)
print(accuracy_pkl == accuracy_clf)
# output = True

After importing all the necessary libraries, we read the data and then split it into testing and training sets. We then train (fit) the classifier on X_train and y_train data. After we have trained the classifier, we then proceed to save this classifier in a pickle file. We create a pickle file (linearregression.pickle) from the Linear Regression classifier (clf).

导入所有必需的库之后,我们读取数据,然后将其分为测试和培训集。 然后,我们在X_trainy_train数据上训练(拟合)分类器。 训练完分类器后,我们便将该分类器保存在pickle文件中。 我们从线性回归分类器( clf )创建一个pickle文件( linearregression.pickle )。

When we want to use a trained classifier, we convert the pickle file (linearregression.pickle) back to a classifier (pickle_clf). Any time we want to use a classifier in our program, we just use pickle_clf.

当我们要使用训练的分类,我们把泡菜文件( linearregression.pickle )回分类( pickle_clf )。 每当我们想要在程序中使用分类器时,我们pickle_clf使用pickle_clf

The original classifier (clf) and pickled classifier (pickle_clf) are the same. This can be proved by comparing the accuracies achieved by both of them on the same testing set. When compared, we are returned True, which shows the accuracies are the same and proves that the classifiers are the same.

原始分类器( clf )和腌制分类器( pickle_clf )相同。 可以通过比较两个用户在同一测试集上获得的精度来证明这一点。 比较后,我们返回True ,这表明精度是相同的,并证明了分类器是相同的。

何时泡菜 (When to Pickle)

  • To save a program’s state.

    保存程序的状态。
  • To send Python data over a TCP connection.

    通过TCP连接发送Python数据。
  • To save Python objects.

    保存Python对象。

什么时候不泡菜 (When Not to Pickle)

  • When working with multiple Python versions: Unpickling objects pickled in different Python versions can be a hassle.

    使用多个Python版本时: 取消腌制在不同Python版本中腌制的对象可能会很麻烦。

  • When working across multiple languages: The data format used by pickle is Python-specific, which means that non-Python programs may not be able to reconstruct pickled Python objects.

    跨多种语言工作时: pickle使用的数据格式是特定于Python的,这意味着非Python程序可能无法重建腌制的Python对象。

  • When working with a recursive data structure: Trying to pickle a highly recursive data structure may exceed the maximum recursion depth. A RuntimeError will be raised in this case. You can raise this limit with sys.setrecursionlimit().

    使用递归数据结构时: 尝试腌制高度递归的数据结构可能会超出最大递归深度。 在这种情况下,将引发RuntimeError 。 您可以使用sys.setrecursionlimit()提高此限制。

  • When working with an external pickle file: The pickle module is not secure. Only unpickle the data that you trust. It is possible to construct malicious pickle data that will execute arbitrary code during unpickling. Never unpickle data that could have come from an untrusted source.

    使用外部泡菜文件时: pickle模块不牢固 。 仅释放您信任的数据。 可能会构造恶意的泡菜数据, 拆开过程中执行任意代码。 永远不要挑剔那些可能来自不可信来源的数据。

cPickle (cPickle)

Just like the pickle module, cPickle supports the serialization and de-serialization of Python objects. Because it is written in the C language, it is a 1,000 times faster than Python-based pickle. As output produced by pickle and cPickle are the same, we can use both modules interchangeably.

就像pickle模块一样, cPickle支持Python对象的序列化和反序列化。 因为它是用C语言编写的,所以它比基于Python的pickle 快1000倍 。 因为picklecPickle产生的输出是相同的,所以我们可以互换使用这两个模块。

资源资源 (Resources)

  1. Python official docs:

    Python官方文档:

    Python official docs: https://docs.python.org/2/library/pickle.html

    Python官方文档: https : //docs.python.org/2/library/pickle.html

    Python official docs: https://docs.python.org/2/library/pickle.html https://docs.python.org/3/library/pickle.html

    Python官方文档: https : //docs.python.org/2/library/pickle.html https://docs.python.org/3/library/pickle.html

  2. Dan Bader’s blog:

    Dan Bader的博客:

    Dan Bader’s blog: https://dbader.org/blog/python-context-managers-and-with-statement

    Dan Bader的博客: https : //dbader.org/blog/python-context-managers-and-with-statement

  3. Manu Kalia’s article on pickle:

    Manu Kalia在腌菜上的文章:

    Manu Kalia’s article on pickle: https://medium.com/better-programming/dont-fear-the-pickle-using-pickle-dump-and-pickle-load-5212f23dbbce

    Manu Kalia在腌菜上的文章: https : //medium.com/better-programming/dont-fear-the-pickle-using-pickle-dump-and-pickle-load-5212f23dbbce

  4. Lokesh Sharma’s blog on pickle:

    Lokesh Sharma在泡菜上的博客:

    Lokesh Sharma’s blog on pickle: https://medium.com/@lokeshsharma596/what-is-pickle-in-python-3d9f261498b4

    Lokesh Sharma在pickle上的博客: https : //medium.com/@lokeshsharma596/what-is-pickle-in-python-3d9f261498b4

  5. Sentdex Machine Learning playlist:

    Sentdex机器学习播放列表:

    Sentdex Machine Learning playlist:https://www.youtube.com/watchv=za5s7RB_VLw&list=PLQVvvaa0QuDfKTOs3Keq_kaG2P55YRn5v&index=7&t=0s

    Sentdex机器学习播放列表: https : //www.youtube.com/watchv=za5s7RB_VLw&list=PLQVvvaa0QuDfKTOs3Keq_kaG2P55YRn5v&index=7&t=0s

  6. Sentdex pickle tutorial:

    Sentdex泡菜教程:

    Sentdex pickle tutorial:https://www.youtube.com/watch?v=2Tw39kZIbhs&t=343s

    Sentdex泡菜教程: https : //www.youtube.com/watch?v = 2Tw39kZIbhs & t = 343s

翻译自: https://medium.com/better-programming/pickling-machine-learning-models-aeb474bc2d78

铅酸蓄电池的等效数学模型

你可能感兴趣的:(python,机器学习,人工智能,tensorflow,java)