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 revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
other:python:misc_by_jyp [2021/09/17 14:05]
jypeter [Printing a readable version of long lists or dictionaries] Added long list example
other:python:misc_by_jyp [2022/02/21 16:46]
jypeter [numpy related stuff] Added reduceat
Line 165: Line 165:
  
 Check the [[https://​docs.python.org/​3/​library/​collections.html#​collections.OrderedDict|OrderedDict class]] (''​from collections import OrderedDict''​) and the [[https://​realpython.com/​python-ordereddict/​|OrderedDict vs dict in Python: The Right Tool for the Job]] tutorial Check the [[https://​docs.python.org/​3/​library/​collections.html#​collections.OrderedDict|OrderedDict class]] (''​from collections import OrderedDict''​) and the [[https://​realpython.com/​python-ordereddict/​|OrderedDict vs dict in Python: The Right Tool for the Job]] tutorial
 +
 +==== Using sets ====
 +
 +[[https://​docs.python.org/​3/​tutorial/​datastructures.html#​sets|Python sets]] are **groups of unique elements**. They can be used to easily find all the unique elements of //​something//​ and you can easily determine the **intersection**,​ **union** (and other similar operations) of sets.
  
 ==== Printing a readable version of long lists or dictionaries ==== ==== Printing a readable version of long lists or dictionaries ====
Line 199: Line 203:
  
 </​code>​ </​code>​
 +
 +==== Sorting ====
 +
 +  * When dealing with **numerical values**, you should use the [[https://​numpy.org/​doc/​stable/​reference/​routines.sort.html|numpy sorting, searching, and counting routines]]!
 +  * [[https://​docs.python.org/​3/​howto/​sorting.html|Sorting HOW TO]]
 +  * Example: sorting the keys and the values of a dictionary, and then using the ''​key''​ parameter to sort the keys of a dictionary according to the value associated with the key
 +    * If we provide a ''​key''​ function, the ''​sort''​ function will sort the elements by the values returned by the function, instead of sorting by the initial values. The function used for generating the key below is very simple and we can use a //lambda// (i.e //in place//) function
 +    * <​code>>>>​ demo_dic = {'​a':​10,​ '​b':​5,​ '​c':​-1,​ '​d':​0}
 +
 +>>>​ sorted(demo_dic.keys())
 +['​a',​ '​b',​ '​c',​ '​d'​]
 +
 +>>>​ sorted(demo_dic.values())
 +[-1, 0, 5, 10]
 +
 +>>>​ sorted(demo_dic.keys(),​ key=lambda key_name:​demo_dic[key_name])
 +['​c',​ '​d',​ '​b',​ '​a'​]</​code>​
 +
 +==== numpy related stuff ====
 +
 +=== Finding and counting unique values ===
 +
 +Use ''​np.unique'',​ do **not** try to use histogram related functions!
 +
 +<​code>>>>​ vals = np.random.randint(2,​ 5, (10,)) * 0.5 # Get 10 discreet float values
 +>>>​ vals
 +array([1. , 2. , 1. , 2. , 2. , 1.5, 1. , 1.5, 2. , 1.5])
 +
 +>>>​ np.unique(vals)
 +array([1. , 1.5, 2. ])
 +>>>​ unique_vals,​ nb_unique = np.unique(vals,​ return_counts=True)
 +>>>​ unique_vals
 +array([1. , 1.5, 2. ])
 +>>>​ nb_unique
 +array([3, 3, 4])
 +
 +>>>​ sorted_vals = np.sort(vals) # Sorted copy, in order to check the result
 +>>>​ sorted_vals
 +array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ])</​code>​
 +
 +=== Applying a ufunc over all the elements of an array ===
 +
 +There are all sorts of //ufuncs// (Universal Functions), and we will just use below ''​add''​ from the [[https://​numpy.org/​doc/​stable/​reference/​ufuncs.html#​math-operations|math operations]],​ applied on the arrays defined in [[#​finding_and_counting_unique_values|Finding and counting unique values]]
 +
 +<​code>#​ Get the sum of all the elements of '​vals'​
 +>>>​ np.add.reduce(vals)
 +15.5
 +>>>​ np.add.reduce(sorted_vals)
 +15.5
 +>>>​ vals.sum() # The usual and easy way to do it
 +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>​
 +
 +=== Applying a ufunc over specified sections of an array ===
 +
 +The [[https://​numpy.org/​doc/​stable/​reference/​generated/​numpy.ufunc.reduceat.html#​numpy.ufunc.reduceat|reduceat]] function can be used to avoid explicit python loops, and improve the speed (but not the readability...) of a script. The example below //​improves//​ what has been shown above
 +
 +<​code>#​ Define a list with the boundaries of the intervals we want to apply the '​add'​ function to
 +# We need to add the beginning index (0), AND remove the last index
 +# (reduceat will automatically go to the end of the input array
 +>>>​ nb_unique
 +array([3, 3, 4])
 +>>>​ slices_indices = [0] + list(np.add.accumulate(nb_unique))
 +>>>​ slices_indices.pop() # Remove last element
 +10
 +>>>​ slices_indices
 +[0, 3, 6]
 +
 +# Compute the sums over the selected intervals with just one call
 +>>>​ np.add.reduceat(np.sort(vals),​ slices_indices)
 +array([3. , 4.5, 8. ])</​code>​
  
 /* /*
other/python/misc_by_jyp.txt · Last modified: 2024/04/19 12:02 by jypeter