Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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
Python3:UnboundLocalError:localvariable';标签';分配前参考_Python_Python 3.x_Plotly Dash - Fatal编程技术网

Python3:UnboundLocalError:localvariable';标签';分配前参考

Python3:UnboundLocalError:localvariable';标签';分配前参考,python,python-3.x,plotly-dash,Python,Python 3.x,Plotly Dash,以下代码给出了错误UnboundLocalError:赋值前引用的局部变量“labels”。我在其他stackoverflow问题中查看了类似的问题,但他们说要处理全局变量,但我没有发现我的问题有任何全局变量标签引用 def get_tags(predicted_list, threshold, labels): mlb = [(i1, c1) for i1, c1 in enumerate(multilabel.classes_)] temp_list = sorted(

以下代码给出了错误
UnboundLocalError:赋值前引用的局部变量“labels”
。我在其他stackoverflow问题中查看了类似的问题,但他们说要处理全局变量,但我没有发现我的问题有任何全局变量
标签
引用

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

如何解决此问题?

如果函数
label\u prediction
中的
num\u clicks
为true,则仅创建局部变量
labels
,但在任何情况下都返回此局部变量。因此,如果使用
num\u clicks
being
False
调用函数
label\u prediction
,则返回一个从未创建过的变量,从而返回错误消息

Edit:由于当
num\u clicks
为false时,您不想返回任何内容,因此只需使用
return f“Predicted labels:{get\u tags(prediction[0],threshold\u value,label\u value)}”
修改if的最后一行,并在函数结束时删除返回


另一种解决方案是在函数开始时使用
None
初始化
标签

解决方案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

你问的是哪一个错误:标题中的错误还是文本中的错误?我认为两者都是相同的。在编辑问题之前它们不一样。请发布完整(或最后10行)的回溯。看起来
num\u clicks
没有验证(到True)因此,在返回之前,
labels
没有被设置。那么应该怎么做?当
num\u clicks
为false时,您希望返回什么?这不是我留下它的原因。另一个解决方案是在函数的开头使用
None
初始化
labels
。请参阅我编辑的答案。