Python ValueError:islice()的停止参数必须为None或整数:0<;=x<;=sys.maxsize关于主题连贯性
我正在学习这些教程并找出问题所在。因此,我对这段代码的目的是使它在主题、alpha和beta参数值范围内迭代。因此,我可以根据alpha和beta生成的一致性得分来确定最佳主题数量Python ValueError:islice()的停止参数必须为None或整数:0<;=x<;=sys.maxsize关于主题连贯性,python,python-3.x,long-integer,itertools,topic-modeling,Python,Python 3.x,Long Integer,Itertools,Topic Modeling,我正在学习这些教程并找出问题所在。因此,我对这段代码的目的是使它在主题、alpha和beta参数值范围内迭代。因此,我可以根据alpha和beta生成的一致性得分来确定最佳主题数量 def compute_coherence_values(corpus, dictionary, k, a, b): lda_model = gensim.models.LdaMulticore(corpus=corpus, id2wor
def compute_coherence_values(corpus, dictionary, k, a, b):
lda_model = gensim.models.LdaMulticore(corpus=corpus,
id2word=id2word,
num_topics=10,
random_state=100,
chunksize=100,
passes=10,
alpha=a,
eta=b,
per_word_topics=True)
coherence_model_lda = CoherenceModel(model=lda_model, texts=data_lemmatized, dictionary=id2word, coherence='c_v')
return coherence_model_lda.get_coherence()
然后
import numpy as np
import tqdm
grid = {}
grid['Validation_Set'] = {}
# Topics range
min_topics = 2
max_topics = 11
step_size = 1
topics_range = range(min_topics, max_topics, step_size)
# Alpha parameter
alpha = list(np.arange(0.01, 1, 0.3))
alpha.append('symmetric')
alpha.append('asymmetric')
# Beta parameter
beta = list(np.arange(0.01, 1, 0.3))
beta.append('symmetric')
# Validation sets
num_of_docs = len(corpus)
corpus_sets = [# gensim.utils.ClippedCorpus(corpus, num_of_docs*0.25),
# gensim.utils.ClippedCorpus(corpus, num_of_docs*0.5),
gensim.utils.ClippedCorpus(corpus, num_of_docs*0.75),
corpus]
corpus_title = ['75% Corpus', '100% Corpus']
model_results = {'Validation_Set': [],
'Topics': [],
'Alpha': [],
'Beta': [],
'Coherence': []
}
# Can take a long time to run
if 1 == 1:
pbar = tqdm.tqdm(total=540)
# iterate through validation corpuses
for i in range(len(corpus_sets)):
# iterate through number of topics
for k in topics_range:
# iterate through alpha values
for a in alpha:
# iterare through beta values
for b in beta:
# get the coherence score for the given parameters
cv = compute_coherence_values(corpus=corpus_sets[i], dictionary=id2word,
k=k, a=a, b=b)
# Save the model results
model_results['Validation_Set'].append(corpus_title[i])
model_results['Topics'].append(k)
model_results['Alpha'].append(a)
model_results['Beta'].append(b)
model_results['Coherence'].append(cv)
pbar.update(1)
pd.DataFrame(model_results).to_csv('lda_tuning_results.csv', index=False)
pbar.close()
出现此错误ValueError:islice()的Stop参数必须是None或整数:0我花了很长时间才弄明白这一点,但这是您需要更改的内容,在语料库集中有num_of_doc*.075,将其更改为int(num_of_docs*0.75),它将运行。这将需要很长时间,但它将消除错误im遵循本教程。请提供完整的回溯;您从未在此处调用islice,因此我们不知道从何处开始查找问题。理想情况下,尝试将其缩小到一个范围。我的深刻见解是错误消息是正确的。如果找不到发生异常的位置,请查找可以在异常时中断的调试器?