我认为这对许多知道如何应对泡菜的人来说至关重要。但是,在尝试了几个小时之后,我仍然无法做到这一点。我有以下代码:
在第一个文件中
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)
第二个文件
我想在第一个文件中加载我的内容,并通过函数name_model(记录)调用一个人(即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'])
这里显示错误:
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'
我知道错误来自于我对泡菜缺乏了解。我会谦卑地接受你的意见来改进这段代码。谢谢!!
更新 我想要实现的更具体的事情:
我希望能够使用我在第一个文件中写入的函数并将其转储,然后在第二个文件中读取它,并能够使用此函数查询记录中任何人的平均分数。
这就是我所拥有的:
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"))
执行此代码时,我遇到此错误File "test.py", line 13, in <module>
B = name_score_function(record)
NameError: name 'record' is not defined
我非常感谢您的帮助和耐心。
答案 0 :(得分:4)
谢谢。看起来以下可以解决问题。
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"))
答案 1 :(得分:3)
嗯。您需要以与编写它相同的方式阅读它 - 将其嵌套在open子句中:
import dill as pickle
with open('name_model.pkl' ,'rb') as f:
B = pickle.load(f)