这是一次作业的笔记

要求及数据集

https://www.kaggle.com/c/naivebayes-21/overview
The overall goal of the exercise is to get hands-on experience with the implementation of a popular machine learning scheme and to work on a real-world task. The task is to implement an improved version of the Naive Bayes algorithm that is able to predict the domain - one of Archaea, Bacteria, Eukaryota or Virus - from the abstract of research papers about proteins taken from the MEDLINE database. You will then apply your implementation on a test set without class labels and hand in the predictions of your implementation.

思路

朴素贝叶斯 + 拉普拉平滑 + 禁用词

具体实践

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pandas as pd
import math

commonWords = ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself',
'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself',
'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these',
'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do',
'does', 'did', 'doing', 'a', 'an',
'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with',
'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to',
'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once',
'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most',
'other', 'some', 'such', 'no', 'nor',
'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don',
'should', 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', 'couldn', 'didn', 'doesn', 'hadn',
'hasn', 'haven', 'isn', 'ma', 'mightn', 'mustn', 'needn', 'shan', 'shouldn', 'wasn', 'weren', 'won',
'wouldn']

class myNaiveBayesAbstract:
def __init__(self, trainingSetName: str, labelName: str, abstractName: str):
print('init trainingSet')
self.trainingSet = self.readTrainingSet(trainingSetName)
self.trainingLabels = self.trainingSet[labelName]
self.trainingAbstract = self.trainingSet[abstractName]
self.trainingSize = len(self.trainingSet)

print('init labelData')
labelSet = list(set(self.trainingLabels))
self.trLabelDic = {}
for i in labelSet:
self.trLabelDic[i] = {}
self.trLabelDic[i]['count'] = 0
self.trLabelDic[i]['word'] = []

print(self.trLabelDic)

def isCommonWord(self, word):
if word in commonWords:
return True
else:
return False

# read train set

def readTrainingSet(self, name: str):
print('readTrainSet: ', name)
return pd.read_csv(name)

def collectAbstract(self, words, label):
if label in self.trLabelDic:
self.trLabelDic[label]['count'] += 1
self.trLabelDic[label]['word'] += words
else:
print("error can't find " + label + " in trLabelDic.")

def calculateProbability(self, testData):
tempDic = {}
for key in self.trLabelDic.keys():
tempDic[key] = {}

# print('tempDic', tempDic)
# print(len(tempDic))

for key in self.trLabelDic.keys():
wordsCollection = self.trLabelDic[key]['word']

# print(len(wordsCollection))
for word in wordsCollection:
# print(len(word))
# print('word: ', word)
if not self.isCommonWord(word):
if word in tempDic[key]:
tempDic[key][word] += 1
else:
tempDic[key][word] = 1

# print(tempDic)

# prepare to Laplacian Smoothing
uniqueWords = []
for key in self.trLabelDic.keys():
for tempword in tempDic[key]:
if tempword not in uniqueWords:
uniqueWords.append(tempword)
# else:
# print(tempword)

# print('uniqueWords: ', len(uniqueWords))

testSize = len(testData)
testUniSize = 0

for i in range(0, testSize):
words = testData[i].split(" ")
for word in words:
if not self.isCommonWord(word):
if word not in uniqueWords:
testUniSize += 1

# print('testUniSize: ', testUniSize)

totalUniCount = testUniSize + len(uniqueWords)

testLabels = []

for i in range(0, testSize):
words = testData[i].split(" ")

probability = {}

for key in self.trLabelDic.keys():
probability[key] = math.log(
self.trLabelDic[key]['count'] / self.trainingSize, 10)

for word in words:
if not self.isCommonWord(word):
# Laplacian Smoothing
for key in self.trLabelDic.keys():
totalCount = len(
self.trLabelDic[key]['word']) + totalUniCount

if word in tempDic[key]:
probability[key] = probability[key] + math.log(
(tempDic[key][word] + 1) / (totalCount), 10)
else:
probability[key] = probability[key] + math.log(
1 / (totalCount), 10)

max_num = max(probability, key=probability.get)

# print('max_num: ' + str(probability[max_num]))

# max_num = max(pa, pb, pe, pv)
for key in self.trLabelDic.keys():
# print(probability[key])
if probability[max_num] == probability[key]:
testLabels = testLabels + [key]
# print('label: ' + key)

return testLabels

def classify(self, testData):
print('classify')

testLabels = self.calculateProbability(testData)

return testLabels

def predict(self, abstracts):
for i in range(0, self.trainingSize):
words = self.trainingAbstract[i].split(' ')
# print(words)

tempLabel = self.trainingLabels[i]
self.collectAbstract(words, tempLabel)

# print(self.trLabelDic['A']['count'])
# print(self.trLabelDic['B']['count'])
# print(self.trLabelDic['E']['count'])
# print(self.trLabelDic['V']['count'])

return self.calculateProbability(abstracts)

naiveBayesAbstract = myNaiveBayesAbstract('trg.csv', 'class', 'abstract')

test_set = pd.read_csv("tst.csv")
test_set['class'] = naiveBayesAbstract.predict(test_set["abstract"])

test_set.drop(['abstract'], axis=1).to_csv('out.csv', index=False)
test_labels = list(set(test_set['class']))

for key in test_labels:
print(key + ': ' + str(len(test_set[test_set['class'] == key])))