NSUUID для NSString

Подход № 1

Вот что-то в numpythonic векторизованном способе, основанном на np.bincount -

# Initial setup             
N = A.shape[1]-1
unqA1, id = np.unique(A[:, 0], return_inverse=True)

# Create subscripts and accumulate with bincount for tagged summations
subs = np.arange(N)*(id.max()+1) + id[:,None]
sums = np.bincount( subs.ravel(), weights=A[:,1:].ravel() )

# Append the unique elements from first column to get final output
out = np.append(unqA1[:,None],sums.reshape(N,-1).T,1)

Пример ввода, вывод -

In [66]: A
Out[66]: 
array([[1, 2, 3],
       [1, 4, 6],
       [2, 3, 5],
       [2, 6, 2],
       [7, 2, 1],
       [2, 0, 3]])

In [67]: out
Out[67]: 
array([[  1.,   6.,   9.],
       [  2.,   9.,  10.],
       [  7.,   2.,   1.]])

Подход # 2

Вот еще один, основанный на np.cumsum и np.diff -

# Sort A based on first column
sA = A[np.argsort(A[:,0]),:]

# Row mask of where each group ends
row_mask = np.append(np.diff(sA[:,0],axis=0)!=0,[True])

# Get cummulative summations and then DIFF to get summations for each group
cumsum_grps = sA.cumsum(0)[row_mask,1:]
sum_grps = np.diff(cumsum_grps,axis=0)

# Concatenate the first unique row with its counts
counts = np.concatenate((cumsum_grps[0,:][None],sum_grps),axis=0)

# Concatenate the first column of the input array for final output
out = np.concatenate((sA[row_mask,0][:,None],counts),axis=1)

Бенчмаркинг

Вот некоторые тесты времени выполнения для основанных на numpy подходов, представленных до сих пор для вопроса -

In [319]: A = np.random.randint(0,1000,(100000,10))

In [320]: %timeit cumsum_diff(A)
100 loops, best of 3: 12.1 ms per loop

In [321]: %timeit bincount(A)
10 loops, best of 3: 21.4 ms per loop

In [322]: %timeit add_at(A)
10 loops, best of 3: 60.4 ms per loop

In [323]: A = np.random.randint(0,1000,(100000,20))

In [324]: %timeit cumsum_diff(A)
10 loops, best of 3: 32.1 ms per loop

In [325]: %timeit bincount(A)
10 loops, best of 3: 32.3 ms per loop

In [326]: %timeit add_at(A)
10 loops, best of 3: 113 ms per loop

Похоже, Approach #2: cumsum + diff довольно неплохо.

20
задан Biribu 12 February 2014 в 08:50
поделиться