为啥python说字典没有定义?

Posted

技术标签:

【中文标题】为啥python说字典没有定义?【英文标题】:Why is python saying dictionary is not defined?为什么python说字典没有定义? 【发布时间】:2018-08-21 23:03:06 【问题描述】:

我目前正在应用一些机器学习代码来分析来自 enron 数据集的电子邮件,使用 python 中的以下代码:

import os
import numpy as np
from collections import Counter
from sklearn.naive_bayes import MultinomialNB, GaussianNB, BernoulliNB
from sklearn.svm import SVC, NuSVC, LinearSVC

def make_dictionary(train_dir):
    emails = [os.path.join(train_dir,f) for f in os.listdir(train_dir)]    
    all_words = []       
    for mail in emails:    
        with open(mail) as m:
            for i,line in enumerate(m):
                if i == 2:
                    words = line.split()
                    all_words += words
    dictionary = Counter(all_words)
    return dictionary
list_to_remove = dictionary.keys()
for item in list_to_remove:
    if item.isalpha() == False:
        del dictionary[item]
    elif len(item) == 1:
        del dictionary[item]
    dictionary = dictionary.most_common(3000)
train_dir = 'train-mails'
dictionary = make_Dictionary(train_dir)


def extract_features(mail_dir):
    files = [os.path.join(mail_dir,fi) for fi in os.listdir(mail_dir)]
    features_matrix = np.zeros((len(files),3000))
    docID = 0;
    for fil in files:
        with open(fil) as fi:
            for i,line in enumerate(fi):
                if i == 2:
                    words = line.split()
                for word in words:
                  wordID = 0
                  for i,d in enumerate(dictionary):
                    if d[0] == word:
                      wordID = i
                      features_matrix[docID,wordID] = words.count(word)
            docID = docID + 1
        return features_matrix

train_labels = np.zeros(702)
train_labels[351:701] = 1
train_matrix = extract_features(train_dir)

model1 = MultinomialNB()
model2 = LinearSVC()
model1.fit(train_matrix,train_labels)
model2.fit(train_matrix,train_labels)

test_dir = 'test-mails'
test_matrix = extract_features(test_dir)
test_labels = np.zeros(260)
test_labels[130:260] = 1
result1 = model1.predict(test_matrix)
result2 = model2.predict(test_matrix)
print confusion_matrix(test_labels,result1)
print confusion_matrix(test_labels,result2)

但是,每次我运行它时,它都会说字典没有定义,我不知道为什么它不想工作。我已经缩进了需要它的区域,并且我导入了正确的模块,但它仍然不起作用。有关如何解决此问题的任何想法都会有所帮助。

【问题讨论】:

list_to_remove = dictionary.keys() 哪里来的字典?您可能想阅读Python scopes and namespaces 我实际上并没有编写 python 脚本,但是,在我看来,您在实际定义“dictionary = make_Dictionary(train_dir)”之前调用了“list_to_remove = dictionary.keys()”。不确定是不是这样,但也许? train_dir =...dictionary = ... 移动到list_to_remove = ... 行上方,您将不会再收到该错误。因印刷错误投票结束。 我尝试将它们移动到 list_to_remove = 部分的上方,结果显示 make_dictionary 现在没有定义 【参考方案1】:

dictionary = make_Dictionary(train_dir) 应该是dictionary = make_dictionary(train_dir)

python 区分大小写。 D 应该是 d

【讨论】:

以上是关于为啥python说字典没有定义?的主要内容,如果未能解决你的问题,请参考以下文章

为啥字典可以解包为元组?

为啥在 Python 2.7 中两级字典的值都指向同一个对象?

python 字典为啥这么快

我的python为啥dict没有isnull方法?

当我将一个字典分配给另一个变量时,为啥 Python 会同时更新两个字典? [复制]

为啥 Python 会引发 TypeError 而不是 SyntaxError?