2016-01-20 91 views
1

我想使用一個單獨的函數計算矩陣值,並將第一列和第一行作爲輸入。從第一行和第一列計算數組值輸入

我想優化下面的代碼進行諮詢:

#imports 
import numpy as np 
import pandas as pd 


#numpy variant 

#creation of sample matrix 
x_range = range(-180, -80, 20) 
y_range = range(5, 30, 5) 

ma = np.zeros(shape=(6,6)) 
ma[0,1:] = x_range 
ma[1:,0] = y_range 
ma[0,0] = np.nan 

# test function: 
def test_func(x, y): 
    z = (x + y)/10 

    return z 

#looping 
for y in range(1, len(ma[0,:])): 
    for x in range(1, len(ma[:,0])): 
     #print ma[0, x], ma[y, 0] 
     ma[x, y] = test_func(ma[0, x], ma[y, 0]) 

ma 
array([[ nan, -180. , -160. , -140. , -120. , -100. ], 
     [ 5. , -17.5, -17. , -16.5, -16. , -15.5], 
     [ 10. , -15.5, -15. , -14.5, -14. , -13.5], 
     [ 15. , -13.5, -13. , -12.5, -12. , -11.5], 
     [ 20. , -11.5, -11. , -10.5, -10. , -9.5], 
     [ 25. , -9.5, -9. , -8.5, -8. , -7.5]]) 

#pandas variant 

cols = x_range 
idx = y_range 

#dataframe 
df = pd.DataFrame(np.zeros(shape=(5,5)), index=idx, columns=cols) 
df.index.name = 'Y-Range' 
df.columns.name = 'X-Range' 

df 
X-Range -180 -160 -140 -120 -100 
Y-Range        
5   0  0  0  0  0 
10   0  0  0  0  0 
15   0  0  0  0  0 
20   0  0  0  0  0 
25   0  0  0  0  0 


#looping 
for col in range(0, (len(df.columns))): 
    for ind in range(0, (len(df.index))): 
     df.iloc[ind, col] = test_func(df.index[ind], df.columns[col]) 

df 
X-Range -180 -160 -140 -120 -100 
Y-Range        
5   -18 -16 -14 -12 -10 
10  -17 -15 -13 -11 -9 
15  -17 -15 -13 -11 -9 
20  -16 -14 -12 -10 -8 
25  -16 -14 -12 -10 -8 
#rounding due to console settings 

上面這正是我想要的。

但是有沒有更好的方法,更有效率和避免循環?

注意: 感謝您的答覆。

+0

如果你已經實現了一個可以工作的loopy版本,把它添加到問題中? – Divakar

+0

什麼是推薦的最終輸出? – jezrael

+0

在 – DaCoEx

回答

0

您可以使用addT

df = pd.DataFrame(ma) 
print df 

    0 1 2 3 4 5 
0 NaN -180 -160 -140 -120 -100 
1 5 0 0 0 0 0 
2 10 0 0 0 0 0 
3 15 0 0 0 0 0 
4 20 0 0 0 0 0 
5 25 0 0 0 0 0 

df.ix[1:,1:] = (df.ix[1:,1:].add(df.ix[1:,0],axis=0) + df.ix[0,1:]).T 
print df 

    0 1 2 3 4 5 
0 NaN -180 -160 -140 -120 -100 
1 5 -175 -170 -165 -160 -155 
2 10 -155 -150 -145 -140 -135 
3 15 -135 -130 -125 -120 -115 
4 20 -115 -110 -105 -100 -95 
5 25 -95 -90 -85 -80 -75 
3

設立ma後,您可以通過擴展切片部​​分之一的尺寸更換的量化方法嵌套循環:ma[0,1:]添加與另一切片ma[1:,0],這將帶來broadcasting發揮。作爲一種矢量化方法,這一定非常快。因此,循環置換是這樣的 -

ma[1:,1:] = ma[0,1:][:,None] + ma[1:,0] 

請注意,以更緊湊的方式來寫ma[0,1:][:,None]ma[0,1:,None]

相關問題