NumPy ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

后端 未结 3 679
情歌与酒
情歌与酒 2020-12-05 15:27

I was calculating eigenvectors and eigenvalues of a matrix in NumPy and just wanted to check the results via assert(). This would throw a ValueError that I don\

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 16:10

    As it says, it is ambiguous. Your array comparison returns a boolean array. Methods any() and all() reduce values over the array (either logical_or or logical_and). Moreover, you probably don't want to check for equality. You should replace your condition with:

    np.allclose(A.dot(eig_vec[:,col]), eig_val[col] * eig_vec[:,col])
    

提交回复
热议问题