2015-04-16 152 views
0

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

+0

您是否認爲有人可以複製並粘貼您發佈的數據?發佈類似於您的預期輸出的數組的'repr()'輸出。 –

+0

謝謝格式化@bignose – stats134711

回答

0

你可以reshape用Fortran順序陣列(或使用轉置的組合),然後slice陣列僅提取頂部7行:

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

這裏選擇的新的形狀是基於所述位置在你的數組中。當你壓扁數組,Fortran命令,然後你看到你的模式出現時,這就變得清晰了。

+1

謝謝!這工作得很好。這個答案是最有意義的,因爲我實際上是試圖通過在Python中包裝C函數來在R(基於Fortran)中複製結果。奇怪的事情發生,你可以看到... – stats134711

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. ]]) 

和更多的切片擺脫最後一行。

+0

非常感謝你! – stats134711