【问题标题】:Problem using a dictionary of numpy array(Indexing it wrong)使用 numpy 数组字典的问题(索引错误)
【发布时间】: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.dataX.target 是什么?

标签: python python-3.x numpy dictionary


【解决方案1】:

代码至少有两个错误:

1) 在行中

freq = dict.fromkeys(self._dict, nrows * [0])

您使用相同的列表初始化freq 字典中的所有项目。 nrows * [0] 被评估一次以创建一个列表,然后将其传递给 dict.fromkeys() 函数。对这一列表的引用分配给freq 字典中的所有键。无论您选择哪个键,您都会获得对同一列表的引用。这是 Python 中的常见问题。

相反,您可以使用字典推导来创建具有单独列表的条目:

freq = {key:nrows*[0] for key in self._dict}

2) 您使用i 作为vec 的索引变量,但您打算使用j

vec[j] += 1

使用具有描述性名称的变量将有助于避免此类混淆。

【讨论】:

  • 谢谢,克雷格我的问题是初始化,确实键引用了同一个列表。 i 索引是正确的。
猜你喜欢
  • 2019-07-30
  • 2018-05-24
  • 1970-01-01
  • 2018-11-21
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多