出现太多值而无法解包的错误(expected2)

时间:2020-02-13 20:36:18

标签: python excel pandas scikit-learn

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

所以我得到这个错误:


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)

2 个答案:

答案 0 :(得分:3)

在这一行

fea1, fea2 = build_feature_matrix_S(sentences)

python期望返回2个变量,一个元组,而在build_feature_matrix_S()函数中的两个return语句都不会这样做,返回两个以上。

    feature_freq = pd.DataFrame(feature_matrix_freq.todense(), columns = vectorizer_freq.get_feature_names())

    return feature_freq

OR

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

            return feature_tfidf

,因此是错误。

答案 1 :(得分:2)

在if,else语句中,您仅返回1个变量。您可以从类似的函数中返回两个变量:

return var1, var2

因此您的函数可以编写为以下形式:

def build_feature_matrix_S(documents):

    # part 1:
    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())

    # part 2:
    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_freq, feature_tfidf