Reputation: 97
I am writing a program to sort the names of amino acids depending on their energy value within a particular company.
I have extracted relevant data to the following numpy array.
And I tried this
In[37]: Data = np.array([
['ASN 205', -9.64164],
['LEU 206', -8.985774],
['ASN 207', -7.314434],
['PRO 208', -4.105338],
['ASN 209', -2.092342],
['GLY 210', -2.101412],
['LYS 211', -2.483852],
['ARG 212', -24.20364],
['SER 213', -1.181002],
['VAL 214', 0.057618]])
In[38]: ind3 = np.lexsort((Data[:,0],Data[:,1]))
In[39]: Result = Data[ind3]
In[40]: Result
Out[40]:
array([['SER 213', '-1.181002'],
['ASN 209', '-2.092342'],
['GLY 210', '-2.101412'],
['LYS 211', '-2.483852'],
['ARG 212', '-24.20364'],
['PRO 208', '-4.105338'],
['ASN 207', '-7.314434'],
['LEU 206', '-8.985774'],
['ASN 205', '-9.64164'],
['VAL 214', '0.057618']],
dtype='|S9')
But the problem here is float values are arranged in a lexicographic way. I want it be be ordered according to their value means -24.20364 first then ...-2.483852.
How do I do this?
Upvotes: 1
Views: 863
Reputation: 6223
explanation: np.array
converts all the passed arguments to the biggest type fitting all arguments, i.e. your float get converted in line 1.
You can instead use tuples with a specific data type as follows:
Data = np.array([
('ASN 205', -9.64164),
('LEU 206', -8.985774),
('ASN 207', -7.314434),
('PRO 208', -4.105338),
('ASN 209', -2.092342),
('GLY 210', -2.101412),
('LYS 211', -2.483852),
('ARG 212', -24.20364),
('SER 213', -1.181002),
('VAL 214', 0.057618)], dtype=[('f', '|S9'), ('g', float)])
ind3 = np.lexsort((Data['f'], Data['g']))
Result = Data[ind3]Out[8]:
output:
array([('ARG 212', -24.20364), ('ASN 205', -9.64164),
('LEU 206', -8.985774), ('ASN 207', -7.314434),
('PRO 208', -4.105338), ('LYS 211', -2.483852),
('GLY 210', -2.101412), ('ASN 209', -2.092342),
('SER 213', -1.181002), ('VAL 214', 0.057618)],
dtype=[('f', 'S9'), ('g', '<f8')])
Upvotes: 2