与Python内建函数类似,使用sort方法按位置排序。
In [112]: arr = np.random.randn(6)
In [113]: arr
Out[113]:
array([-1.54009488, 0.14138051, 0.07455801, 0.74340943, -0.70681659,
-0.1163607 ])
In [114]: arr.sort()
In [115]: arr
Out[115]:
array([-1.54009488, -0.70681659, -0.1163607 , 0.07455801, 0.14138051,
0.74340943])
In [116]:
In [116]: arr = np.random.randn(5, 3)
In [117]: arr
Out[117]:
array([[ 0.20103223, 1.05742459, 0.57607095],
[-1.44059807, 0.97252296, 0.20796782],
[ 0.12345811, -0.46441879, 0.31409119],
[-0.96164247, -1.12039945, -0.39718085],
[ 1.26915581, 0.79770197, 0.33933553]])
In [118]: arr.sort(1)
In [119]: arr
Out[119]:
array([[ 0.20103223, 0.57607095, 1.05742459],
[-1.44059807, 0.20796782, 0.97252296],
[-0.46441879, 0.12345811, 0.31409119],
[-1.12039945, -0.96164247, -0.39718085],
[ 0.33933553, 0.79770197, 1.26915581]])
In [120]: arr.sort(0)
In [121]: arr
Out[121]:
array([[-1.44059807, -0.96164247, -0.39718085],
[-1.12039945, 0.12345811, 0.31409119],
[-0.46441879, 0.20796782, 0.97252296],
[ 0.20103223, 0.57607095, 1.05742459],
[ 0.33933553, 0.79770197, 1.26915581]])
网友评论