initialize a numpy array

前端 未结 12 1592
情歌与酒
情歌与酒 2020-11-28 02:55

Is there way to initialize a numpy array of a shape and add to it? I will explain what I need with a list example. If I want to create a list of objects generated in a loop,

相关标签:
12条回答
  • 2020-11-28 03:24

    For your first array example use,

    a = numpy.arange(5)
    

    To initialize big_array, use

    big_array = numpy.zeros((10,4))
    

    This assumes you want to initialize with zeros, which is pretty typical, but there are many other ways to initialize an array in numpy.

    Edit: If you don't know the size of big_array in advance, it's generally best to first build a Python list using append, and when you have everything collected in the list, convert this list to a numpy array using numpy.array(mylist). The reason for this is that lists are meant to grow very efficiently and quickly, whereas numpy.concatenate would be very inefficient since numpy arrays don't change size easily. But once everything is collected in a list, and you know the final array size, a numpy array can be efficiently constructed.

    0 讨论(0)
  • 2020-11-28 03:25

    Maybe something like this will fit your needs..

    import numpy as np
    
    N = 5
    res = []
    
    for i in range(N):
        res.append(np.cumsum(np.ones(shape=(2,4))))
    
    res = np.array(res).reshape((10, 4))
    print(res)
    

    Which produces the following output

    [[ 1.  2.  3.  4.]
     [ 5.  6.  7.  8.]
     [ 1.  2.  3.  4.]
     [ 5.  6.  7.  8.]
     [ 1.  2.  3.  4.]
     [ 5.  6.  7.  8.]
     [ 1.  2.  3.  4.]
     [ 5.  6.  7.  8.]
     [ 1.  2.  3.  4.]
     [ 5.  6.  7.  8.]]
    
    0 讨论(0)
  • 2020-11-28 03:28

    The way I usually do that is by creating a regular list, then append my stuff into it, and finally transform the list to a numpy array as follows :

    import numpy as np
    big_array = [] #  empty regular list
    for i in range(5):
        arr = i*np.ones((2,4)) # for instance
        big_array.append(arr)
    big_np_array = np.array(big_array)  # transformed to a numpy array
    

    of course your final object takes twice the space in the memory at the creation step, but appending on python list is very fast, and creation using np.array() also.

    0 讨论(0)
  • 2020-11-28 03:28

    Introduced in numpy 1.8:

    numpy.full

    Return a new array of given shape and type, filled with fill_value.

    Examples:

    >>> import numpy as np
    >>> np.full((2, 2), np.inf)
    array([[ inf,  inf],
           [ inf,  inf]])
    >>> np.full((2, 2), 10)
    array([[10, 10],
           [10, 10]])
    
    0 讨论(0)
  • 2020-11-28 03:28

    Array analogue for the python's

    a = []
    for i in range(5):
        a.append(i)
    

    is:

    import numpy as np
    
    a = np.empty((0))
    for i in range(5):
        a = np.append(a, i)
    
    0 讨论(0)
  • 2020-11-28 03:33

    Whenever you are in the following situation:

    a = []
    for i in range(5):
        a.append(i)
    

    and you want something similar in numpy, several previous answers have pointed out ways to do it, but as @katrielalex pointed out these methods are not efficient. The efficient way to do this is to build a long list and then reshape it the way you want after you have a long list. For example, let's say I am reading some lines from a file and each row has a list of numbers and I want to build a numpy array of shape (number of lines read, length of vector in each row). Here is how I would do it more efficiently:

    long_list = []
    counter = 0
    with open('filename', 'r') as f:
        for row in f:
            row_list = row.split()
            long_list.extend(row_list)
            counter++
    #  now we have a long list and we are ready to reshape
    result = np.array(long_list).reshape(counter, len(row_list)) #  desired numpy array
    
    0 讨论(0)
提交回复
热议问题