initialize a numpy array

前端 未结 12 1696
情歌与酒
情歌与酒 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条回答
  •  Happy的楠姐
    2020-11-28 03:46

    To initialize a numpy array with a specific matrix:

    import numpy as np
    
    mat = np.array([[1, 1, 0, 0, 0],
                    [0, 1, 0, 0, 1],
                    [1, 0, 0, 1, 1],
                    [0, 0, 0, 0, 0],
                    [1, 0, 1, 0, 1]])
    
    print mat.shape
    print mat
    

    output:

    (5, 5)
    [[1 1 0 0 0]
     [0 1 0 0 1]
     [1 0 0 1 1]
     [0 0 0 0 0]
     [1 0 1 0 1]]
    

提交回复
热议问题