我有一個NumPy的陣列,其看起來如下:索引/在Python重塑矩陣以匹配目標矩陣
>>> 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-loops?也許使用broadcasting or strides?
您是否認爲有人可以複製並粘貼您發佈的數據?發佈類似於您的預期輸出的數組的'repr()'輸出。 –
謝謝格式化@bignose – stats134711