numpy中怎么判断数值类型-创新互联

本篇文章为大家展示了numpy中怎么判断数值类型,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

成都创新互联公司专注于中大型企业的网站设计制作、网站建设和网站改版、网站营销服务,追求商业策划与数据分析、创意艺术与技术开发的融合,累计客户近千家,服务满意度达97%。帮助广大客户顺利对接上互联网浪潮,准确优选出符合自己需要的互联网运用,我们将一直专注成都品牌网站建设和互联网程序开发,在前进的路上,与客户一起成长!

numpy是无法直接判断出由数值与字符混合组成的数组中的数值型数据的,因为由数值类型和字符类型组成的numpy数组已经不是数值类型的数组了,而是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不可用,因为np.isnan只能用于数值型与np.nan组成的numpy数组:

>>> 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数组转换为python的list,然后通过filter过滤出数值型的值,再转为numpy, 但是,有一个严重的问题,无法保证原来的索引

>>> 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数组,然后再过滤数据和索引:

>>> 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这样的迭代结果只能list一次,一次再list时会是空,所以保存一下list的结果
>>> 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
>>>

上述内容就是numpy中怎么判断数值类型,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联成都网站设计公司行业资讯频道。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

分享标题:numpy中怎么判断数值类型-创新互联
本文来源:https://www.cdcxhl.com/article40/djhgho.html

成都网站建设公司_创新互联,为您提供软件开发品牌网站设计建站公司电子商务外贸建站品牌网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

h5响应式网站建设