def fit_predict(self, sess, input_data): total_loss = [] collect_time = [] total_pred = [] total_labels = [] step = 0 try: for batch_data in input_data: batch_x, batch_sNum, batch_wNum, batch_labels = batch_data[ 0], batch_data[1], batch_data[2], batch_data[3] step += 1 total_labels.extend(batch_labels) feed_dict = { self.model.ph_input: batch_x, self.model.ph_labels: batch_labels, self.model.ph_sNum: batch_sNum, self.model.ph_wNum: batch_wNum, self.model.ph_train: False, } start_stamp = time.time() global_step, summary, loss, y_pred = sess.run( [ self.model.global_step, self.merged, self.model.loss, self.model.prediction ], feed_dict=feed_dict) self.test_writer.add_summary(summary, step + global_step) self.test_writer.flush() end_stamp = time.time() time_stamp = end_stamp - start_stamp collect_time.append(time_stamp) total_loss.append(loss) total_pred.extend(y_pred) except: traceback.print_exc() exit() total_labels = np.argmax(total_labels, axis=1) correct_predictions = float(sum(total_pred == total_labels)) accuracy = correct_predictions / float(len(total_labels)) pre, recall, f1 = score(total_pred, total_labels) avg_loss = np.mean(total_loss) return avg_loss, accuracy, pre, recall, f1
def classifier(train_X, train_y, test_X, test_y): # train_X, train_y, test_X, test_y = np.array(train_X), np.array(train_y), np.array(test_X), np.array(test_y) print(np.shape(train_X)) # clf = OneVsRestClassifier(SVC(kernel='linear')) clf = svm.SVC() # clf = LogisticRegression() clf.fit(train_X, train_y) predict_y = clf.predict(test_X) print(predict_y) # precision = precision_score(test_y,predict_y) acc = metric_acc(test_y, predict_y) pre, recall, f1 = score(test_y, predict_y) print("acc:", acc) print("pre:", pre) print("recall:", recall) print("f1:", f1) return acc, recall, f1