【发布时间】:2021-01-08 17:59:48
【问题描述】:
以下代码给出了错误UnboundLocalError: local variable 'labels' referenced before assignment。我在其他 stackoverflow 问题中查看了类似的问题,但他们说关于处理全局变量,但我发现我的问题没有任何全局变量 labels 指代。
def get_tags(predicted_list, threshold, labels):
mlb = [(i1, c1) for i1, c1 in enumerate(multilabel.classes_)]
temp_list = sorted(
[(i, c) for i, c in enumerate(list(predicted_list))],
key=lambda x: x[1],
reverse=True,
)
tag_list = [item1 for item1 in temp_list if item1[1] >= threshold]
tags = [
item[1] for item2 in tag_list[:labels] for item in mlb if item2[0] == item[0]
]
return tags
def label_prediction(num_clicks, text, threshold_value, preprocess_func, label_value):
if text is None:
raise PreventUpdate
else:
if num_clicks:
params = ["remove_digits", "remove_stopwords", "text_lemmatization"]
dict_params = {param: True for param in params}
preprocess_text = preprocess(text, **dict_params)
transformed_text = tfidf.fit_transform([preprocess_text])
prediction = classifier.predict_proba(transformed_text)
labels= f"Predicted labels:{get_tags(prediction[0],threshold_value,label_value)}"
return labels
如何解决这个问题?
【问题讨论】:
-
你问的是哪个错误:标题中的错误还是正文中的错误?
-
我认为两者都是一样的。
-
在您编辑问题之前它们并不相同。
-
请发布完整的(或最后 10 行)回溯。
-
看起来
num_clicks没有验证(为真),因此labels在返回之前没有设置。
标签: python python-3.x plotly-dash