numpy.sort() and numpy.ndarray.sort()函数

numpy.sort()Calling method:

numpy.sort(a, axis=-1, kind=None, order=None)

Meaning of each parameter:
aArray object to be sorted
axis: Choose which coordinate axis to sort according to. If set to a Nonevalue, the sorted array will be flattened first and then sorted. If you do not set the value, then the default value -1is to sort along the last coordinate axis.
kind: Four sort algorithms can be selected, such as { }, the default algorithm is order`: string or string list. If it is a string, it means sort the selected category. If it is a string list, it means to sort the first character category first, and then sort the second character category. There is a return value, which returns a copied array in order. code show as below: ‘quicksort’, ‘mergesort’, ‘heapsort’, ‘stable’‘quicksort’。

# -*- coding:utf-8 -*-
"""
author: 15025
time: 2020/12/30 20:27
software: PyCharm
"""
import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        array = np.array([[1, 3], [2, 4]])
        array_ = np.sort(array, axis=None)
        print("array_的值为:")
        print(array_)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
array_的值为:
[1 2 3 4]
"""

We can see that when we set axis=Noneit, we will first initialize the sorted array, and then perform the sorting operation, and finally we get a one-dimensional sorted array.

# -*- coding:utf-8 -*-
"""
author: 15025
time: 2020/12/30 20:27
software: PyCharm
"""
import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        array = np.array([[3, 1], [4, 2]])
        array_ = np.sort(array)
        array_1 = np.sort(array, axis=-1)
        print("array_的值为:")
        print(array_)
        print("array_1的值为:")
        print(array_1)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
array_的值为:
[[1 3]
 [2 4]]
array_1的值为:
[[1 3]
 [2 4]]
"""

We can see that when we do not set the axisparameters, the results obtained axis=-1are consistent with the set results.

# -*- coding:utf-8 -*-
"""
author: 15025
time: 2020/12/30 20:27
software: PyCharm
"""
import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        dtype = [('name', 'S10'), ('height', float), ('age', int)]
        values = [('Arthur', 1.8, 41), ('Lancelot', 1.9, 38),
                  ('Galahad', 1.7, 38)]
        # 创建一个结构数组
        array = np.array(values, dtype=dtype)
        array_ = np.sort(array, order='height')
        print("创建好的结构数组array的值为:")
        print(array)
        print("array_的值为:")
        print(array_)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
创建好的结构数组array的值为:
[(b'Arthur', 1.8, 41) (b'Lancelot', 1.9, 38) (b'Galahad', 1.7, 38)]
array_的值为:
[(b'Galahad', 1.7, 38) (b'Arthur', 1.8, 41) (b'Lancelot', 1.9, 38)]
"""

We can see that when we specify ordera string, it means sorting the objects represented by this string. That is, they heightare sorted by height .

Here is a way to arrange the elements of an array from large to small.

# -*- coding:utf-8 -*-
"""
author: 15025
time: 2020/12/30 20:27
software: PyCharm
"""
import numpy as np


class Debug:
    @staticmethod
    def mainProgram():
        array = np.array([[1, 3], [2, 4]])
        array_ = abs(np.sort(-array, axis=None))
        print("array_的值为:")
        print(array_)


if __name__ == "__main__":
    main = Debug()
    main.mainProgram()
"""
array_的值为:
[4 3 2 1]
"""

It can be seen from the above results that the array elements have been successfully arranged from large to small.

numpy.ndarray.sort()The function and the numpy.sort()parameters are exactly the same, but there is a difference in the calling method between the two, the numpy.ndarray.sort()function calling method is as follows:

ndarray.sort(axis=-1, kind=None, order=None)

E.g:

array = np.array([[1,4], [3,1]])
array.sort(axis=1)

The code word is not easy, if you find it useful, please raise your hand to give a like and let me recommend it for more people to see~

Guess you like

Origin blog.csdn.net/u011699626/article/details/112057536