Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在函数:TypeError:类型为';的对象中返回None;非类型';没有len()_Python_Lda_Nonetype - Fatal编程技术网

Python 在函数:TypeError:类型为';的对象中返回None;非类型';没有len()

Python 在函数:TypeError:类型为';的对象中返回None;非类型';没有len(),python,lda,nonetype,Python,Lda,Nonetype,我正在尝试打印LDA中每个主题的主题和文本。但是,打印主题后的“无”正在破坏我的脚本。我可以打印我的主题,但不能打印文本 import pandas import numpy as np from sklearn.feature_extraction.text import CountVectorizer from sklearn.decomposition import LatentDirichletAllocation n_top_words = 5 n_components = 5 d

我正在尝试打印
LDA
中每个主题的主题和文本。但是,打印主题后的“无”正在破坏我的脚本。我可以打印我的主题,但不能打印文本

import pandas
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation

n_top_words = 5
n_components = 5

def print_top_words(model, feature_names, n_top_words):
    for topic_idx, topic in enumerate(model.components_):
        message = "Topic #%d: " % topic_idx
        message += " ".join([feature_names[i] for i in topic.argsort()[:-n_top_words - 1:-1]])

        return message

text = pandas.read_csv('text.csv', encoding = 'utf-8')
text_list = text.values.tolist()

tf_vectorizer = CountVectorizer()
tf = tf_vectorizer.fit_transform(text_list)

lda = LatentDirichletAllocation(n_components=n_components, learning_method='batch', max_iter=25, random_state=0)

doc_distr = lda.fit_transform(tf)

tf_feature_names = tf_vectorizer.get_feature_names()
print (print_top_words(lda, tf_feature_names, n_top_words))

doc_distr = lda.fit_transform(tf)
topics = print_top_words(lda, tf_feature_names, n_top_words)
for i in range(len(topics)):
    print ("Topic {}:".format(i))
    docs = np.argsort(doc_distr[:, i])[::-1]
    for j in docs[:10]:
       print (" ".join(text_list[j].split(",")[:2]))
我的输出:

Topic 0: no order mail received back 

Topic 1: cancel order wishes possible wish 

Topic 2: keep current informed delivery order 

Topic 3: faulty wooden box present side 

Topic 4: delivered received be produced urgent 

Topic 5: good waiting day response share 
然后是此错误:

  File "lda.py", line 41, in <module>

    for i in range(len(topics)):

TypeError: object of type 'NoneType' has no len()
文件“lda.py”,第41行,在
对于范围内的i(len(主题)):
TypeError:类型为“NoneType”的对象没有len()

您没有提供完整的代码,但最可能的原因是变量
主题
没有。唯一可能发生的方法是,如果
print\u top\u words
函数中的
model.components\u
是一个空集合,那么循环永远不会运行,函数(隐式)也不会返回任何结果。检查集合的值。更好的是,选择在这种情况下要返回的值


另一个不相关的要点:在每次迭代时初始化
消息
变量,并在每次迭代时返回它。检查你的意思。

如果不了解
最新DirichletAllocation的内部工作原理,这有点难以回答。然而,它的组成部分与它有关,因为它的重复迭代会产生不同的结果

您最有可能通过更改以下内容来避免此错误:

print (print_top_words(lda, tf_feature_names, n_top_words))

doc_distr = lda.fit_transform(tf)
topics = print_top_words(lda, tf_feature_names, n_top_words)
致:

第二次调用函数时,model.components uu不返回任何内容,因此跳过循环,函数不返回任何内容

然而,我不确定这是否是代码的实际意图。看起来你可能想让print\u top\u Word成为一个生成器?您将返回for循环的内部,这使得它永远不会到达第二次迭代。这可能不是循环的目的。

您的
print\u top\u words()
函数(至少)有四个问题

导致当前问题的第一个问题是,如果
model.components\uu
为空,则for循环体将不会执行,然后您的函数将(隐式)返回
None

第二个更为微妙:如果
model.components\uu
不是空的,函数将只返回第一条消息,然后返回它并退出-这正是
return
语句的定义:返回一个值(或者
None
如果没有指定值)并退出函数

第三个问题是(当
model.components\uuu
不为空时),函数返回一个字符串,调用代码显然需要一个列表。这是一个微妙的错误,因为字符串有一个长度,所以
范围(len(topics))
上的for循环似乎可以运行,但是
len(topics)
肯定不是您期望的值

最后,该函数的名称非常糟糕,因为它没有“打印”任何内容-与前三个问题相比,这似乎微不足道,并且它不会阻止代码真正工作(假设前三个问题已修复),但是,对代码进行推理本身就很困难,因此正确命名非常重要,因为它大大减少了认知负载,并使维护/调试更容易

长话短说:想想你真正想要这个函数做什么,并适当地修复它。我不会在这里发布一个“更正”的版本,因为我不知道你想做什么,但是上面的注释应该会有所帮助


注意:另外,您使用完全相同的参数调用了
doc\u distr=lda.fit\u transform(tf)
print\u top\u words(lda,tf\u feature\u names,n\u top\u words)
两次,这要么完全无用,要么纯粹浪费处理器周期(在最佳情况下)或者,如果第二次调用的结果不同,就会闻到另一个bug的味道。

对于该迭代,topic的值为None。似乎您的
print\u top\u words
在返回
消息时有问题。检查函数中的
循环。
temp = print_top_words(lda, tf_feature_names, n_top_words)
print (temp)

doc_distr = lda.fit_transform(tf)
topics = print_top_words(temp)