1. 程式人生 > >TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely錯誤解決方法

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely錯誤解決方法

將一個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陣列,解決方法就是將列數改為一致即可。