赞
踩
如果解决了你的问题,点个赞再走嘛٩(๑❛ᴗ❛๑)۶
1.np.sort(a,axis=1-,kind=None,order=None)给数组排序(升序)
2.np.argsort(a,axis=-1,kind,=None,order=None)给数组升序排序并返回对应索引值
参数说明:
- import numpy as np
-
- a = np.random.randint(0, 10, size=10)
- b = np.array([[3, 1, 30, 45],
- [41, 2, 34, 1],
- [3, 42, 56, 34]])
- print('a =', a)
- print('升序np.sort(a) = ', np.sort(a)) # 升序
- print('降序=', np.sort(a)[::-1]) # 降序
- print('np.argsort(a) = ', np.argsort(a)) # 升序,返回的结果是排序完后对应原先数组的索引值
- print('np.argsort(a) = ', np.argsort(-a)) # 降序,返回的结果是排序完后对应原先数组的索引值
-
- # 注意axis,对于n维维数组,不填axis默认axis=-1
- print('\nb = \n', b)
- print('\nnp.sort(b) = \n', np.sort(b))
- print('\nnp.sort(b,axis=1) = \n', np.sort(b, axis=1))
- print('\nnp.sort(b,axis=0) = \n', np.sort(b, axis=0))
- print('\nnp.argsort(b) = \n', np.argsort(b))
- print('\nnp.argsort(b,axis=1) = \n', np.argsort(b, axis=1))
- print('\nnp.argsort(b,axis=0) = \n', np.argsort(b, axis=0))
结果:
a = [0 4 0 3 9 3 4 4 6 8]
升序np.sort(a) = [0 0 3 3 4 4 4 6 8 9]
降序= [9 8 6 4 4 4 3 3 0 0]
np.argsort(a) = [0 2 3 5 1 6 7 8 9 4]
np.argsort(a) = [4 9 8 1 6 7 3 5 0 2]b =
[[ 3 1 30 45]
[41 2 34 1]
[ 3 42 56 34]]np.sort(b) =
[[ 1 3 30 45]
[ 1 2 34 41]
[ 3 34 42 56]]np.sort(b,axis=1) =
[[ 1 3 30 45]
[ 1 2 34 41]
[ 3 34 42 56]]np.sort(b,axis=0) =
[[ 3 1 30 1]
[ 3 2 34 34]
[41 42 56 45]]np.argsort(b) =
[[1 0 2 3]
[3 1 2 0]
[0 3 1 2]]np.argsort(b,axis=1) =
[[1 0 2 3]
[3 1 2 0]
[0 3 1 2]]np.argsort(b,axis=0) =
[[0 0 0 1]
[2 1 1 2]
[1 2 2 0]]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。