【问题标题】:"Not enough values to unpack" in sklearn.fitsklearn.fit 中的“没有足够的值来解包”
【发布时间】:2022-01-09 17:02:32
【问题描述】:

这是一段代码:

from sklearn.model_selection import StratifiedKFold
from sklearn.linear_model import LogisticRegressionCV

skf = StratifiedKFold(n_splits=5)
skf_1 = skf.split(titanic_dataset, surv_titanic)

ls_1 = np.logspace(-1.0, 2.0, num=500)

clf = LogisticRegressionCV(Cs=ls_1, cv = skf_1, scoring = "roc_auc", n_jobs=-1, random_state=17)

clf_model = clf.fit(x_train, y_train)

这说:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-130-b99a5912ff5a> in <module>
----> 1 clf_model = clf.fit(x_train, y_train)

H:\Anaconda_3\lib\site-packages\sklearn\linear_model\_logistic.py in fit(self, X, y, sample_weight)
   2098         #  (n_classes, n_folds, n_Cs . n_l1_ratios) or
   2099         #  (1, n_folds, n_Cs . n_l1_ratios)
-> 2100         coefs_paths, Cs, scores, n_iter_ = zip(*fold_coefs_)
   2101         self.Cs_ = Cs[0]
   2102         if multi_class == 'multinomial':

ValueError: not enough values to unpack (expected 4, got 0)

训练和测试数据集之前已经准备好,它们与其他分类器表现得很好。

这样的通用错误消息什么也没告诉我。这里有什么问题?

【问题讨论】:

  • cv = skf_1 更改为cv=5 并检查?
  • @sotmot 是的,错误消失了。这是什么意思?问题的本质是什么?

标签: python scikit-learn


【解决方案1】:

简而言之,问题是当您需要直接传递 StratifiedKFold(n_splits=5) 时,您将 skf.split(titanic_dataset, surv_titanic) 的结果传递给了 LogisticRegressionCV 上的 cv 参数。

下面我展示了重现您的错误的代码,下面我展示了两种替代方法,它们可以完成我认为您正在尝试做的事情。

# Some example data
data = load_breast_cancer()
X = data['data']
y = data['target']

# Set up the stratifiedKFold
skf = StratifiedKFold(n_splits=5)

# Don't do this... only here to reproduce the error
skf_indicies = skf.split(X, y)

# Some regularization
ls_1 = np.logspace(-1.0, 2.0, num=5)

# This creates your error
clf_error = LogisticRegressionCV(Cs=ls_1,
                                 cv = skf_indicies, 
                                 scoring = "roc_auc", 
                                 n_jobs=-1, 
                                 random_state=17)

# Error created by passing result of skf.split to cv
clf_model = clf_error.fit(X, y)

# This is probably what you meant to do
clf_using_skf = LogisticRegressionCV(Cs=ls_1,
                                     cv = skf, 
                                     scoring = "roc_auc", 
                                     n_jobs=-1,
                                     random_state=17, 
                                     max_iter=1_000)

# This will now fit without the error
clf_model_skf = clf_using_skf.fit(X, y)

# This is the easiest method, and from the docs also does the
# same thing as StratifiedKFold
# https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegressionCV.html
clf_easiest = LogisticRegressionCV(Cs=ls_1,
                                     cv = 5, 
                                     scoring = "roc_auc", 
                                     n_jobs=-1,
                                     random_state=17, 
                                     max_iter=1_000)

# This will now fit without the error
clf_model_easiest = clf_easiest.fit(X, y)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 2018-01-26
    • 2021-11-28
    • 2017-12-29
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多