本篇文章為大家展示了numpy中怎么判斷數(shù)值類型,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司專注于中大型企業(yè)的網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè)和網(wǎng)站改版、網(wǎng)站營銷服務(wù),追求商業(yè)策劃與數(shù)據(jù)分析、創(chuàng)意藝術(shù)與技術(shù)開發(fā)的融合,累計客戶近千家,服務(wù)滿意度達(dá)97%。幫助廣大客戶順利對接上互聯(lián)網(wǎng)浪潮,準(zhǔn)確優(yōu)選出符合自己需要的互聯(lián)網(wǎng)運(yùn)用,我們將一直專注成都品牌網(wǎng)站建設(shè)和互聯(lián)網(wǎng)程序開發(fā),在前進(jìn)的路上,與客戶一起成長!numpy是無法直接判斷出由數(shù)值與字符混合組成的數(shù)組中的數(shù)值型數(shù)據(jù)的,因?yàn)橛蓴?shù)值類型和字符類型組成的numpy數(shù)組已經(jīng)不是數(shù)值類型的數(shù)組了,而是dtype='<U11'。
1、math.isnan也不行,它只能判斷float("nan"):
>>> import math >>> math.isnan(1) False >>> math.isnan('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: a float is required >>> math.isnan(float("nan")) True >>>
2、np.isnan不可用,因?yàn)閚p.isnan只能用于數(shù)值型與np.nan組成的numpy數(shù)組:
>>> import numpy as np >>> test1=np.array([1,2,'aa',3]) >>> np.isnan(test1) Traceback (most recent call last): File "<stdin>", line 1, in <module> 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 ''sa fe'' >>> test2=np.array([1,2,np.nan,3]) >>> np.isnan(test2) array([False, False, True, False], dtype=bool) >>>
解決辦法:
方法1:將numpy數(shù)組轉(zhuǎn)換為python的list,然后通過filter過濾出數(shù)值型的值,再轉(zhuǎn)為numpy, 但是,有一個嚴(yán)重的問題,無法保證原來的索引
>>> import numpy as np >>> test1=np.array([1,2,'aa',3]) >>> list1=list(test1) >>> def filter_fun(x): ... try: ... return isinstance(float(x),(float)) ... except: ... return False ... >>> list(filter(filter_fun,list1)) ['1', '2', '3'] >>> np.array(filter(filter_fun,list1)) array(<filter object at 0x0339CA30>, dtype=object) >>> np.array(list(filter(filter_fun,list1))) array(['1', '2', '3'], dtype='<U1') >>> np.array([float(x) for x in filter(filter_fun,list1)]) array([ 1., 2., 3.]) >>>
方法2:利用map制作bool數(shù)組,然后再過濾數(shù)據(jù)和索引:
>>> import numpy as np >>> test1=np.array([1,2,'aa',3]) >>> list1=list(test1) >>> def filter_fun(x): ... try: ... return isinstance(float(x),(float)) ... except: ... return False ... >>> import pandas as pd >>> test=pd.DataFrame(test1,index=[1,2,3,4]) >>> test 0 1 1 2 2 3 aa 4 3 >>> index=test.index >>> index Int64Index([1, 2, 3, 4], dtype='int64') >>> bool_index=map(filter_fun,list1) >>> bool_index=list(bool_index) #bool_index這樣的迭代結(jié)果只能list一次,一次再list時會是空,所以保存一下list的結(jié)果 >>> bool_index [True, True, False, True] >>> new_data=test1[np.array(bool_index)] >>> new_data array(['1', '2', '3'], dtype='<U11') >>> new_index=index[np.array(bool_index)] >>> new_index Int64Index([1, 2, 4], dtype='int64') >>> test2=pd.DataFrame(new_data,index=new_index) >>> test2 0 1 1 2 2 4 3 >>>
上述內(nèi)容就是numpy中怎么判斷數(shù)值類型,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享標(biāo)題:numpy中怎么判斷數(shù)值類型-創(chuàng)新互聯(lián)
本文來源:http://www.rwnh.cn/article40/djhgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、品牌網(wǎng)站設(shè)計、建站公司、電子商務(wù)、外貿(mào)建站、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容