gpt4 book ai didi

python - 解包值过多时出现错误(预期2)

转载 作者:行者123 更新时间:2023-12-04 21:19:08 27 4
gpt4 key购买 nike

我想从我的函数返回两个不同的值如何重写我的代码以在两个不同的变量中返回这两个值。

所以我得到这个错误:


Traceback (most recent call last):
File "features.py", line 300, in <module>
fea1, fea2 = build_feature_matrix_S(sentences)
ValueError: too many values to unpack (expected 2)


这里的代码
def build_feature_matrix_S(documents):

feature_type = ['frequency','tfdif']
#feature_type = feature_type.lower().strip()

for f in feature_type:

if feature_type == 'frequency':
vectorizer_freq = CountVectorizer(tokenizer = tokenize_lemmatize_spacy, binary=False, min_df=5, ngram_range=(1, 2))
feature_matrix_freq = vectorizer_freq.fit_transform(documents)
feature_freq = pd.DataFrame(feature_matrix_freq.todense(), columns = vectorizer_freq.get_feature_names())

return feature_freq

else:

vectorizer_tfidf = TfidfVectorizer(tokenizer = tokenize_lemmatize_spacy, min_df=5, ngram_range=(1, 2))
feature_matrix_tfidf = vectorizer_tfidf.fit_transform(documents)
feature_tfidf = pd.DataFrame(feature_matrix_tfidf.todense(), columns = vectorizer_tfidf.get_feature_names())

return feature_tfidf

fea1, fea2 = build_feature_matrix_S(sentences)

print(fea1, fea2)

最佳答案

在这一行中,

fea1, fea2 = build_feature_matrix_S(sentences)

python期望返回2个变量,一个元组,并且函数build_feature_matrix_S()中的两个return语句都没有这样做,返回超过2个。
    feature_freq = pd.DataFrame(feature_matrix_freq.todense(), columns = vectorizer_freq.get_feature_names())

return feature_freq

或者
            feature_tfidf = pd.DataFrame(feature_matrix_tfidf.todense(), columns = vectorizer_tfidf.get_feature_names())

return feature_tfidf

因此错误。

关于python - 解包值过多时出现错误(预期2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60216073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com