问题描述
如果存在NaN值,则后面的justify function应该将数据fown移动到行中(这样,NaN将位于顶部。 但是不知何故,“向下”选项与“正确”功能相同。 如何解决?
以下是可再现的数据(3d数组):
import numpy as np
def justify(a,invalid_val=0,axis=1,side='left'):
""" Justifies a 2D array
Parameters
----------
A : ndarray
Input array to be justified
axis : int
Axis along which justification is to be made
side : str
Direction of justification. It Could be 'left','right','up','down'
It should be 'left' or 'right' for axis=1 and 'up' or 'down' for axis=0.
"""
if invalid_val is np.nan:
mask = ~np.isnan(a)
else:
mask = a!=invalid_val
justified_mask = np.sort(mask,axis=axis)
if (side=='up') | (side=='left'):
justified_mask = np.flip(justified_mask,axis=axis)
out = np.full(a.shape,invalid_val)
if axis==1:
out[justified_mask] = a[mask]
else:
out.T[justified_mask.T] = a.T[mask.T]
return out
a = np.asarray([
np.asarray([np.asarray([ 1.21643707,0.9280912,5.20711915]),np.asarray([-2.01148217,2.72869681,2.54161257]),np.asarray([ 0.49170286,0.72304396,1.56706948]),np.asarray([-0.77553082,2.74300372,0.10107189]),np.asarray([ 2.54368976,0.53107898,0.09351025]),np.asarray([ 1.03176737,-0.54742843,np.nan])])])
b = justify(a,invalid_val=np.nan,side='down')
print(a)
print(b)
实际输出:
#a
[[[ 1.21643707 0.9280912 5.20711915]
[-2.01148217 2.72869681 2.54161257]
[ 0.49170286 0.72304396 1.56706948]
[-0.77553082 2.74300372 0.10107189]
[ 2.54368976 0.53107898 0.09351025]
[ 1.03176737 -0.54742843 nan]]]
_
#b
[[[ 1.21643707 0.9280912 nan]
[ 5.20711915 -2.01148217 2.72869681]
[ 2.54161257 0.49170286 0.72304396]
[ 1.56706948 -0.77553082 2.74300372]
[ 0.10107189 2.54368976 0.53107898]
[ 0.09351025 1.03176737 -0.54742843]]]
实际预期输出:
#a
[[[ 1.21643707 0.9280912 5.20711915]
[-2.01148217 2.72869681 2.54161257]
[ 0.49170286 0.72304396 1.56706948]
[-0.77553082 2.74300372 0.10107189]
[ 2.54368976 0.53107898 0.09351025]
[ 1.03176737 -0.54742843 nan]]]
_
# expected b
[[[ 1.21643707 0.9280912 nan]
[-2.01148217 2.72869681 5.20711915]
[ 0.49170286 0.72304396 2.54161257]
[-0.77553082 2.74300372 1.56706948]
[ 2.54368976 0.53107898 0.10107189]
[ 1.03176737 -0.54742843 0.09351025]]]
因此,“真实”输出与输入side =“ right”相同。为什么会发生这种情况,或者有更好的解决办法?
编辑:不自觉地写了“我的”,我并不是说它是我的,对不起。 (来源:Divakar)
EDIT2:更详细的示例(3d):
# in reality were dealing with about 1million samples (==2d matrices) of 50 'columns' and 10.000 'rows'
samples = np.asarray([np.asarray([
np.asarray([89.319787,1.329743,99.234670,52.329743,0.319787,2.319787]),np.asarray([84.319787,49.329743,0.319,np.asarray([12.319787,np.nan,33.329743,np.asarray([33.319787,23.329743,np.asarray([23.319787,45.234670,0.32721,np.asarray([89.319787,2.319787])
]),np.asarray([
np.asarray([89.319787,np.asarray([np.nan,2.319787])])])
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)