Creating this numpy array in Python

假如想象 提交于 2019-12-06 06:51:04

You can use triu_indices:

i0,i1 = np.triu_indices(4,1)
a[i0]
# array([1, 1, 1, 2, 2, 6])
a[i1]
# array([2, 6, 8, 6, 8, 8])
a[i0]+a[i1]
# array([ 3,  7,  9,  8, 10, 14])

For more terms we need to build our own "nd_triu_idx". Here is how to do it for 3 terms out of a list of 5:

n = 5
full = np.mgrid[:n,:n,:n]
nd_triu_idx = full[:,(np.diff(full,axis=0)>0).all(axis=0)]
nd_triu_idx
# array([[0, 0, 0, 0, 0, 0, 1, 1, 1, 2],
#        [1, 1, 1, 2, 2, 3, 2, 2, 3, 3],
#        [2, 3, 4, 3, 4, 4, 3, 4, 4, 4]])

To fully generalize the number of terms use something like

k = 4
full = np.mgrid[k*(slice(n),)]

etc.

You can do combinations on your array of size 2 and sum each one:

import numpy as np
from itertools import combinations

a = np.array([1,2,6,8])

print(list(map(sum, combinations(a, 2))))
# [3, 7, 9, 8, 10, 14]

Or using numpy:

import numpy as np

a = np.array([1,2,6,8,1])

b = a + a[:,None]
print(b[np.triu_indices(4, 1)])
# [ 3  7  9  8 10 14]

What about computing the cartesian product of exponentiated version of a?

>>> a = np.array([1, 2, 6, 8])[:, None]
>>> b = np.exp(a)
>>> np.unique(np.tril(np.log(np.dot(b, b.T)), k=-1))[1:]
array([ 3.,  7.,  8.,  9., 10., 14.])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!