亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Numpy:檢查整數 NaN

Numpy:檢查整數 NaN

慕碼人2483693 2023-01-04 10:10:18
我需要一個包含不同數據類型(浮點數或整數)列的表。我使用 dtype 來定義它們:import numpy as np# define arraydatadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<i4') ]arr = np.full((4,), np.nan, dtype=datadef) # fill array with dataarr['i'] = np.array([1, 2, 3, 4])arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])# nothing for 'j'print arr輸出 :[(1,  1.33333333,  2.77777778, -2147483648) (2,         nan,  5.4       , -2147483648) (3,  2.66666667,  3.4       , -2147483648) (4,  5.        ,         nan, -2147483648)]最后一列的NaN值已轉換為-2147483648,到目前為止沒有問題。但是現在我無法檢查我的數組中的值是否確實是 NaN :row = arr[1]print np.isnan(row) # 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''在單個單元格上,信息似乎NaN丟失了,-2147483648被認為是“經典數字”:print row # (2,  nan,  5.4, -2147483648)print np.isnan(row[0]) # False, OKprint np.isnan(row[1]) # True, OKprint np.isnan(row[3]) # False, expected True在這種情況下是否有一種簡單的方法來檢查NaN整數?
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

np.nan是浮點數而不是整數。您要么必須更改最后一列的數據類型,要么使用不同的結構將 nan 存儲為整數。


datadef=[ ('i', '<i4'), ('f', '<f8'), ('g', '<f8'), ('j', '<f4') ]

arr = np.full((4,), np.nan, dtype=datadef) 


# fill array with data

arr['i'] = np.array([1, 2, 3, 4])

arr['f'] = np.array([1.3333333333, np.nan, 2.6666666666666666, 5.0])

arr['g'] = np.array([2.77777777777, 5.4, 3.4, np.nan])

# nothing for 'j'

現在嘗試打印 np.isnan 語句:


print(np.isnan(arr[1][3]))


True


查看完整回答
反對 回復 2023-01-04
  • 1 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號