python - average of all rows corresponing to all unique rows -
i have numpy array 2 columns:
a = [[1,1,1,2,3,1,2,3],[0.1,0.2,0.2,0.1,0.3,0.2,0.2,0.1]]
for uniques in first column, want average of values corresponding it. example
b = [[1,2,3], [0.175, 0.15, 0.2]]
is there pythonic way this?
i think following standard numpy approach these kind of computations. call np.unique
can skipped if entries of a[0]
small integers, makes whole operation more robust , independent of actual data.
>>> = [[1,1,1,2,3,1,2,3],[0.1,0.2,0.2,0.1,0.3,0.2,0.2,0.1]] >>> unq, unq_idx = np.unique(a[0], return_inverse=true) >>> unq_sum = np.bincount(unq_idx, weights=a[1]) >>> unq_counts = np.bincount(unq_idx) >>> unq_avg = unq_sum / unq_counts >>> unq array([1, 2, 3]) >>> unq_avg array([ 0.175, 0.15 , 0.2 ])
you of course stack both arrays, although convert unq
float dtype:
>>> np.vstack((unq, unq_avg)) array([[ 1. , 2. , 3. ], [ 0.175, 0.15 , 0.2 ]])
Comments
Post a Comment