【发布时间】:2019-08-24 17:27:45
【问题描述】:
我正在尝试使用 python 和 numpy 从头开始编写高斯朴素贝叶斯代码,但在创建词频表时遇到了一些麻烦。
我有一个包含 N 个单词的字典作为键,这 N 个单词中的每一个都有一个关联的 numpy 数组。
例子:
freq_table['subject'] -> Vector of ocurrences of this word of length nrows where nrows is the size of the dataset.
所以对于我正在做的数据集中的每一行:
freq_table[WORD][i] += 1
def train(self, X):
# Creating the dictionary
self.dictionary(X.data[:100])
# Calculating the class prior probabilities
self.p_class = self.prior_probs(X.target)
# Calculating the likelihoods
nrows = len(X.data[:100])
freq = dict.fromkeys(self._dict, nrows * [0])
for doc, target, i in zip(X.data[:2], X.target[:2], range(2)):
print('doc [%d] out of %d' % (i, nrows))
words = preprocess(doc)
print(len(words), i)
for j, w in enumerate(words):
print(w, j)
# Getting the vector assigned by the word w
vec = freq[w]
# In the ith position (observation id) sum one of ocurrence
vec[i] += 1
print(freq['subject'])
输出是
Dictionary length 4606
doc [0] out of 100
43 0
wheres 0
thing 1
subject 2
nntppostinghost 3
racwamumdedu 4
organization 5
university 6
maryland 7
college 8
lines 9
wondering 10
anyone 11
could 12
enlighten 13
sports 14
looked 15
early 16
called 17
bricklin 18
doors 19
really 20
small 21
addition 22
front 23
bumper 24
separate 25
anyone 26
tellme 27
model 28
engine 29
specs 30
years 31
production 32
history 33
whatever 34
funky 35
looking 36
please 37
email 38
thanks 39
brought 40
neighborhood 41
lerxst 42
[43, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
看来我的字典和向量索引错误。
“主题”这个词不应该出现 43 或 53 次,因为文档/行中预处理词的长度是 43/53。
【问题讨论】:
-
这里的
X.data和X.target是什么?
标签: python python-3.x numpy dictionary