NumPy creation by fromfunction error

后端 未结 1 966
温柔的废话
温柔的废话 2021-01-24 03:23

Code:

n=3
x=np.fromfunction(lambda i,j: (i==1)and(j==1), (n,n), dtype=int)

leads to \"ValueError: The truth value of an array with more than on

相关标签:
1条回答
  • 2021-01-24 04:08

    The documentation is misleading. The function isn't called repeatedly with the indices of each individual cell; it's called once, with index arrays representing the indices of all the cells at once. The return value of this one function call is returned directly:

    >>> numpy.fromfunction(lambda *args: 1, (2, 2))
    1
    >>> numpy.fromfunction(lambda *args: args, (2, 2))
    (array([[ 0.,  0.],
           [ 1.,  1.]]), array([[ 0.,  1.],
           [ 0.,  1.]]))
    

    You'll need to change your function to operate that way:

    lambda i, j: (i==1) & (j==1)
    #                   ^ elementwise bitwise and
    
    0 讨论(0)
提交回复
热议问题