为 Twitter 数据实施朴素贝叶斯

Implementing Naive Bayes for Twitter data

我有一个推文数据集,其中包含与疫苗认知相关的关键字。这些包括像

这样的词

[jab, shot, measles, MMR, vaccine, autism,...].

我希望能够将一条新推文归类为支持疫苗、反对疫苗或两者都不是。我知道朴素贝叶斯是一种方法。

我宁愿使用 SKlearns 库来实现分类算法,因为这些算法比我能写的更健壮。

如何实现朴素贝叶斯?从 Sklearn 的网站上看,我的选择似乎是多项式和高斯,但我不确定该使用哪个。

下面是对5种疾病进行分类的分类器的简单实现

它有两个文件:

  1. 训练文件 (train.txt)

  2. 测试文件(test.txt)

基本上,根据您的问题,您的推文应该在 Train 文件中。以及您要在测试文件中分类的推文。

[注意:您也可以使用 CSV 或 JSON 表示来加载您的数据集,为了简单起见,我使用了文本文件。]

训练文件的内容:[train.txt]

A highly contagious virus spread by coughing, sneezing or direct contact with skin lesions.
A contagious liver disease often caused by consuming contaminated food or water. It is the most common vaccine-preventable travel disease.
A serious liver disease spread through contact with blood or body fluids. The hepatitis B virus can cause liver cancer and possible death.
A group of over 100 viruses that spreads through sexual contact. HPV strains may cause genital warts and lead to cervical cancer.
A potentially fatal bacterial infection that strikes an average of 1,500 Americans annually.

测试文件内容:[test.txt]

died due to liver cancer.

分类码:[classifier.py]

import codecs
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
trainfile = 'train.txt'
testfile = 'test.txt'
word_vectorizer = CountVectorizer(analyzer='word')
trainset = word_vectorizer.fit_transform(codecs.open(trainfile,'r','utf8'))
tags = ['CHICKEN POX','HEPATITIS A','HEPATITIS B','Human papillomavirus','MENINGITIS']
mnb = MultinomialNB()
mnb.fit(trainset, tags)
codecs.open(testfile,'r','utf8')
testset = word_vectorizer.transform(codecs.open(testfile,'r','utf8'))
results = mnb.predict(testset)
print results