I've this 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. ])
which is the first row of the 5D numpy array called allSimilarity
. I have defined it with np.full()
and the fill_value
is -1. After computing, I would want to remove last unuseful -1
value. So, I calculate the size difference, but when I use np.delete()
or np.resize()
or allSimilarity[index1][index2][index3][index4] = allSimilarity[index1][index2][index3][index4][:diff].copy()
(where diff
is the size difference between old size and new size) I got this error:
这是名为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)
Any advice?
Thanks in advance.
提前致谢。
1 个解决方案
#1
1
Hope this helps.
希望这可以帮助。
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
Result:
[ 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
Hope this helps.
希望这可以帮助。
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
Result:
[ 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. ]