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 Both sides next revision
other:python:misc_by_jyp [2022/02/21 12:52]
jypeter Added numpy section
other:python:misc_by_jyp [2022/02/21 14:47]
jypeter [numpy related stuff] Added np.unique example
Line 223: Line 223:
 ==== numpy related stuff ==== ==== 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. ])
 +>>>​ np.unique(vals,​ return_counts=True)
 +(array([1. , 1.5, 2. ]), array([3, 3, 4]))
 +>>>​ np.sort(vals) # Sorted copy, in order to check the result
 +array([1. , 1. , 1. , 1.5, 1.5, 1.5, 2. , 2. , 2. , 2. ])</​code>​
  
  
other/python/misc_by_jyp.txt · Last modified: 2024/04/19 12:02 by jypeter