为Twitter实现naivebayes

2024-05-18 23:42:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组tweet的数据集,其中包含与疫苗认知相关的关键字。这些词包括

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

我希望能够将一条新的推文分类为支持疫苗、抗疫苗,或者两者都不是。我知道天真的贝耶斯是一种方法。在

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

如何实现naivebayes?从Sklearn的网站上看,我的选择是多项式和高斯的,但我不知道该用哪一个。在


Tags: 数据方法算法分类关键字tweetalgshot
1条回答
网友
1楼 · 发布于 2024-05-18 23:42:08

下面是一个分类器的简单实现,它可以对5种疾病进行分类。

它有两个文件:

  1. 列车文件(火车.txt)

  2. 测试文件(测试.txt)

基本上,根据你的问题,你应该把你的微博放在火车文件里。以及要在测试文件中分类的tweet。在

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

列车文件内容:[火车.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.

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

^{pr2}$

分类代码:[分类器.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

相关问题 更多 >

    热门问题