使用来自2个不同类的pickle加载数据

时间:2015-07-08 23:43:35

标签: python nltk pickle

我是python的新手,我学习了pickle在类之间传递数据。我有2个不同的类,第一个是分类器类,我想训练和分类我的所有数据集。数据集非常大,包含50k的推文,我使用nltk朴素贝叶斯分类器进行分类。我有另外一个来自不同类的数据,我想把它加载到同一个类中。

我曾尝试将其他类的数据加载到分类器类中。然后我将所有数据与分类器一起腌制。我设法做到这一点,但我想避免每次运行我的系统时都必须对我的数据集进行分类。

这是我从其他类加载数据并将其与分类器一起回收的方法。

#This is the snipped coding in my first class
k = open('key_word1.pickle','wb')
pickle.dump(key_word,k)
pickle.dump(final,k)
#print(key_word)
k.close()



#This is snipped coding in my second class
import pickle
j = open('key_word1.pickle','rb')
key_word = pickle.load(j)
final = pickle.load(j)
#print(key_word)
#print(final)
j.close()

f = open('data.pickle', 'wb')
pickle.dump(featureList, f)
pickle.dump(classifier, f)
pickle.dump(key_word, f)
pickle.dump(final, f)
#print(classifier)
f.close()

#this is snipped coding in my third class
#i load pickle from second class that have combine with first class pickle
f = open('data.pickle', 'rb')
featureList = pickle.load(f)
data = pickle.load(f)
key_word = pickle.load(f)
final = pickle.load(f)
f.close()

我可以做一些这样的事情,以避免每次运行系统时我的数据都要经过第二课吗?我试过了,我认为一次只能加载一个泡菜而另一个加载泡菜会被忽略。或许我在编码时做错了。

#I try to load from first class and second class at the same time

import pickle
j = open('key_word1.pickle','rb')
key_word = pickle.load(j)
final = pickle.load(j)
#print(key_word)
#print(final)
j.close()


f = open('data.pickle', 'rb')
featureList = pickle.load(f)
data = pickle.load(f)
f.close()

0 个答案:

没有答案