IndentationError: expected an indented block when trying to reproduce LDA for a document

IndentationError: expected an indented block when trying to reproduce LDA for a document

我试图在我的 collection 的第一篇文章中获取 LDA 分布,但我 运行 遇到了几个错误:

我的collection:doc_set,是一个pandas.core.series.Series。每当我想 运行 简单代码时:

print(ldamodel[doc_set[1]])

我 运行 出现以下错误:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). 我想我通过以下方式解决了它:

if doc_set is not None:
print(ldamodel[doc_set[1]])

然而,现在我收到以下错误:IndentationError: expected an indented block。我正在寻找错误的直觉而不是更正,我不能把我的整个 LDA 用于复制,因为它太大了。提前致谢!

您的缩进不正确,因为您没有将 print 语句放在 if 块中。如果以冒号 (:) 结束行,则必须增加缩进级别,否则会出现 IndentationError 异常。
这将是正确的代码:

if doc_set is not None:
    print(ldamodel[doc_set[1]])

缩进在 python 中非常奇特。您必须通过为每个块使用 white-spaces 或制表符来维护层次结构。每个块只能有制表符或(任意数量的)白色 spaces.

for item in list:
    print item

if flag:
  raise SystemExit

在第一个代码块中,我使用了四个白色 space,在第二个代码块中,我使用了两个。

评论也是如此。注释必须相应地缩进。

print 'Starting module'
if not configs:
    '''
    sys.exit('Error in Configuration files.')
    '''
    pass

在这种情况下,该行应该被注释,python 不会抱怨。否则它会抛出关于缩进的错误。