【问题标题】:Python3: ValueError: too many values to unpack (expected 2)Python3:ValueError:解包的值太多(预期为 2)
【发布时间】:2017-07-15 12:49:54
【问题描述】:

现在我正在从 txt 文件中读取大量(大约 100 万行)训练数据(标签:0 或 1)来练习使用 scikit-learn 进行机器学习:数据如下:

label       data
0      xd,xw,gh
1      xg,xh,xl,xk,yh,xd

.......................

但运行代码后出现错误:

 Traceback (most recent call last):
  X, y = get_minibatch(train_text, train_label, chunksize=1000)
 ValueError: too many values to unpack (expected 2

请给我关于这个问题的建议! 感谢您的时间和考虑!

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn


    【解决方案1】:

    get_minibatch(train_x, train_y, chunksize) 正在使用 yield 将其呈现为 generator/iterator。

    你调用它就像一个函数,期待两个参数并将它们解包。

    但是你没有得到两个元素,你在调用之后获得了一个生成器/迭代器对象。 (在这些情况下:总是打印出print(type(function()))。当然这个单一的对象不能被解包成两个。

    您需要像生成器/迭代器一样使用它,例如在python3中:

    X, y = next(get_minibatch(train_text, train_label, chunksize=1000))
    

    【讨论】:

    • 感谢@sascha,在我添加下一个(get_minibach.....)之后,代码仍然有错误:raise ValueError("classes should include all valid labels that can" ValueError: classes should包括所有可以在 y 中的有效标签
    • 这是一些新的无关问题。基本问题是,存在未知标签,因为您的装配工不知道它们。阅读this。摘录:For classification, a somewhat important thing to note is that although a stateless feature extraction routine may be able to cope with new/unseen attributes, the incremental learner itself may be unable to cope with new/unseen targets classes. In this case you have to pass all the possible classes to the first partial_fit call using the classes= parameter.
    • 感谢@sascha 您的宝贵建议。对不起,我仍然不知道如何处理它。
    • 为此提出一个新问题。这个回答了(虽然不被接受)。在一些 cmets 中就一个不相关的问题讨论这个其他问题是不合适的。 (简短的解决方案:只需构建一个 set(),遍历所有 y,收集所有目标,转换为数组并使用 classes= param 将其传递给 partial_fit)
    • 感谢@sascha,我将开始一个新问题,我会接受这个。
    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2014-07-31
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多