简体   繁体   English

沿2D数组排序数组

[英]Sorting an Array Alongside a 2d Array

So I'm using NumPy's linear algebra routines to do some basic computational quantum mechanics. 因此,我正在使用NumPy的线性代数例程来执行一些基本的计算量子力学。 Say I have a matrix, hamiltonian, and I want its eigenvalues and eigenvectors 说我有一个汉密尔顿矩阵,我想要它的特征值和特征向量

import numpy as np
from numpy import linalg as la

hamiltonian = np.zeros((N, N)) # N is some constant I have defined
# fill up hamiltonian here
energies, states = la.eig(hamiltonian)

Now, I want to sort the energies in increasing order, and I want to sort the states along with them. 现在,我想按升序对能量进行排序,并希望将状态与它们一起排序。 For example, if I do: 例如,如果我这样做:

groundStateEnergy = min(energies)
groundStateIndex = np.where(energies == groundStateEnergy)
groundState = states[groundStateIndex, :]

I correctly plot the ground state (eigenvector with the lowest eigenvalue). 我正确地绘制了基态(特征值最低的特征向量)。 However, if I try something like this: 但是,如果我尝试这样的事情:

energies, states = zip(*sorted(zip(energies, states)))

or even 甚至

energies, states = zip(*sorted(zip(energies, states), key = lambda pair:pair[0])))

plotting in the same way no longer plots the correct state.So how can I sort states alongside energies, but only by row? 以相同的方式绘制不再能绘制正确的状态,那么我如何才能将状态与能量并列,而只能按行排序? (ie, I want to associate each row of states with a value in energies, and I want to rearrange the rows so that the ordering of the rows corresponds to the sorted ordering of the values in energies) (即,我想将每行状态与一个能量值相关联,并且我想重新排列各行,以便各行的顺序与能量值的排序顺序相对应)

You can use argsort as follows: 您可以按以下方式使用argsort

>>> x = np.random.random((1,10))

>>> x
array([ 0.69719108,  0.75828237,  0.79944838,  0.68245968,  0.36232211,
        0.46565445,  0.76552493,  0.94967472,  0.43531813,  0.22913607])
>>> y = np.random.random((10))
>>> y
array([ 0.64332275,  0.34984653,  0.55240204,  0.31019789,  0.96354724,
    0.76723872,  0.25721343,  0.51629662,  0.13096252,  0.86220311])
>>> idx = np.argsort(x)
>>> idx
array([9, 4, 8, 5, 3, 0, 1, 6, 2, 7])
>>> xsorted= x[idx]
>>> xsorted
array([ 0.22913607,  0.36232211,  0.43531813,  0.46565445,  0.68245968,
        0.69719108,  0.75828237,  0.76552493,  0.79944838,  0.94967472])
>>> ysordedbyx = y[idx]
>>> ysordedbyx
array([ 0.86220311,  0.96354724,  0.13096252,  0.76723872,  0.31019789,
        0.64332275,  0.34984653,  0.25721343,  0.55240204,  0.51629662])

and as suggested by the comments an example where we sort a 2d array by it's first collumn 正如评论所建议的那样,我们通过第一个柱子对二维数组进行排序

>>> x=np.random.random((10,2))
>>> x
array([[ 0.72789275,  0.29404982],
       [ 0.05149693,  0.24411234],
       [ 0.34863983,  0.58950756],
       [ 0.81916424,  0.32032827],
       [ 0.52958012,  0.00417253],
       [ 0.41587698,  0.32733306],
       [ 0.79918377,  0.18465189],
       [ 0.678948  ,  0.55039723],
       [ 0.8287709 ,  0.54735691],
       [ 0.74044999,  0.70688683]])
>>> idx = np.argsort(x[:,0])
>>> idx
array([1, 2, 5, 4, 7, 0, 9, 6, 3, 8])
>>> xsorted = x[idx,:]
>>> xsorted
array([[ 0.05149693,  0.24411234],
       [ 0.34863983,  0.58950756],
       [ 0.41587698,  0.32733306],
       [ 0.52958012,  0.00417253],
       [ 0.678948  ,  0.55039723],
       [ 0.72789275,  0.29404982],
       [ 0.74044999,  0.70688683],
       [ 0.79918377,  0.18465189],
       [ 0.81916424,  0.32032827],
       [ 0.8287709 ,  0.54735691]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM