我有一个NumPy数组,如下所示:
>>> import numpy
>>> foo = numpy.array(
[[ 1. , 0.3491, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
1. , 0.1648, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
1. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.4269, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.225 , 0.1637, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.4269, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.2929, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.4078, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.4212, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.1719, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.3856, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.147 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.2459, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.3581, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.1676, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.2545, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.0619, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.2195, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.3558, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.1658, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.2544, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0.2159, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])
我想重新索引并重新塑造它,以便它变成以下内容:
>>> bar
array(
[[ 1. , 1. , 1. , 1. , 1. ],
[ 0.4269, 0.225 , 0.4269, 0.2929, 0.4078],
[ 0.4212, 0.1719, 0.3856, 0.147 , 0.2459],
[ 0.3581, 0.1676, 0.2545, 0.0619, 0.2195],
[ 0.3558, 0.1658, 0.2544, 0. , 0.2159],
[ 0.3491, 0.1648, 0. , 0. , 0. ],
[ 0. , 0.1637, 0. , 0. , 0. ]])
有没有一种有效的方法可以在不使用for循环的情况下执行此操作?也许使用broadcasting or strides?
答案 0 :(得分:1)
首先,创建一个numpy数组:
import numpy as np
arr = np.asarray(a)
arr
array([[ 1. , 0.3491, 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 1. , 0.1648, 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 1. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 1. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 1. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.4269, 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0.225 , 0.1637, 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0.4269,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0.2929, 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0.4078, 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.4212, 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0.1719, 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0.3856,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0.147 , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0.2459, 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.3581, 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0.1676, 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0.2545,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0.0619, 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0.2195, 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 0.3558, 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0.1658, 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0.2544,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. , 0.2159, 0. ,
0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])
然后,将其切成:
b = arr[:,0:50:10]
Out[13]: array([[ 1. , 1. , 1. , 1. , 1. ],
[ 0.4269, 0.225 , 0.4269, 0.2929, 0.4078],
[ 0.4212, 0.1719, 0.3856, 0.147 , 0.2459],
[ 0.3581, 0.1676, 0.2545, 0.0619, 0.2195],
[ 0.3558, 0.1658, 0.2544, 0. , 0.2159]])
然后,堆叠您要保留的其他数据:
c = arr[:,1:50:10]
np.vstack((b,c))
Out[17]:
array([[ 1. , 1. , 1. , 1. , 1. ],
[ 0.4269, 0.225 , 0.4269, 0.2929, 0.4078],
[ 0.4212, 0.1719, 0.3856, 0.147 , 0.2459],
[ 0.3581, 0.1676, 0.2545, 0.0619, 0.2195],
[ 0.3558, 0.1658, 0.2544, 0. , 0.2159],
[ 0.3491, 0.1648, 0. , 0. , 0. ],
[ 0. , 0.1637, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ]])
并且更多的切片摆脱了最后一行。
答案 1 :(得分:0)