【问题标题】:Python3: UnboundLocalError: local variable 'labels' referenced before assignmentPython3:UnboundLocalError:分配前引用的局部变量“标签”
【发布时间】: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


【解决方案1】:

只有在函数label_predictionnum_clicks 为真时才创建局部变量labels,但无论如何都返回此局部变量。因此,如果您调用函数 label_prediction,而 num_clicksFalse,您将返回一个从未创建的变量,因此会出现错误消息。

编辑:由于您不想在num_clicks 为false 时返回任何内容,因此您可以将if 的最后一行修改为return f"Predicted labels:{get_tags(prediction[0],threshold_value,label_value)}",并删除最后的return你的功能。

另一种解决方案是在函数开头使用None 初始化labels

解决方案 1

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)
            return f"Predicted labels:{get_tags(prediction[0],threshold_value,label_value)}"

解决方案 2

def label_prediction(num_clicks, text, threshold_value, preprocess_func, label_value):
    labels = None
    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

【讨论】:

  • 那么应该怎么做呢?
  • num_clicks为假时,你想返回什么?
  • 在这种情况下,您只需使用 return f"Predicted labels:{get_tags(prediction[0],threshold_value,label_value)}" 修改 if 的最后一行,并删除函数末尾的返回。
  • 另一种解决方案是在函数开头使用None 初始化labels
  • 现在实际点击没有返回任何第一个解决方案。
猜你喜欢
  • 2015-12-06
  • 1970-01-01
  • 2017-08-10
  • 2020-01-16
  • 2019-12-05
  • 2017-09-19
  • 2020-09-26
  • 2022-01-02
  • 2019-03-22
相关资源
最近更新 更多