User Tools

Site Tools


other:python:misc_by_jyp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
other:python:misc_by_jyp [2022/02/21 16:15] – [numpy related stuff] jypeterother:python:misc_by_jyp [2022/02/21 17:31] – [numpy related stuff] Added ufuncs jypeter
Line 253: Line 253:
 15.5 15.5
 >>> vals.sum() # The usual and easy way to do it >>> vals.sum() # The usual and easy way to do it
-15.5</code>+15.5 
 + 
 +# Compute the sum of the elements of 'nb_unique' 
 +# AND keep (accumulate) the intermediate results 
 +>>> nb_unique 
 +array([3, 3, 4]) 
 +>>> np.add.accumulate(nb_unique) 
 +array([ 3,  6, 10]) 
 + 
 +# The accumulated values can be used as indices to separate the different groups of sorted values! 
 +>>> sorted_vals 
 +array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ]) 
 +>>> sorted_vals[0:3] 
 +array([1., 1., 1.]) 
 +>>> sorted_vals[3:6] 
 +array([1.5, 1.5, 1.5]) 
 +>>> sorted_vals[6:10] 
 +array([2., 2., 2., 2.]) 
 + 
 +# Compute the sum of each equal-value group 
 +>>> sorted_vals[0:3].sum(), sorted_vals[3:6].sum(), sorted_vals[6:10].sum() 
 +(3.0, 4.5, 8.0)</code>
  
 /* /*
other/python/misc_by_jyp.txt · Last modified: 2024/11/04 15:01 by jypeter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki