對於分割的問題,我們可以有在使用時的np.triu_indices
生成那些列數的配對組合的通用解決方案,像所謂
from __future__ import division
r,c = np.triu_indices(arr.shape[1],1) # pairwise column IDs
out = arr[:,r]/arr[:,c]
樣品運行
1)3列(驗證輸出值):
In [74]: arr = np.random.randint(11,99,(5,3)) #Sample input array with 3 cols
In [75]: r,c = np.triu_indices(arr.shape[1],1)
In [76]: arr[:,r]/arr[:,c]
Out[76]:
array([[ 1.85714286, 0.4875 , 0.2625 ],
[ 0.94565217, 0.92553191, 0.9787234 ],
[ 0.45652174, 0.24137931, 0.52873563],
[ 0.84931507, 0.69662921, 0.82022472],
[ 0.23170732, 0.52777778, 2.27777778]])
In [77]: arr[:,0]/arr[:,1]
Out[77]: array([ 1.85714286, 0.94565217, 0.45652174, 0.84931507, 0.23170732])
In [78]: arr[:,0]/arr[:,2]
Out[78]: array([ 0.4875 , 0.92553191, 0.24137931, 0.69662921, 0.52777778])
In [79]: arr[:,1]/arr[:,2]
Out[79]: array([ 0.2625 , 0.9787234 , 0.52873563, 0.82022472, 2.27777778])
2)20列(驗證輸出):
In [71]: arr = np.random.randint(11,99,(5,20)) #Sample input array with 20 cols
In [72]: r,c = np.triu_indices(arr.shape[1],1)
In [73]: (arr[:,r]/arr[:,c]).shape # Verify output array shape
Out[73]: (5, 190)
第二個問題:'平均(A,B)[i] ..'對我來說不是很清楚。你能否在那裏澄清一下,或者添加一個適用於較小數據集的工作代碼? – Divakar
我在想是這樣,這樣的: 高清myDiv(X,Y): 返回X/Y ARR = np.random.ranf((5,4)) R,C = np.triu_indices( arr.shape [1],1) #arr [:,r]/arr [:,c] #new ratios newArr = myDiv(arr [:,r],arr [:,c]) –
Please add to這個問題。很難從評論中閱讀。 – Divakar