【发布时间】:2019-12-15 01:46:14
【问题描述】:
我正在研究一种使用 Keras 对邮件进行分类的方法。我阅读了已经分类的邮件,将它们标记化以创建一个链接到文件夹的字典。
所以我用熊猫创建了一个数据框:
data = pd.DataFrame(list(zip(lst, lst2)), columns=['text', 'folder'])
文本列是电子邮件中存在的所有单词所在的位置,文件夹列是电子邮件所属的类(路径)。
感谢我创建了我的模型,它给了我这些结果:
3018/3018 [==============================] - 0s 74us/步 - 损耗:0.0325 - acc: 0.9950 - val_loss:0.0317 - val_acc:0.9950
100 纪元
我的模型的评价
755/755 [===============================] - 0s 28us/步 测试成绩:0.0316697002592071 测试精度:0.995000006268356
所以最后我需要做的是预测随机邮件的类别,但model.predict_classes(numpy.array) 调用给了我一个充满整数的二维数组,但我仍然不知道它属于哪个“文件夹/类别”。
这是我的代码:
#lst contains all the words in the mail
#lst2 the class/path of lst
data = pd.DataFrame(list(zip(lst, lst2)), columns=['text', 'folder'])
train_size = int(len(data) * .8)
train_posts = data['text'][:train_size]
train_tags = data['folder'][:train_size]
test_posts = data['text'][train_size:]
test_tags = data['folder'][train_size:]
num_labels = 200 #The numbers of total classes
#the way I tokenize and encode my data
tokenizer = Tokenizer(num_words=len(lst))
tokenizer.fit_on_texts(pd.concat([train_posts, test_posts], axis = 1))
x_train = tokenizer.texts_to_matrix(train_posts, mode=TOKENISER_MODE)
x_test = tokenizer.texts_to_matrix(test_posts, mode=TOKENISER_MODE)
encoder = preprocessing.LabelBinarizer()
encoder.fit(train_tags)
y_train = encoder.transform(train_tags)
y_test = encoder.transform(test_tags)
#my model, vocab_size = len(lst) = number of the words present in the mails
model = Sequential()
model.add(Dense(16, input_shape=(vocab_size,)))
model.add(Activation('elu'))
model.add(Dropout(0.2))
model.add(Dense(32))
model.add(Activation('elu'))
model.add(Dropout(0.2))
model.add(Dense(16))
model.add(Activation('elu'))
model.add(Dropout(0.2))
model.add(Dense(num_labels))
model.add(Activation('sigmoid'))
model.summary()
#compile training and evaluate
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=100, verbose=1, validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, batch_size=batch_size, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
#read the random file
sentences = read_files("mail.eml")
sentences = ' '.join(sentences)
sentences = sentences.lower()
salut = unidecode.unidecode(sentences)
#predict
pred = model.predict_classes(salut, batch_size=batch_size, verbose=1)
print(pred)
pred的实际输出:
[125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125 125]
我不知道为什么,但每次启动时的输出总是充满相同的数字。我正在寻找的输出是:
['药物/艾滋病/',帮助/,项目/分类/]
按正确的概率排序。 read_files 调用只是一个读取邮件并返回邮件中所有单词列表的函数。
有没有办法用model.predict_classes() 获取邮件的类,还是我需要使用别的东西?
【问题讨论】:
-
一个充满
125的数组肯定表示某些事情是非常错误的——你怎么能从这样的输出中取出任何有意义的东西呢?你有多少课(即num_labels是什么)?这是一个标准的多类分类问题(即样本只能属于一个类)吗? -
num_labels 等于 200 是我拥有的类数
-
请澄清(如上所述)如果您处于多类别设置(样本只能属于一个类别)或多标签设置(样本可以属于多个类别)同时上课)。
-
只上一节课
-
所以您不是处于多标签设置中;然后我的答案成立...
标签: python machine-learning keras nlp classification