繁体   English   中英

ValueError:要解压的值太多(预期为 2)-train_test_split

[英]ValueError: too many values to unpack(expected 2) - train_test_split

我在特征提取之前正在做 test_split。 但是,当我尝试遍历任何集合时,无论是训练还是测试,都会出现以下错误(ValueError:要解压的值太多(预期 2))

    for cls in os.listdir(path):
        for sound in tqdm(os.listdir(os.path.join(path, cls))):
         wav  = librosa.load(os.path.join(os.path.join(path, cls, sound)), sr=16000)[0].astype(np.float32)
         tmp_samples.append(wav)
         tmp_labels.append(cls)
         
    
    print(tmp_samples)
   
   X_train, y_train , X_test ,  y_test  = train_test_split( tmp_samples, tmp_labels , test_size=0.60,shuffle=True) 
              
   for x,y in X_test,y_test:
       extract_features(x, y, model, plain_samples , plain_labels )

这是 X_train 中的内容:

[array([ 0.09814453,  0.04959106,  0.04248047, ..., -0.08251953,
       -0.07385254, -0.03076172], dtype=float32), array([ 0.06820679,  0.03372192,  0.00292969, ..., -0.19833374,
       -0.18746948, -0.18157959], dtype=float32), array([-0.04940796, -0.04251099, -0.02798462, ..., -0.04455566,
       -0.03005981, -0.01742554], dtype=float32), ....etc

您应该使用zip对 python 迭代器中的变量进行分组:

for x,y in zip(X_test,y_test):
   extract_features(x, y, model, plain_samples , plain_labels )

解释

当您将多个值传递到 for 语句的右侧时,python 会将其解释为一个元组,因此它会期望解包一个变量。 例子:

poo = ("one", "two", "three", "four")
foo = [1, 2, 3, 4]

for x in poo, foo:  # Equals to for x in (poo, foo):
    print(x)

Output:

('one', 'two', 'three', 'four')
[1, 2, 3, 4]

使用 Zip

如果你想以这样的方式对两个可迭代对象进行分组,即在每次迭代中第 i 个元组包含每个项目中的第 i 个元素,你应该使用内置的 zip python function(还有其他函数可以对可迭代对象进行分组使用 itertools 包中的其他标准)。

例子:

poo = ("one", "two", "three", "four")
foo = [1, 2, 3, 4]

for x, y in zip(poo, foo):
    print(f"{x}: {y}")

Output:

one: 1
two: 2
three: 3
four: 4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM