我正在尝试使用 pickle 来保存通过其他数据集生成的几个大型数据集。转储时它不会给我任何错误,但是当我尝试加载这些数据集时,pickle 退出并出现 eof 错误。以下是我运行以保存数据集的代码:
from scipy.stats.mstats import mode
trainingSetCustomers = pd.DataFrame({'visitFrequency': trainingSet.size(),'totalAmountSpent': trainingSet['amountSpent'].sum(),'totalProducts': trainingSet['productCount'].sum(),'firstVisit': trainingSet['visitDate'].min(),'lastVisit': trainingSet['visitDate'].max(),'visitType':trainingSet['visitType'].apply(f),'country': trainingSet['country'].apply(f),'isReferred':trainingSet['isReferred'].sum()}).reset_index()
p2 = pickle.Pickler(open("trainingSetCustomers.p","wb")) #finaldatasetYear1AndYear2 #trainingset groupedCustomersWithDates dfOrdersNew groupedCustomersNew
p2.clear_memo()
p2.dump(trainingSetCustomers)
print "Training Set saved" #Done
trainingResultSetCustomers = pd.DataFrame({'futureVisitFrequency': trainingResultSet.size(),'futureTotalAmountSpent': trainingResultSet['amountSpent'].sum(),'futureTotalProducts': trainingResultSet['productCount'].sum(),'firstVisit': trainingResultSet['visitDate'].min(),'lastVisit': trainingResultSet['visitDate'].max(),'visitType':trainingResultSet['visitType'].apply(f),'country': trainingResultSet['country'].apply(f),'isReferred':trainingResultSet['isReferred'].sum()}).reset_index()
p3 = pickle.Pickler(open("trainingResultSetCustomers.p","wb")) #finaldatasetYear1AndYear2 #trainingset groupedCustomersWithDates dfOrdersNew groupedCustomersNew
p3.clear_memo()
p3.dump(trainingResultSetCustomers)
print "trainingresult set saved" #Done
这运行没有任何错误并打印消息。但是当我运行以下代码时:
trainingResultSetCustomers = pickle.load( open( "trainingResultSetCustomers.p", "rb" ) )
它给了我一个 EoFError。我需要存储 4 个这样的测试集,我真的很困惑为什么会这样。如果这有什么不同,我会通过 ssh 在 IPython notebook 上运行它。此外,如果我只尝试 5 行,它就可以完美运行
数据结构:从代码中可以看出,这个数据框是由分组对象的属性生成的。
这是我得到的错误:
EOFError Traceback (most recent call last)
<ipython-input-10-86d38895c564> in <module>()
5 p = pickle.Pickler(o) #finaldatasetYear1AndYear2 #trainingset groupedCustomersWithDates dfOrdersNew groupedCustomersNew
6 p.clear_memo()
----> 7 trainingset = pickle.load(o)
8 o.close()
9 print "done"
/usr/lib/python2.7/pickle.pyc in load(file)
1376
1377 def load(file):
-> 1378 return Unpickler(file).load()
1379
1380 def loads(str):
/usr/lib/python2.7/pickle.pyc in load(self)
856 while 1:
857 key = read(1)
--> 858 dispatch[key](self)
859 except _Stop, stopinst:
860 return stopinst.value
/usr/lib/python2.7/pickle.pyc in load_eof(self)
878
879 def load_eof(self):
--> 880 raise EOFError
881 dispatch[''] = load_eof
882