Parameters to numpy's fromfunction

前端 未结 6 1794
终归单人心
终归单人心 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 08:01

    The documentation is very misleading in that respect. It's just as you note: instead of performing f(0,0), f(0,1), f(1,0), f(1,1), numpy performs

    f([[0., 0.], [0., 1.]], [[1., 0.], [1., 1.]])
    

    Using ndarrays rather than the promised integer coordinates is quite frustrating when you try and use something likelambda i: l[i], where l is another array or list (though really, there are probably better ways to do this in numpy).

    The numpy vectorize function fixes this. Where you have

    m = fromfunction(f, shape)
    

    Try using

    g = vectorize(f)
    m = fromfunction(g, shape)
    

提交回复
热议问题