我有朴素的贝叶斯分类器类,该类已经实现了tf-idf,将邮件分类为垃圾邮件或火腿。但是,tf-idf部分仅计算训练数据集的tf-idf。
这是分类器类 SpamClassifier(object)类: def init (自身,traindata): self.mails,self.labels = traindata ['Review'],traindata ['Polarity']
def train(self):
self.calc_TF_and_IDF()
self.calc_TF_IDF()
def calc_TF_and_IDF(self):
noOfMessages = self.mails.shape[0]
self.spam_mails, self.ham_mails = self.labels.value_counts()[1], self.labels.value_counts()[0]
self.total_mails = self.spam_mails + self.ham_mails
self.spam_words = 0
self.ham_words = 0
self.tf_spam = dict()
self.tf_ham = dict()
self.idf_spam = dict()
self.idf_ham = dict()
for i in range(noOfMessages):
message = self.mails[i]
count = list() #To keep track of whether the word has ocured in the message or not.
#For IDF
for word in message:
if self.labels[i]:
self.tf_spam[word] = self.tf_spam.get(word, 0) + 1
self.spam_words += 1
else:
self.tf_ham[word] = self.tf_ham.get(word, 0) + 1
self.ham_words += 1
if word not in count:
count += [word]
for word in count:
if self.labels[i]:
self.idf_spam[word] = self.idf_spam.get(word, 0) + 1
else:
self.idf_ham[word] = self.idf_ham.get(word, 0) + 1
def calc_TF_IDF(self):
self.prob_spam = dict()
self.prob_ham = dict()
self.sum_tf_idf_spam = 0
self.sum_tf_idf_ham = 0
for word in self.tf_spam:
self.prob_spam[word] = (self.tf_spam[word]) * log((self.spam_mails + self.ham_mails) \
/ (self.idf_spam[word] + self.idf_ham.get(word, 0)))
self.sum_tf_idf_spam += self.prob_spam[word]
for word in self.tf_spam:
self.prob_spam[word] = (self.prob_spam[word] + 1) / (self.sum_tf_idf_spam + len(list(self.prob_spam.keys())))
for word in self.tf_ham:
self.prob_ham[word] = (self.tf_ham[word]) * log((self.spam_mails + self.ham_mails) \
/ (self.idf_spam.get(word, 0) + self.idf_ham[word]))
self.sum_tf_idf_ham += self.prob_ham[word]
for word in self.tf_ham:
self.prob_ham[word] = (self.prob_ham[word] + 1) / (self.sum_tf_idf_ham + len(list(self.prob_ham.keys())))
self.prob_spam_mail, self.prob_ham_mail = self.spam_mails / self.total_mails, self.ham_mails / self.total_mails
def classify(self, processed_message):
pSpam, pHam = 0, 0
for word in processed_message:
if word in self.prob_spam:
pSpam += log(self.prob_spam[word])
else:
pSpam -= log(self.sum_tf_idf_spam + len(list(self.prob_spam.keys())))
if word in self.prob_ham:
pHam += log(self.prob_ham[word])
else:
pHam -= log(self.sum_tf_idf_ham + len(list(self.prob_ham.keys())))
pSpam += log(self.prob_spam_mail)
pHam += log(self.prob_ham_mail)
return pSpam >= pHam
def predict(self, testdata):
result = []
for (i, message) in enumerate(testdata):
#processed_message = process_message(message)
result.append(int(self.classify(message)))
return result
这就是我所谓的分类器
sc_tf_idf = SpamClassifier(traindata)
sc_tf_idf.train()
preds_tf_idf = sc_tf_idf.predict(testdata['Review'])
testdata['Predictions'] = preds_tf_idf
print(testdata['Polarity'], testdata['Predictions'])
我如何将分类器中的tf-idf计算应用于整个数据集(训练和测试数据集)?
答案 0 :(得分:0)
不应同时计算火车和测试数据的tf-idf。首先应将数据集分为训练和测试(以及验证),然后可以分别为每个数据集计算tf-idf。如果在数据分离之前计算tf-idf,则该模型将了解测试/验证数据的某些“功能”,并输出真正的乐观性能。您可以参考详细的答案here。
此外,您可以使用sklearn中的tfidfvectorizer。