我已经运行代码来加载pickle保存的变量。这是我的密码
import pickle
app = Flask(__name__)
nb_model = pickle.load(open('model_pickle.pkl', 'rb'))
这是一个物体
class NaiveBayesSentiment():
def __init__(self, datasentimen, stopword, norm, datatest):
self.datasentimen = datasentimen
self.stop_word = stopword.T.values
self.norm_words = pd.Series(norm['kata kbbi'].values,index=norm['kata singkatan']).to_dict()
self.datatest = pd.DataFrame([datatest], columns=['comment'])
def cleansing_data(self, datasentimen):
datasentimen = datasentimen.replace('\n', ' ',regex = True)
datasentimen = datasentimen.str.replace("[^a-zA-Z]+", " ", regex = True)
datasentimen = datasentimen.str.replace('\s{2,}', ' ', regex=True)
datasentimen = datasentimen.str.lower()
return datasentimen
cleanposdata = cleansing_data(datasentimen[datasentimen['class sentiment'] == 'positif']['comment'])
cleannegdata = cleansing_data(datasentimen[datasentimen['class sentiment'] == 'negatif']['comment'])
if __name__ == "__main__":
app.run(debug=True)
我得到的错误如下:
nb_model = pickle.load(open('model_pickle.pkl', 'rb'))
AttributeError: Can't get attribute 'NaiveBayesSentiment' on <module '__main__' from 'app.py'>
这是我在Pyton中执行pickle时的代码
nb_model = NaiveBayesSentiment(datasentimen, stopword, norm, datatest)
with open('model_pickle.pkl', 'wb') as pickle_out:
pickle.dump(nb_model, pickle_out)
with open('model_pickle.pkl', 'rb') as pickle_in:
unpickled_nb_model = pickle.load(pickle_in)
print(unpickled_nb_model.finalclassification())
我该怎么办?请帮帮我
Python的
pickle
序列化数据,但不序列化类或函数。它们只保存了名称,所以反序列化时不存在具有相同名称的类或函数,出现上述错误。因此,同样要加载pickle对象,必须存在相同的定义添加:
这意味着调用
pickle.load
时必须定义NaiveBayesSentiment
。换句话说,导入一个具有NaiveBayesSentiment
的模块相关问题 更多 >
编程相关推荐