Exemplo n.º 1
0
import random
import os
import json
import numpy as np
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import zero_one_loss
from sklearn.naive_bayes import MultinomialNB

import Bayes as bayes

base_dir = os.path.dirname(__file__)

n_estimators = 500
learning_rate = 1.
vocabList = bayes.build_key_word(os.path.join(base_dir, "train.txt"))
line_cut, label = bayes.loadDataSet(os.path.join(base_dir, "train.txt"))
train_mood_array = bayes.setOfWordsListToVecTor(vocabList, line_cut)
test_word_array = []
test_word_arrayLabel = []
testCount = 100  # 从中随机选取100条用来测试,并删除原来的位置
for i in range(testCount):
    try:
        randomIndex = int(random.uniform(0, len(train_mood_array)))
        test_word_arrayLabel.append(label[randomIndex])
        test_word_array.append(train_mood_array[randomIndex])
        del (train_mood_array[randomIndex])
        del (label[randomIndex])
    except Exception as e:
        print(e)
multi = MultinomialNB()
Exemplo n.º 2
0
from pylab import mpl
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB

from sklearn.preprocessing import label_binarize

if __name__ == '__main__':
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    fig = plt.figure()
    ax = fig.add_subplot(111)
    n_estimators = 500
    learning_rate = 1.
    vocabList = bayes.build_key_word("../train/train.txt")
    line_cut, label = bayes.loadDataSet("../train/train.txt")
    train_mood_array = bayes.setOfWordsListToVecTor(vocabList, line_cut)
    test_word_array = []
    test_word_arrayLabel = []
    testCount = 100  # 从中随机选取100条用来测试,并删除原来的位置
    for i in range(testCount):
        try:
            randomIndex = int(random.uniform(0, len(train_mood_array)))
            test_word_arrayLabel.append(label[randomIndex])
            test_word_array.append(train_mood_array[randomIndex])
            del (train_mood_array[randomIndex])
            del (label[randomIndex])
        except Exception as e:
            print(e)
    multi = MultinomialNB()