Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
deep learning python
#1
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)
Larz60+ write Sep-26-2021, 02:02 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2

  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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,215 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  [split] Teacher (thrown in at the deep end - help) Mr90 2 3,022 May-23-2018, 02:04 PM
Last Post: DeaD_EyE
  Teacher (thrown in at the deep end - help) Mr90 5 3,902 May-22-2018, 01:08 PM
Last Post: DeaD_EyE
  Parse XML - how to handle deep levels/hierarchy dwill 8 9,454 Apr-17-2018, 04:17 PM
Last Post: dwill

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020