python numpy数组在单个轴上迭代

时间:2014-08-13 16:12:57

标签: python arrays numpy

我见过几个与我类似的问题,但我找不到适合我的问题。 我想迭代我的数组中的一个单轴,而不使用2个for循环使其更快。

首先,我打开一堆图片并将它们附加到togheter(转换为np数组)

获得这样的数组数组后:

ffImageArr[0]
array([[  45.49061198,  172.49061198,  174.49061198, ...,   30.49061198,
         -71.50938802,  -69.50938802],
       [  60.49061198,  169.49061198,  183.49061198, ...,    0.49061198,
         -83.50938802,  -66.50938802],
       [  55.49061198,  133.49061198,  135.49061198, ...,  -43.50938802,
        -130.50938802,  -99.50938802],
       ..., 
       [ 118.49061198,  203.49061198,  195.49061198, ...,  182.49061198,
          97.49061198,  132.49061198],
       [ 108.49061198,  238.49061198,  197.49061198, ...,  121.49061198,
          99.49061198,  133.49061198],
       [ 118.49061198,  232.49061198,  196.49061198, ...,  130.49061198,
         123.49061198,  145.49061198]])

ffImageArr[1]
array([[  43.59677409,  172.59677409,  173.59677409, ...,   29.59677409,
         -73.40322591,  -71.40322591],
       [  60.59677409,  167.59677409,  182.59677409, ...,    0.59677409,
         -86.40322591,  -64.40322591],
       [  55.59677409,  133.59677409,  134.59677409, ...,  -46.40322591,
        -131.40322591, -102.40322591],
       ..., 
       [ 119.59677409,  201.59677409,  194.59677409, ...,  180.59677409,
          98.59677409,  131.59677409],
       [ 109.59677409,  238.59677409,  197.59677409, ...,  119.59677409,
          98.59677409,  134.59677409],
       [ 117.59677409,  231.59677409,  197.59677409, ...,  129.59677409,
         122.59677409,  144.59677409]])

ffImageArr[2]
array([[  42.16040365,  174.16040365,  177.16040365, ...,   28.16040365,
         -75.83959635,  -74.83959635],
       [  59.16040365,  168.16040365,  183.16040365, ...,   -1.83959635,
         -87.83959635,  -66.83959635],
       [  54.16040365,  133.16040365,  135.16040365, ...,  -47.83959635,
        -133.83959635, -103.83959635],
       ..., 
       [ 119.16040365,  203.16040365,  196.16040365, ...,  182.16040365,
          98.16040365,  132.16040365],
       [ 108.16040365,  240.16040365,  199.16040365, ...,  121.16040365,
          98.16040365,  132.16040365],
       [ 116.16040365,  232.16040365,  196.16040365, ...,  129.16040365,
         122.16040365,  143.16040365]])

ffImageArr[3]
array([[  43.89271484,  174.89271484,  175.89271484, ...,   28.89271484,
         -78.10728516,  -75.10728516],
       [  59.89271484,  169.89271484,  183.89271484, ...,   -2.10728516,
         -89.10728516,  -67.10728516],
       [  54.89271484,  132.89271484,  135.89271484, ...,  -50.10728516,
        -137.10728516, -105.10728516],
       ..., 
       [ 118.89271484,  204.89271484,  195.89271484, ...,  181.89271484,
          98.89271484,  131.89271484],
       [ 108.89271484,  240.89271484,  199.89271484, ...,  121.89271484,
          98.89271484,  134.89271484],
       [ 118.89271484,  234.89271484,  199.89271484, ...,  128.89271484,
         123.89271484,  145.89271484]])

我的目标是尽可能快地检索包含每个数组的n元素的数组,数组和数组。

喜欢array =[45.49061198,43.59677409,42.16040365...]

我试过

for i in range(ffImageArr.shape[0]):
    print ffImageArr[i,:,:]

但奇怪的是,[i,:,:]提供的内容与[:,i:]

相同

感谢您的帮助和解释!

编辑: 在此期间我写的代码,我会尝试直接使用polyfit:

for k in range (ffImageArr.shape[1]):
   for i in range(ffImageArr.shape[2]):
 fffunc = []
 for j in range(ffImageArr.shape[0]): 
    fffunc.append(ffImageArr[j,k,i])
 fffunc = np.array(fffunc)
 a = np.polyfit(tempArr,fffunc,1)
 firstOrder0.append(a[1])
 firstOrder1.append(a[0])
 b = np.polyfit(tempArr,fffunc,2)   
 secondOrder0.append(b[2])
 secondOrder1.append(b[1])  
 secondOrder2.append(b[1])
 c = np.polyfit(tempArr,fffunc,3)   
 thirdOrder0.append(c[3])
 thirdOrder1.append(c[2])   
 thirdOrder2.append(c[1])
 thirdOrder3.append(c[0])

0 个答案:

没有答案