ValueError: too many values to unpack(expected 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)

输出:

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

使用 Zip

如果你想以一种方式对两个可迭代对象进行分组,在每次迭代中 i-th 元组包含来自每个项目的 i-th 元素 你应该使用 zip built-in python 函数(还有其他函数使用 itertools 包中的其他标准对可迭代对象进行分组)。

示例:

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

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

输出:

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