简体   繁体   English

转储并在两个不同的文件中加载莳萝(腌)

[英]dump and load a dill (pickle) in two different files

I think this is fundamental to many people who know how to deal with pickle. 我认为这对于许多知道如何处理泡菜的人来说是至关重要的。 However, I still can't get it very right after trying for a few hours. 但是,尝试了几个小时后,我仍然无法完全解决问题。 I have the following code: 我有以下代码:

In the first file 在第一个文件中

import pandas as pd

names = ["John", "Mary", "Mary", "Suzanne", "John", "Suzanne"]
scores = [80, 90, 90, 92, 95, 100]

records = pd.DataFrame({"name": names, "score": scores})
means = records.groupby('name').mean()

def name_score_function(record):
    if record in names:
        return(means.loc[record, 'score'])

import dill as pickle
with open('name_model.pkl', 'wb') as file:
    pickle.dump(means, file)

The second file 第二档

I would like to load what I have in the first file and make the score of a person (ie John, Mary, Suzanne) callable via a function name_model(record): 我想加载第一个文件中的内容,并通过一个函数name_model(record)来调用一个人(例如John,Mary,Suzanne)的分数:

import dill as pickle
B = pickle.load('name_model.pkl')

def name_model(record):
    if record in names:
        return(means.loc[record, 'score'])

Here it shows the error: 这里显示错误:

File "names.py", line 21, in <module>
B = pickle.load('name_model.pkl')
File "/opt/conda/lib/python2.7/site-packages/dill/dill.py", line 197, in load
pik = Unpickler(file)
File "/opt/conda/lib/python2.7/site-packages/dill/dill.py", line 356, in __init__
StockUnpickler.__init__(self, *args, **kwds)
File "/opt/conda/lib/python2.7/pickle.py", line 847, in __init__
self.readline = file.readline
AttributeError: 'str' object has no attribute 'readline'

I know the error comes from my lack of understanding of pickle. 我知道错误是由于我对泡菜缺乏了解。 I would humbly accept your opinions to improve this code. 我会谦虚接受您的意见以改进此代码。 Thank you!! 谢谢!!

UPDATE The more specific thing I would like to achieve: 更新我想实现的更具体的事情:

I would like to be able to use the function that I write in the first file and dump it, and then read it in the second file and be able to use this function to query the mean score of any person in the records. 我希望能够使用我在第一个文件中编写的函数并将其转储,然后在第二个文件中读取它,并能够使用此函数查询记录中任何人的平均得分。

Here is what I have: 这是我所拥有的:

import pandas as pd

names = ["John", "Mary", "Mary", "Suzanne", "John", "Suzanne"]
scores = [80, 90, 90, 92, 95, 100]

records = pd.DataFrame({"name": names, "score": scores})
means = records.groupby('name').mean()

def name_score_function(record):
if record in names:
    return(means.loc[record, 'score'])

B = name_score_function(record)

import dill as pickle
with open('name_model.pkl', 'wb') as file:
    pickle.dump(B, file)

with open('name_model.pkl', 'rb') as file:
    B = pickle.load(f)

def name_model(record):
   return B(record)

print(name_model("John"))  

As I execute this code, I have this error File "test.py", line 13, in <module> B = name_score_function(record) NameError: name 'record' is not defined 当我执行此代码时,我File "test.py", line 13, in <module> B = name_score_function(record) NameError: name 'record' is not defined有此错误File "test.py", line 13, in <module> B = name_score_function(record) NameError: name 'record' is not defined

I highly appreciate your assistance and patience. 非常感谢您的协助和耐心。

Thank you. 谢谢。 It looks like the following can solve the problem. 看起来以下方法可以解决问题。

import pandas as pd

names = ["John", "Mary", "Mary", "Suzanne", "John", "Suzanne"]
scores = [80, 90, 90, 92, 95, 100]

records = pd.DataFrame({"name": names, "score": scores})
means = records.groupby('name').mean()

import dill as pickle
with open('name_model.pkl', 'wb') as file:
    pickle.dump(means, file)

with open('name_model.pkl', 'rb') as file:
    B = pickle.load(file)

def name_score_function(record):
    if record in names:
        return(means.loc[record, 'score'])

print(name_score_function("John"))

Hmm. you need to read it the same way you wrote it -- nesting it inside an open clause: 您需要以与编写方式相同的方式进行阅读-将其嵌套在open子句中:

import dill as pickle
with open('name_model.pkl' ,'rb') as f:
    B = pickle.load(f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM