Exemplo n.º 1
0
 def load_data(self, ):
     temp_cfy = Classify()
     temp_cfy.add_jieba_dict(self.words)
     p_wls = [temp_cfy.doc2words(c) for c in load_positive_contents()]
     n_wls = [temp_cfy.doc2words(c) for c in load_negative_contents()]
     n = len(p_wls) * 80 / 100
     self.train_data = p_wls[:]
     self.test_data = n_wls
     self.test_y = [-1] * len(n_wls)
     for wl in p_wls[n:]:
         self.test_data.append(wl)
         self.test_y.append(1)
Exemplo n.º 2
0
 def test(self, params):
     fnum, na, nu, gs = params
     cfy = Classify(fnum, na, nu, gs)
     cfy.add_jieba_dict(self.words)
     cfy.use_dict(self.words)
     cfy.train_by_wls(self.train_data)
     y_pred = cfy.predict_by_wls(self.test_data)
     r = zip(map(int, y_pred), self.test_y)
     tp = r.count((1, 1))
     fp = r.count((1, -1))
     fn = r.count((-1, 1))
     if (tp == 0):
         precision = 0
         recall = 0
         f1 = 0
     else:
         precision = tp * 1.0 / (tp + fp)
         recall = tp * 1.0 / (tp + fn)
         f1 = 5 * precision * recall / (4 * precision + recall)
     print(fnum, na, nu, gs, precision, recall, f1)
     return (precision, recall, f1)