Ran
Ran

Reputation: 57

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

I'm doing test_split before the feature extraction. however, when I try to loop through any set, whether train or test, I get the following error (ValueError: too many values to unpack(expected 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 )

This is what is inside the 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

Upvotes: 2

Views: 2809

Answers (1)

Fran Arenas
Fran Arenas

Reputation: 648

You should use zip to group the variables in a python iterator:

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

Explanation

When you pass multiple values to the right side of a for statement python interprets it as a tuple so it will expect one variable to unpack. Example:

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]

Using Zip

If you want to group two iterables in a way that in each iteration the i-th tuple contains the i-th element from each of the items you should use the zip built-in python function (there are also other functions that group the iterables using other criteria in the itertools package).

Example:

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

Upvotes: 2

Related Questions