Added example

This commit is contained in:
Nicolas Rougier 2014-05-27 06:50:50 +02:00
parent e207cb18c9
commit a4491d1ed6

View File

@ -364,6 +364,22 @@ Journeyman
sum = A.reshape(A.shape[:-2] + (-1,)).sum(axis=-1)
7. Considering a one-dimensional vector D, how to compute means of subsets of D
using a vector S of same size describing subset indices ?
.. code:: python
# Jaime Fernández del Río
D = np.random.uniform(0,1,100)
S = np.random.randint(0,10,100)
D_sums = np.bincount(S, weights=D)
D_counts = np.bincount(S)
D_means = D_sums / D_counts
Craftsman
@ -406,6 +422,7 @@ Craftsman
.. code:: python
# Jaime Fernández del Río
C = np.bincount([1,1,2,3,4,4,6])
A = np.repeat(np.arange(len(C)), C)