Rational Girl

Attempting to be rational while dreaming of 3.141592653589793...

Sorting structured array

Here is a nice numpy quickie that can be handy.

Lets say you have some data of mixed type, and you need to do some fancy sorting

import numpy as np

subid = ['B12-01', 'B12-05', 'B12-10', 'B13-02', 'B13-05']
age = [ 65, 72, 62, 88, 71]
biomarker_index = [1.312, 2.335, 1.122, 1.091, 3.005]
values = [(a,b,c) for a,b,c in zip(subid, age, biomarker_index)]
dtype = [('subid', 'S5'), ('age', int), ('biom', float)]
sarray = np.array(values, dtype=dtype)

You now have a structured array that allows you to easily index, the data, but it also makes sorting the data easy For example, you could sort by age, then by biomarker,and get your subids in this new order.


age_biom_sorted = np.sort(sarray, order=['age', 'biom'])