Python Forum

Full Version: deep learning python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
WHat does x_train, y_train = zip(*train) mean? WHat does the "zip" and the asterick mean? How are they dividing X and Y ?

for x in range(40):
    
    y, sr = librosa.load('C:/audio files/folderCat/'  + str(x)+'.wav',   
    duration=2.95)  
    ps = librosa.feature.melspectrogram(y=y, sr=sr)
    
    if ps.shape != (128, 128): continue
    #print(ps.shape)
    D.append( (ps, 1) )

#print(D)

for x in range(40):
    
    y, sr = librosa.load('C:/audio files/folderDog/' +  str(x)+'.wav',  
    duration=2.95)  
    ps = librosa.feature.melspectrogram(y=y, sr=sr)
    if ps.shape != (128, 128): continue
    D.append( (ps, 2) )

print("Number of samples: ", len(D))

#print(D)
dataset = D
random.shuffle(dataset)

train = dataset[:20]
test = dataset[20:40]
x_train, y_train = zip(*train)
x_test, y_test = zip(*test)

  1. https://docs.python.org/3/tutorial/contr...ment-lists Wrote:Unpacking Argument Lists
    The reverse situation occurs when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments. For instance, the built-in range() function expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:
    >>> list(range(3, 6))            # normal call with separate arguments
    [3, 4, 5]
    >>> args = [3, 6]
    >>> list(range(*args))            # call with arguments unpacked from a list
    [3, 4, 5]
    In the same fashion, dictionaries can deliver keyword arguments with the **-operator:
    >>> def parrot(voltage, state='a stiff', action='voom'):
    ...     print("-- This parrot wouldn't", action, end=' ')
    ...     print("if you put", voltage, "volts through it.", end=' ')
    ...     print("E's", state, "!")
    ...
    >>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"}
    >>> parrot(**d)
    -- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised !
  2. https://docs.python.org/3/library/functions.html#zip Wrote:zip(*iterables)
    Make an iterator that aggregates elements from each of the iterables.

    Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. Equivalent to:
    def zip(*iterables):
        # zip('ABCD', 'xy') --> Ax By
        sentinel = object()
        iterators = [iter(it) for it in iterables]
        while iterators:
            result = []
            for it in iterators:
                elem = next(it, sentinel)
                if elem is sentinel:
                    return
                result.append(elem)
            yield tuple(result)
    The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n). This repeats the same iterator n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.

    zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use itertools.zip_longest() instead.

    zip() in conjunction with the * operator can be used to unzip a list:
    >>> x = [1, 2, 3]
    >>> y = [4, 5, 6]
    >>> zipped = zip(x, y)
    >>> list(zipped)
    [(1, 4), (2, 5), (3, 6)]
    >>> x2, y2 = zip(*zip(x, y))
    >>> x == list(x2) and y == list(y2)
    True