用NAN替换强制转换非数字numpy数组

用NAN替换强制转换非数字numpy数组,第1张

用NAN替换强制转换非数字numpy数组

您可以使用以下方式将字符串数组转换为浮点数组(带有NaN)

np.genfromtxt

In [83]: np.set_printoptions(precision=3, suppress=True)In [84]: np.genfromtxt(np.array(['1','2','3.14','1e-3','b','nan','inf','-inf']))Out[84]: array([ 1.   ,  2.   ,  3.14 ,  0.001,    nan,    nan,    inf,   -inf])

这是一种识别“数字”字符串的方法:

In [34]: xOut[34]: array(['1', '2', 'a'],       dtype='|S1')In [35]: x.astype('unipre')Out[35]: array([u'1', u'2', u'a'],       dtype='<U1')In [36]: np.char.isnumeric(x.astype('unipre'))Out[36]: array([ True,  True, False], dtype=bool)

请注意,“数字”是指仅包含数字字符的Unipre,即具有Unipre数值属性的字符。它 包括小数点。因此

u'1.3'
不被视为“数字”。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5630383.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存