繁体   English   中英

删除numpy数组中的最后一个元素

[英]Remove last elements in a numpy array

我有这个numpy数组:

array([ 0.49010508,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.09438115,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        ])

这是称为allSimilarity的5D numpy数组的第一行。 我已经用np.full()定义了它,并且fill_value是-1。 计算之后,我想删除最后一个无用的-1值。 因此,我计算了大小差异,但是当我使用np.delete()np.resize()allSimilarity[index1][index2][index3][index4] = allSimilarity[index1][index2][index3][index4][:diff].copy() (其中diff是旧尺寸和新尺寸之间的尺寸差),我收到此错误:

ValueError: could not broadcast input array from shape (55) into shape (67)

有什么建议吗?

提前致谢。

希望这可以帮助。

import numpy as np

j = np.array([ 0.49010508,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.09438115,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
    0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        , -1.        , -1.        , -1.        ,
   -1.        , -1.        ])

j = j[j!=-1]
print j

结果:

[ 0.49010508  0.          0.          0.          0.          0.          0.
  0.          0.09438115  0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.          0.
  0.          0.          0.          0.          0.          0.        ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM