TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely错误解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/missyougoon/article/details/84989210

将一个list矩阵转化为numpy数组之后,使用np.isnan()方法,报出了这么一个错误:

TypeError: ufunc ‘isnan’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ‘‘safe’’

经过测试,发现是二维list转化为numpy数组的时候就存在问题了。
出错原因是list第二维的元素个数不一致导致如:
list1 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9, 10]]
这时候使用 np.array(list1),输出如下内容:
[
list(1, 2, 3),
list(4, 5, 6),
list(7, 8, 9, 10)
]
接着使用np.isnan()方法,就会报出最上面的错误。
因为第三行是4个元素,所以无法转化为我们想要的numpy数组,解决方法就是将列数改为一致即可。

猜你喜欢

转载自blog.csdn.net/missyougoon/article/details/84989210