背景:我刚刚开始使用scikit-learn,并在页面底部阅读了关于joblib和pickle的信息。
使用joblib替换pickle(joblib.dump和joblib.load)可能会更有趣,这在大数据上效率更高,但只能在磁盘而不是字符串中进行酸洗。
我阅读了有关Pickle的问答,Python的Pickle 常见用例,想知道这里的社区是否可以共享joblib和pickle之间的差异?一个人何时应该使用另一个?
背景:我刚刚开始使用scikit-learn,并在页面底部阅读了关于joblib和pickle的信息。
使用joblib替换pickle(joblib.dump和joblib.load)可能会更有趣,这在大数据上效率更高,但只能在磁盘而不是字符串中进行酸洗。
我阅读了有关Pickle的问答,Python的Pickle 常见用例,想知道这里的社区是否可以共享joblib和pickle之间的差异?一个人何时应该使用另一个?
Answers:
mmap_mode="r"
。Joblib
over Pickle
?Joblib
我们应该考虑哪些缺点?我最近才听说Joblib
过,这听起来很有趣。
感谢Gunjan给我们这个脚本!我为Python3结果修改了它
#comapare pickle loaders
from time import time
import pickle
import os
import _pickle as cPickle
from sklearn.externals import joblib
file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'database.clf')
t1 = time()
lis = []
d = pickle.load(open(file,"rb"))
print("time for loading file size with pickle", os.path.getsize(file),"KB =>", time()-t1)
t1 = time()
cPickle.load(open(file,"rb"))
print("time for loading file size with cpickle", os.path.getsize(file),"KB =>", time()-t1)
t1 = time()
joblib.load(file)
print("time for loading file size joblib", os.path.getsize(file),"KB =>", time()-t1)
time for loading file size with pickle 79708 KB => 0.16768312454223633
time for loading file size with cpickle 79708 KB => 0.0002372264862060547
time for loading file size joblib 79708 KB => 0.0006849765777587891
我遇到了同样的问题,所以我尝试了这个问题(使用Python 2.7),因为我需要加载一个大型的pickle文件
#comapare pickle loaders
from time import time
import pickle
import os
try:
import cPickle
except:
print "Cannot import cPickle"
import joblib
t1 = time()
lis = []
d = pickle.load(open("classi.pickle","r"))
print "time for loading file size with pickle", os.path.getsize("classi.pickle"),"KB =>", time()-t1
t1 = time()
cPickle.load(open("classi.pickle","r"))
print "time for loading file size with cpickle", os.path.getsize("classi.pickle"),"KB =>", time()-t1
t1 = time()
joblib.load("classi.pickle")
print "time for loading file size joblib", os.path.getsize("classi.pickle"),"KB =>", time()-t1
的输出是
time for loading file size with pickle 1154320653 KB => 6.75876188278
time for loading file size with cpickle 1154320653 KB => 52.6876490116
time for loading file size joblib 1154320653 KB => 6.27503800392
根据这个作业库,这三个模块中的cPickle和Pickle模块效果更好。谢谢