def test_pickle(self): cf_cv = ClassifierCv(self.labels, self.texts) name = 'MultinomialNB' metric = 'f1' cf_cv.train_save_metrics([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('clf', MultinomialNB(alpha=.05))], metric, name, self.test_dir, self.test_dir) savefile = os.path.join(self.test_dir, 'clf_cv.cv') cf_cv.pickle(savefile) self.assertTrue(os.path.isfile(savefile))
def test_unpickle(self): cf_cv = ClassifierCv(self.labels, self.texts) name = 'MultinomialNB' metric = 'f1' cf_cv.train_save_metrics([('vect', CountVectorizer()), ('tfidf', TfidfTransformer()), ('clf', MultinomialNB(alpha=.05))], metric, name, self.test_dir, self.test_dir) savefile = os.path.join(self.test_dir, 'clf_cv.cv') cf_cv.pickle(savefile) new_cf_cv = ClassifierCv.unpickle(savefile) texts = ['dont know that', 'nice good and bad'] predicted_labels_orig = cf_cv.predict(texts, proba=True) predicted_labesl_new = new_cf_cv.predict(texts, proba=True) self.assertTrue(all(predicted_labels_orig == predicted_labesl_new))