1def do_rnn_wordbag(trainX, testX, trainY, testY): 2 y_test=testY 3 #trainX = pad_sequences(trainX, maxlen=100, value=0.) 4 #testX = pad_sequences(testX, maxlen=100, value=0.) 5 # Converting labels to binary vectors 6 trainY = to_categorical(trainY, nb_classes=2) 7 testY = to_categorical(testY, nb_classes=2) 8 9 # Network building 10 net = tflearn.input_data([None, 100]) 11 net = tflearn.embedding(net, input_dim=1000, output_dim=128) 12 net = tflearn.lstm(net, 128, dropout=0.1) 13 net = tflearn.fully_connected(net, 2, activation='softmax') 14 net = tflearn.regression(net, optimizer='adam', learning_rate=0.005, 15 loss='categorical_crossentropy') 16 17 # Training 18 model = tflearn.DNN(net, tensorboard_verbose=0) 19 model.fit(trainX, trainY, validation_set=0.1, show_metric=True, 20 batch_size=1,run_id="uba",n_epoch=10) 21 22 y_predict_list = model.predict(testX) 23 #print y_predict_list 24 25 y_predict = [] 26 for i in y_predict_list: 27 #print i[0] 28 if i[0] >= 0.5: 29 y_predict.append(0) 30 else: 31 y_predict.append(1) 32 33 print(classification_report(y_test, y_predict)) 34 print metrics.confusion_matrix(y_test, y_predict) 35 36 print y_train 37 38 print "ture" 39 print y_test 40 print "pre" 41 print y_predict
传统方法贝叶斯:
1def do_nb(x_train, x_test, y_train, y_test): 2 gnb = GaussianNB() 3 gnb.fit(x_train,y_train) 4 y_pred=gnb.predict(x_test) 5 print(classification_report(y_test, y_pred)) 6 print metrics.confusion_matrix(y_test, y_pred)
传统方法hmm:
1def do_hmm(trainX, testX, trainY, testY): 2 T=-580 3 N=2 4 lengths=[1] 5 X=[[0]] 6 print len(trainX) 7 for i in trainX: 8 z=[] 9 for j in i: 10 z.append([j]) 11 #print z 12 #X.append(z) 13 X=np.concatenate([X,np.array(z)]) 14 lengths.append(len(i)) 15 16 #print lengths 17 #print X.shape 18 19 20 21 remodel = hmm.GaussianHMM(n_components=N, covariance_type="full", n_iter=100) 22 remodel.fit(X, lengths) 23 24 y_predict=[] 25 for i in testX: 26 z=[] 27 for j in i: 28 z.append([j]) 29 y_pred=remodel.score(z) 30 print y_pred 31 if y_pred < T: 32 y_predict.append(1) 33 else: 34 y_predict.append(0) 35 y_predict=np.array(y_predict) 36 37 print(classification_report(testY, y_predict)) 38 print metrics.confusion_matrix(testY, y_predict) 39 40 print testY 41 print y_predict