http://blog.youkuaiyun.com/pipisorry/article/details/51822775
numpy排序、搜索和计数函数和方法。(重新整合过的)
排序Sorting
sort(a[, axis, kind, order]) | Return a sorted copy of an array. |
lexsort(keys[, axis]) | Perform an indirect sort using a sequence of keys. |
argsort(a[, axis, kind, order]) | Returns the indices that would sort an array. |
ndarray.sort([axis, kind, order]) | Sort an array, in-place. |
msort(a) | Return a copy of an array sorted along the first axis. |
sort_complex(a) | Sort a complex array using the real part first, then the imaginary part. |
partition(a, kth[, axis, kind, order]) | Return a partitioned copy of an array. |
argpartition(a, kth[, axis, kind, order]) | Perform an indirect partition along the given axis using the algorithm specified by the kind keyword. |
numpy多维数组排序
python列表排序
list.sort()一般用法:list.sort(axis = None, key=lambdax:x[1],reverse = True)
或者使用内置函数sorted():
sorted(data.tolist(), key=lambda x: x[split])
用ndarray.sort内建函数排序
数组的sort()方法用于对数组进行排序,它将改变数组的内容。
ndarray.sort()没有key参数,那怎么编写比较函数comparator?
示例
list1 = [[1, 3, 2], [3, 5, 4]]
array = numpy.array(list1)
array.sort(axis=1)
print(array)
[[1 2 3]
[3 4 5]]
sort内建函数是就地排序,会改变原有数组,不同于python中自带的sorted函数和numpy.sort通用函数,参数也不一样。
sort内建函数返回值为None,所以不能有这样的语法:array.sort(axis=1)[:5],这相当于是对None类型进行切片操作
矩阵按其第一列元素大小顺序来对整个矩阵进行行排序
mat1=mat1[mat1[:,0].argsort()]