Parameters to numpy's fromfunction

前端 未结 6 1801
终归单人心
终归单人心 2020-11-30 07:37

I haven\'t grokked the key concepts in numpy yet.

I would like to create a 3-dimensional array and populate each cell with the result of a function call

6条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 07:58

    I think it is a little confusing that most examples of fromfunction use square arrays.

    Perhaps looking at a non-square array could be helpful?

    def f(x,y):
        print(f'x=\n{x}')
        print(f'y=\n{y}')
        return x+y
    
    z = np.fromfunction(f,(4,3))
    print(f'z=\n{z}')
    

    Results in:

    x=
    [[0 0 0]
     [1 1 1]
     [2 2 2]
     [3 3 3]]
    y=
    [[0 1 2]
     [0 1 2]
     [0 1 2]
     [0 1 2]]
    z=
    [[0 1 2]
     [1 2 3]
     [2 3 4]
     [3 4 5]]
    

提交回复
热议问题