Python 为什么不是';t np.nan_to_num()转换这个(nx m)数组?
我想将我的numpy数组Python 为什么不是';t np.nan_to_num()转换这个(nx m)数组?,python,arrays,numpy,type-conversion,nan,Python,Arrays,Numpy,Type Conversion,Nan,我想将我的numpy数组a中的所有nan项设置为零 无论我如何使用np.nan\u to_num(),数组都不会被处理(它仍然在数组中保留np.nan) 以下是我失败的尝试: np.nan_to_num(a) np.nan_to_num(a,copy=True) np.nan_to_num(a,copy=False) a=np.nan_to_num(a) a=np.nan_to_num(a,copy=False) a=np.nan_to_num(a,copy=True) 正如nan\u to\
a
中的所有nan
项设置为零
无论我如何使用np.nan\u to_num()
,数组都不会被处理(它仍然在数组中保留np.nan
)
以下是我失败的尝试:
np.nan_to_num(a)
np.nan_to_num(a,copy=True)
np.nan_to_num(a,copy=False)
a=np.nan_to_num(a)
a=np.nan_to_num(a,copy=False)
a=np.nan_to_num(a,copy=True)
正如
nan\u to\u num
docstring所述:
如果x
不精确,则不进行替换
而dtype对象不算作不精确
如果出于某种原因需要使用dtype对象(例如,可能希望使用nan
s和exactint
s),那么这里有一个解决方法:
a[a!=a] = 0
请注意,理论上除了nan
之外,还有其他对象是x=x
的计算结果为True(当然,可以创建自己的类并摆弄\uuuuueq\uuuuu
,\uuuuneq\uuuuu
),但实际上我想不出任何
这只是一个精心设计的例子:
>>> import numpy as np
>>> import math
>>>
>>> a = np.random.randint(0, 1000, (6,)).astype(object)
>>> a[a%2==0] = np.nan
>>>
>>> fact_exact = np.vectorize(math.factorial, 'O', 'O')
>>>
>>> fact_exact(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/paul/.local/lib/python3.6/site-packages/numpy/lib/function_base.py", line 1972, in __call__
return self._vectorize_call(func=func, args=vargs)
File "/home/paul/.local/lib/python3.6/site-packages/numpy/lib/function_base.py", line 2048, in _vectorize_call
outputs = ufunc(*inputs)
ValueError: factorial() only accepts integral values
>>>
>>> a[a!=a] = 0
>>> fact_exact(a)
array([9819935662418089743352075922310862095706065486822583658822975979153852871637910339598847876493575760863201233608970580391009961465728060140206398380369810186460532083760537973722230477712617437079362600099095591538946730193485520929914465963675497331037894791629662134417383906616748712477435411911352595846133057242505006764835196420336585309344206359125847804414531691517822911373600118902137858177047463867389635205323328678714656377591230065986360526515442653777496908763065282294664208227077490200850296013058820462199153017425546879776071769432946284989651969735166129654123362278827485074178681546981559466233191972688158356430976918192398846419304865350500808417927115875428971873067092978672051108353026958311731456630717915806992149025378731927814021805881859364498816522297657223802150320368577537638698692463078070519911729996949263069045872688620575874758242248117345983373644762881336075203583068807371386560008413979828440302163961903567206206098114957943899603695885783671168564745354608640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
22328783881661914958481873975346502495151470121092663127656427617172486869336444341196216861471796204456103981797935323465763492125980526669772652700063306391000092324747490987759008282321662774044560021923711172537165034028116470777032463317525690139861312277154265627409161865934581816407380706408159413469087649804140238680046340298380454769197056000000000000000000000000000000000000000000000000000,
1,
61249584099358401539774988285121649211647782880181065019552657036267338153088195303988201779967275642784589505913349592976251572958797164520286603082616258499126414850388770750032832244874744865500684599339365169094265281656246018624169125087086336929008659140773790287427038315506740711640971717627407262119806133914039569804387544893605360482632749642132398074143010093832414811273406748220437584361624445361171146706501836044960640727879585735220969146850637281930634576684379022439144569827759897323120413808197447743317836963898751450642251281351982277623696403714801809091137618510094637754741546381374172490209156669750628265287758243565040756752491082629092890931663069084118249960190350279925210044221389170848672643624902424798289485981643559009642358060100976306359010066013465973059932028926310180595315985960099791957394179039519432507444190747654625992620055591848528852607925564873303749001475451862943569149219508203963665660697011849205174996326078837279628237406181221912723812127044670946612175065696608648876366755523800502033220426264259724448110042998615347327090687044945724644868095726898042638404229137017574884525227292991943592508583104116919096883640157188742952660337139750108570879849335960456768856494175006057451288109527150100807278246132549650716938934121106772599000305859091685578549764454500874996178837114679306052077693402114826710945748516120895211107804543955416170463298478450007640457295281818515443548160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
1, 1], dtype=object)
>>将numpy作为np导入
>>>输入数学
>>>
>>>a=np.random.randint(0,1000,(6,).aType(对象)
>>>a[a%2==0]=np.nan
>>>
>>>事实精确=np.矢量化(数学阶乘,'O','O')
>>>
>>>事实(a)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/home/paul/.local/lib/python3.6/site packages/numpy/lib/function\u base.py”,第1972行,在调用中__
返回self.\u矢量化\u调用(func=func,args=vargs)
文件“/home/paul/.local/lib/python3.6/site packages/numpy/lib/function\u base.py”,第2048行,在_vectorize\u调用中
输出=ufunc(*输入)
ValueError:阶乘()只接受整数值
>>>
>>>a[a!=a]=0
>>>事实(a)
排列([9819935662418089743352075922310862095706065486822583658822975979153852871637910339598847876493575760863201233608970580391009961465728060140206398380369810186460532083760537973722230477712617437079362600099095591538946730193485520929914465963675497331037894791629662134417383906616748712477435411911352595846133057242505006764835196420336585309344206359125847804414531691517822911373600118902137858177047463867389635205323328678714656377591230065986360526515442653777496908763065282294664208227077490200850296013058820462199153017425546879776071769432946284989651969735166129654123362278827485074178681546981559466233191972688158356430976918192398846419304865350500808417927115875428971873067092978672051108353026958311731456630717915806992149025378731927814021805881859364498816522297657223802150320368577537638698692463078070519911729996949263069045872688620575874758242248117345983373644762881336075203583068807371386560008413979828440302163961903567206206098114957943899603695885783671168564745354608640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
22328783881661914958481873975346502495151470121092663127656427617172486869336444341196216861471796204456103981797935323465763492125980526669772652700063306391000092324747490987759008282321662774044560021923711172537165034028116470777032463317525690139861312277154265627409161865934581816407380706408159413469087649804140238680046340298380454769197056000000000000000000000000000000000000000000000000000,
1.
61249584099358401539774988285121649211647782880181065019552657036267338153088195303988201779967275642784589505913349592976251572958797164520286603082616258499126414850388770750032832244874744865500684599339365169094265281656246018624169125087086336929008659140773790287427038315506740711640971717627407262119806133914039569804387544893605360482632749642132398074143010093832414811273406748220437584361624445361171146706501836044960640727879585735220969146850637281930634576684379022439144569827759897323120413808197447743317836963898751450642251281351982277623696403714801809091137618510094637754741546381374172490209156669750628265287758243565040756752491082629092890931663069084118249960190350279925210044221389170848672643624902424798289485981643559009642358060100976306359010066013465973059932028926310180595315985960099791957394179039519432507444190747654625992620055591848528852607925564873303749001475451862943569149219508203963665660697011849205174996326078837279628237406181221912723812127044670946612175065696608648876366755523800502033220426264259724448110042998615347327090687044945724644868095726898042638404229137017574884525227292991943592508583104116919096883640157188742952660337139750108570879849335960456768856494175006057451288109527150100807278246132549650716938934121106772599000305859091685578549764454500874996178837114679306052077693402114826710945748516120895211107804543955416170463298478450007640457295281818515443548160000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
1,1],数据类型=对象)
这是一个如何声明数组的问题
a = np.array([[1,np.nan,3],[np.nan, 0, np.nan]])
a=np.insert(a, a.shape[0],[[1, np.nan, 1]], axis=0)
a
array([[ 1., nan, 3.],
[nan, 0., nan],
[ 1., nan, 1.]])
np.nan_to_num(a)
array([[1., 0., 3.],
[0., 0., 0.],
[1., 0., 1.]])
a=np.empty((0,3),dtype='object')
为什么要创建对象
数组?nan\u to_num
不会替换对象
数组中的nan
s。请使用浮点值的常规数组。
a = np.array([[1,np.nan,3],[np.nan, 0, np.nan]])
a=np.insert(a, a.shape[0],[[1, np.nan, 1]], axis=0)
a
array([[ 1., nan, 3.],
[nan, 0., nan],
[ 1., nan, 1.]])
np.nan_to_num(a)
array([[1., 0., 3.],
[0., 0., 0.],
[1., 0., 1.]])