垃圾邮件分类器的“空词汇”错误

时间:2018-08-15 08:49:29

标签: python machine-learning anaconda jupyter-notebook vectorization

我正在尝试创建垃圾邮件分类器,并且我使用以下代码来做到这一点。

但是问题是我收到一条错误消息,指出我的程序有一个“空库”,并且“我的文档除了停用词外什么都没有”。

您知道是什么原因造成的吗?

import os
import io
from pandas import DataFrame
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB


def readFiles(path):
    for root, dirnames, filenames in os.walk(path):
        for filename in filenames:
            path = os.path.join(root, filename)

            inBody = False
            lines = []
            f = io.open(path, 'r', encoding='latin1')
            for line in f:
                if inBody:
                    lines.append(line)
                elif line == '\n':
                    inBody = True
            f.close()
            message = '\n'.join(lines)
            yield path, message


def dataFrameFromDirectory(path, classification):
    rows = []
    index = []
    for filename, message in readFiles(path):
        rows.append({'message': message, 'class': classification})
        index.append(filename)

    return DataFrame(rows, index=index)


data = DataFrame({'message': [], 'class': []})

data = data.append(dataFrameFromDirectory('C:/Users/Tapan/Desktop/enron/enron1/spam', 'spam'))
data = data.append(dataFrameFromDirectory('C:/Users/Tapan/Desktop/enron/enron1/ham', 'ham'))

vectorizer = CountVectorizer()
counts = vectorizer.fit_transform(data['message'].values)

classifier = MultinomialNB()
targets = data['class'].values
classifier.fit(counts, targets)

examples = ['prescribed online and shipped overnight to your door', 'vastar owns 68% of the gross production']
examples_counts = vectorizer.transform(examples)
predictions = classifier.predict(examples_counts)

print(predictions)

0 个答案:

没有答案