Python中的索引/重塑矩阵以匹配目标矩阵

时间:2015-04-16 22:05:32

标签: python numpy matrix indexing

我有一个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

2 个答案:

答案 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)

您可以按Fortran顺序reshape数组(或使用转置组合),然后slice数组只提取前7行:

a.reshape(50, -1, order='F')[:7,:]

此处选择的新形状基于阵列中的形状。当你展平数组,Fortran订单,然后你看到你的模式出现时,这就变得清晰了。