排序函数(sort、sorted、argsort、lexsort、partition、argpartition、searchsorted)

HiveSQL题——排序函数(row_number/rank/dense_rank) 阅读详情

list.sort

通过对提供的数组进行就地修改来返回已排序的数组。因此,元素数组将被修改。

numpy.sort(ndarray.sort与之类似)

使用numpy.sort函数可以对数组进行排序,并返回排序好的数组。

  • 语法

    numpy.sort(a, aixs=-1, kind=None, order=None)
    
  • 参数

    numpy.sort(a, axis=1, kind='quicksort', order=None)
    Parameters:	a : array_like
    				Array to be sorted.
    			axis : int or None, optional
    				Axis along which to sort. If None, the array is flattened 
    				before sorting. The default is 1, which sorts along the last
    				 axis.
    			kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional
    				Sorting algorithm. Default is ‘quicksort’.
    			order : str or list of str, optional
    				When a is an array with fields defined, this argument
    				specifies which fields to compare first, second, etc. A 
    				single field can be specified as a string, and not all 
    				fields need be specified, but unspecified fields will still 
    				be used, in the order in which they come up in the dtype, to 
    				break ties.
    Returns:	sorted_array : ndarray
    			Array of the same type and shape as a.
    
    
    • a 要排序的数组
    • axis 按什么轴进行排序,默认按最后一个轴进行排序。
    • kind 排序方法,‘quicksort’为快排,‘mergesort’为混排,‘heapsort’为堆排,默认是快速排序
    • order 当数组定义了字段属性时,可以按照某个属性进行排序。
  • 例子

    import numpy as np
    # 创建一个一维数组
    x1 = np.array([1,8,2,4])
    x1
    '''
    一维数组:
    array([1, 8, 2, 4])
    '''
    # 排序
    np.sort(x1)
    '''
    输出:
    array([1, 2, 4, 8])
    '''
    
    # 创建一个二维数组
    x2 = np.array([[1,8,2,4],[4,5,1,3]])
    x2
    '''
    二维数组:
    array([[1, 8, 2, 4],
           [4, 5, 1, 3]])
    '''
    # 默认按最后一个轴排序,这里按行排序
    np.sort(x2)
    '''
    输出:
    array([[1, 2, 4, 8],
           [1, 3, 4, 5]])
    '''
    # 轴设为0,即按列排序
    np.sort(x2,axis=0)
    '''
    输出:
    array([[1, 5, 1, 3],
           [4, 8, 2, 4]])
    '''
    

    在这里插入图片描述

    按照字段属性进行排序,需要用到order参数。

    import numpy as np
    # 这是一个名字、身高、年龄的数组
    # 先给各字段配置属性类型
    dtype = [('Name', 'S10'), ('Height', float), ('Age', int)]
    # 各字段值
    values = [('Li', 1.8, 41), ('Wang', 1.9, 38),('Duan', 1.7, 38)]
    # 创建数组
    a = np.array(values, dtype=dtype)
    a
    '''
    数组:
    array([(b'Li', 1.8, 41), (b'Wang', 1.9, 38), (b'Duan', 1.7, 38)],
          dtype=[('Name', 'S10'), ('Height', '<f8'), ('Age', '<i4')])
    '''
    # 按照属性Height进行排序,此时参数为字符串          
    np.sort(a, order='Height')     
    '''
    输出:
    array([(b'Duan', 1.7, 38), (b'Li', 1.8, 41), (b'Wang', 1.9, 38)],
          dtype=[('Name', 'S10'), ('Height', '<f8'), ('Age', '<i4')])
    '''
    # 先按照属性Age排序,如果Age相等,再按照Height排序,此时参数为列表     
    np.sort(a, order=['Age', 'Height']) 
    '''
    输出:
    array([(b'Duan', 1.7, 38), (b'Wang', 1.9, 38), (b'Li', 1.8, 41)],
          dtype=[('Name', 'S10'), ('Height', '<f8'), ('Age', '<i4')])
    '''
    

    在这里插入图片描述
    在这里插入图片描述

sorted

对所有可迭代的对象进行排序操作。返回排序后的数组,而不修改原始数组。

  • 语法

    sorted(iterable, cmp=None, key=None, reverse=False)
    
  • 参数说明

    • iterable 可迭代对象
    • cmp 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0.
    • key 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
    • reverse 排序规则,reverse=True降序,reverse=False升序(默认)。
  • 例子
    在这里插入图片描述

sort函数与sorted函数最大的区别是:

  • sort函数对已存在的列表进行操作,调用其没有返回值。
    在这里插入图片描述
  • 内置函数sorted方法返回一个新的list,不在原来的list上进行操作,调用其返回一个排好序的list。
    在这里插入图片描述

numpy.argsort

numpy中的argsort函数用于将数组排序后,返回数组元素从小到大依次排序的所有元素索引。返回排序数组的索引,而不是数组元素的索引。

  • 语法

    numpy.argsort(a, axis=-1, kind=None, order=None)
    
  • 参数

    numpy.argsort(a, axis=1, kind='quicksort', order=None)
    Parameters:	a : array_like
    				Array to sort.
    			axis : int or None, optional
    				Axis along which to sort. The default is 1 (the last axis).
    				If None, the flattened array is used.
    			kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional
    				Sorting algorithm.
    			order : str or list of str, optional
    				When a is an array with fields defined, this argument
    				specifies which fields to compare first, second, etc. A
    				single field can be specified as a string, and not all fields
    				need be specified, but unspecified fields will still be used,
    				in the order in which they come up in the dtype, to break
    				ties.
    Returns:	index_array : ndarray, int
    				Array of indices that sort a along the specified axis. If a
    				is one-dimensional, a[index_array] yields a sorted a.
    
    
    • a 要排序的数组
    • axis 数组排序时的基准,按什么轴进行排序,axis=0按行排列,axis=1按列排列,默认按最后一个轴进行排序
    • kind 排序方法,默认是快速排序
    • order 当数组定义了字段属性时,可以按照某个属性进行排序
  • 例子

    import numpy as np
    # 创建一维数组
    x = np.array([3, 1, 2])
    '''
    数组:
    array([3, 1, 2])
    '''
    # 获取排序后的索引
    np.argsort(x)
    '''
    输出:
    array([1, 2, 0], dtype=int64)
    '''
    
    # 创建二维数组
    x2 = np.array([[0, 3], [2, 2]])
    '''
    数组:
    array([[0, 3],
           [2, 2]])
    '''
    # 默认按照最后一个轴进行排序,即行排序
    # 获取排序后的索引
    np.argsort(x2)
    '''
    输出:
    array([[0, 1],
           [0, 1]], dtype=int64)
    '''
    

    在这里插入图片描述

    按字段属性进行排序,并获取索引。

    # 先给各字段配置属性类型
    dtype = [('name', str), ('age', int)]
    # 值
    values = [('Anna', 28), ('Bob', 27),('Brown',21)]
    # 创建数组
    x = np.array(values, dtype=dtype)
    x
    '''
    数组:
    array([('', 28), ('', 27), ('', 21)],
          dtype=[('name', '<U'), ('age', '<i4')])
    '''
    # 先按照属性age排序,如果age相等,再按照name排序
    np.argsort(x,order=['name','age'])
    '''
    输出:
    array([2, 1, 0], dtype=int64)
    '''
    

    在这里插入图片描述

    >>>list1=[4,2,5,7,3]
    >>>a=np.array(list1)
    >>>a
    array([4, 2, 5, 7, 3])
    >>>b=np.argsort(a)
    >>>b
    array([1, 4, 0, 2, 3], dtype=int64)
    # 列表b的元素表示的是原列表a中的元素的索引,5各元素的索引分别为0-4
    # 返回的结果可以这样解读:
    # 	b[0]=1,表示原列表a的最小元素的索引为1,即原列表a中的第2个元素为最小值
    #	b[1]=4,表示原列表a的第二小元素的索引为4,即原列表a中的第5个元素为第二小元素
    #	...
    #	b[4]=3,表示原列表a中的最大元素的索引为3,即原列表a中的第4个元素为最大值
    >>>list2=[[3, 2],[5, 7]]
    >>>c=np.array(list2)
    >>>c
    array([[3, 2],[5, 7]])
    >>>np.argsort(c, axis=1)
    array([[1, 0],[0, 1]],dtype=int64)
    # axis=1,表明按照行进行排序,即是对[3, 2]进行排序,所以得到索引为[1, 0],其他同理
    >>>np.argsort(c, axis=0)
    array([[0, 1],[0, 1]],dtype=int64)
    # axis=0,表明按照列进行排序,即是对[3, 5]进行排序,所以得到索引为[0, 1],其他同理
    >>> x = np.array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')])
    >>> x
    array([(1, 0), (0, 1)], dtype=[('x', '<i4'), ('y', '<i4')])
    >>> np.argsort(x, order=('x','y'))
    # 先按照x进行比较,再按照y进行比较,即是先比较1与0
    array([1, 0])
    >>> np.argsort(x, order=('y','x'))
    # 先按照y进行比较,再按照x进行比较,即是先比较0与1
    array([0, 1])
    
    a = np.array([[1,2,1,5],[5,3,6,9],[6,2,9,5]])
    #按照第1行对列进行排序
    a1 = a.T[a[0].argsort()].T
    #或者
    a2 = a[:, a[0].argsort()]
    #按照最后1行对列进行排序
    a3 = a.T[a[-1].argsort()].T
    #或者
    a4 = a[:, a[-1].argsort()]
    #按照第1列对行排序
    a5 = a[a[:, 0].argsort()] 
    #按照最后1列对行排序
    a6 = a[a[:, -1].argsort()] 
    

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

numpy.lexsort

numpy.lexsort函数用于按照多个条件(键)进行排序,返回排序后索引。使用键序列执行间接稳定排序,用于使用涉及多个数组的多个排序键进行排序。
给定多个排序键(可以将其解释为电子表格中的列),lexsort返回一个整数索引数组,该数组描述按多个列排序的顺序。序列中的最后一个键用于主排序顺序,倒数第二个键用于辅助排序顺序,依此类推。keys参数必须是可以转换为相同形状的数组的对象序列。如果为keys参数提供了2D数组,则将其行解释为排序键,并根据最后一行,倒数第二行等进行排序。

  • 语法

    numpy.lexsort(keys, axis=-1)
    
  • 参数

    • keys 序列或元组,要排序的不同的列
    • axis 沿指定轴进行排序
  • 例子

    import numpy as np
    # 英语成绩
    eng = [90,85,95,80]
    # 数学成绩
    math = [80,95,90,85]
    # 总成绩
    total = [170,170,185,165]
    # 排序,获取索引
    np.lexsort((eng,math,total))
    '''
    先按总成绩total进行排序,
    再按数学成绩math进行排序,
    最后按英语成绩进行排序。
    可以看到total里有两个170,
    这时候就按下一级math排序,
    最后获取排序后的索引
    输出:
    array([3, 0, 1, 2], dtype=int64)
    '''
    # 也可以直接传入数组
    score = np.array([[90,85,95,80],[80,95,90,85],[170,170,185,165]])
    np.lexsort(score)
    '''
    输出:
    array([3, 0, 1, 2], dtype=int64)
    '''
    

    在这里插入图片描述

    >>> a=[1,5,1,4,3,4,4]
    >>> b=[9,4,0,4,0,2,1]
    >>> np.lexsort((b,a))
    # b在前,a在后,即是先按照a的元素进行比较
    # 如a中的最小值为两个1,其索引分别为0,2,再计较b中相应索引上的值,即9,0
    # 对应的最小应是:1,0,而其对应的索引为2,所以排序后返回的结果第一个值为索引2
    # 下一个最小应是:1,9,而其对应的索引为0,所以排序后返回的结果第一个值为索引0
    # 以此类推...
    array([2, 0, 4, 6, 5, 3, 1], dtype=int64)
    >>> np.lexsort((a,b))
    # a在前,b在后,即是先按照b的元素进行比较
    # 如b中的最小值为两个0,其索引分别为0,4,再计较a中相应索引上的值,即1,3
    # 对应的最小应是:0,1,而其对应的索引为2,所以排序后返回的结果第一个值为索引2
    # 下一个最小应是:0,3,而其对应的索引为4,所以排序后返回的结果第一个值为索引4
    # 以此类推...
    array([2, 4, 6, 5, 3, 1, 0], dtype=int64)
    >>> c=[[1,5,1,4,3,4,4],[9,4,0,4,0,2,1]]
    >>> c
    [[1, 5, 1, 4, 3, 4, 4], [9, 4, 0, 4, 0, 2, 1]]
    >>> np.lexsort(c)
    # 此种情况与先b后a的情况一致
    array([2, 4, 6, 5, 3, 1, 0], dtype=int64)
    
    

实现对数组或列表按照某一行或列进行排序。按照一个键值序列提供一个间接稳定的排序。

lexsort(keys, axis=-1)
  • 利用lexsort对二维数组按照某一行排序
    lexsort的键参数是二维数组时,对行排序,排序的方式是先对最后一行排序,当有相同元素时,然后按照倒数第二行对应的元素排序,……依次类推。
    例如:a1返回的是a的最后一行按照从小到大排列后所对应的索引值,比如最后一行为[6,2,9,5],从小到大排列为:2,5,6,9;其对应的索引号为:1,3,0,2.
    如果对a按照第二行升序排列,当有相同项时按照第一行升序排序.

    import numpy as np
    a = np.array([[1,2,1,5],[5,3,6,9],[6,2,9,5]])
    a.shape
    a
    #对a按照最后一行排序
    a1 = np.lexsort(a)
    a1
    a.T[a1].T
    #或者
    a[:, a1]
    #对a按照最后一行排序2
    a4 = np.lexsort(a[-1, None])
    a4
    a.T[a4].T
    #对a按照第二行升序排列,当有相同项时按照第一行升序排序
    a2 = np.lexsort(a[0:2, :])
    a2
    a.T[a2].T
    #或者
    a[:, a2]
    #对a按照第一行排序
    a3 = np.lexsort(a[::-1, :])
    a3
    a.T[a3].T
    #或者
    a[:, a3]
    #对a按照第一行排序2
    a5 = np.lexsort(a[0, None])
    a5
    a.T[a5].T
    

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

  • 利用lexsort对二维数组按照某一列排序
    只需将数组矩阵做转置,将列变成行,行变成列就可以了。例如:对a矩阵按照第二列升序排列,当遇到相同元素时按照第一列升序排列。首先对a矩阵装置,这样矩阵的列变成了行,然后在对行按照第二行升序的方式排列,都获取相应的序列编号。

    	a = np.array([[1,2,1,5],[5,3,6,9],[6,2,9,5]])
    	a.shape
    	a
    	#按最后一列顺序排序
    	a1 = np.lexsort(a.T)
    	a1
    	a[a1]
    	#或者
    	a[a1, :]
    	#按最后一列顺序排序2
    	a5 = np.lexsort(a.T[-1, None])
    	a5
    	a[a5]
    	#按最后一列逆序排序
    	a2 = np.lexsort(-a.T)
    	a2
    	a[a2]
    	#或者
    	a[a2, :]
    	#按第二列升序排序,当遇到相同元素按照第一列升序排列
    	a3 = np.lexsort(a.T[:2, :])
    	a3
    	a[a3]
    	#或者
    	a[a3, :]
    	#按照第一列排序
    	a4 = np.lexsort(a[:, ::-1].T)
    	a4
    	a[a4]
    	#或者
    	a[a4, :]
    	#按照第一列排序2
    	a6 = np.lexsort(a.T[0, None])
    	a6
    	a[a6]
    

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

numpy.searchsorted

给定升序数组以及待插入元素,返回保持序列有序的插入位置。

  • 语法

    numpy.searchsorted(a, v, side='left', sorted=None)
    
  • 参数

    numpy.searchsorted(a, v, side='left', sorter=None)
    Parameters:		a : 1-D array_like
    					Input array. If sorter is None, then it must be sorted in
    					ascending order, otherwise sorter must be an array of
    					indices that sort it.
    				v : array_like
    					Values to insert into a.
    				side : {‘left’, ‘right’}, optional
    					If ‘left’, the index of the first suitable location found
    					is given. If ‘right’, return the last such index. If 
    					there is no suitable index, return either 0 or N (where N 
    					is the length of a).
    				sorter : 1-D array_like, optional
    					Optional array of integer indices that sort array a into
    					ascending order. They are typically the result of 
    					argsort.
    Returns:		indices : array of ints
    					Array of insertion points with the same shape as v
    
    
    • a 所需排序的数组
    • v 待查询索引的元素值
    • side 查询索引时的方向,其中kind=left为从左至右,kind=right为从右至左
    • sorder 一个字符串或列表,可以设置按照某个属性进行排序
  • 例子

    >>> list3=[1,2,3,4,5]
    >>> np.searchsorted(list3,2)
    1
    # 如若要在list3中插入元素2,则应当将其插在原列表索引为1的地方,即是插在元素1的后面
    >>> np.searchsorted(list3,[-5,7,4,9])
    array([0, 5, 3, 5], dtype=int64)
    # 如若要在list3中插入元素-5,则应当将其插在原列表索引为0的地方,即是插在元素1的前面
    # 其他以此类推...
    

numpy.partition

返回数组沿指定轴分区/部分排序的副本。排序后的数组副本第k个位置的元素的值不小于其左边元素的值,不大于其右边元素的值。也就是说,返回的数组副本的第k个位置的元素位于正确的有序位置。
把所有元素中按大小排第k位的元素放在排序结果的第k位,比它小的放在前面,比它大的放在后面

  • 语法

    numpy.partition(a, kth, axis=-1, kind='introselect', order=None)
    
  • 参数

    numpy.partition(a, kth, axis=-1, kind='introselect', order=None)
    Parameters:		a : array_like
    					Array to be sorted.
    				kth : int or sequence of ints
    					Element index to partition by. The k-th value of the
    					element will be in its final sorted position and all 
    					smaller elements will be moved before it and all equal or 
    					greater elements behind it. The order all elements in the 
    					partitions is undefined. If provided with a sequence of 
    					k-th it will partition all elements indexed by k-th of 
    					them into their sorted position at once.
    				axis : int or None, optional
    					Axis along which to sort. If None, the array is flattened
    					before sorting. The default is -1, which sorts along the 
    					last axis.
    				kind : {‘introselect’}, optional
    					Selection algorithm. Default is ‘introselect’.
    				order : str or list of str, optional
    					When a is an array with fields defined, this argument
    					specifies which fields to compare first, second, etc. A 
    					single field can be specified as a string. Not all fields 
    					need be specified, but unspecified fields will still be 
    					used, in the order in which they come up in the dtype, to 
    					break ties.
    Returns:		partitioned_array : ndarray
    					Array of the same type and shape as a
    
    
    • a 待排序的数组或类似数组的对象
    • kth 第k个位置,可指定多个位置
    • axis 待排序的轴,axis=None,将所有元素排序,返回一维数组;axis为其它整数值,则沿着指定轴排序,默认值-1,沿最后一个轴排序
    • kind 排序方法
    • order 用于指定字段的结构化数组排序的参数
  • 例子

    >>>list=[3,4,5,2,1]
    >>>np.partition(list,3)
    array([2, 1, 3, 4, 5])
    # 以排序后的第3个数,即3进行分区,分区后的结果即是:
    # 小于3的元素2,1位于3的前面,大于等于3的元素4,5位于3的后面
    >>>a = np.array([[3, 4, 2, 1],[7,5,6,8]])
    >>>np.partition(a,kth=3,axis=1)# along the sencond axis (column)
    array([[2, 1, 3, 4],
           [6, 5, 7, 8]])
    >>>np.partition(a,kth=0,axis=0)# along the first axis (row)
    array([[3, 4, 2, 1],
           [7, 5, 6, 8]])
    

    在这里插入图片描述

    >>> a = np.array([ 8, 10,  3,  2, 14, 9,  7, 11,  0, 13, 6,  1,  5,  4, 12, 15])
    >>> np.partition(a, 4)# 第4个位置的元素位于正确有序元素,即元素4位于有序位置
    array([ 2,  0,  1,  3,  4,  5,  6, 10,  7,  8,  9, 11, 13, 14, 12, 15])
    >>> np.partition(a, (4,12))	# 第4和12个位置的元素位于正确有序位置
    array([ 2,  0,  1,  3,  4,  5,  6, 10,  7,  8,  9, 11, 12, 14, 13, 15])
    a = np.asarray([np.random.randint(0,20, 10) for _ in range(10)])
    >>> a = np.asarray([np.random.randint(0,10, 10) for _ in range(5)])
    >>> a
    array([[1, 0, 2, 2, 0, 1, 7, 3, 6, 9],
           [9, 8, 0, 2, 8, 2, 0, 2, 6, 9],
           [2, 6, 3, 5, 5, 7, 7, 3, 2, 2],
           [3, 2, 7, 8, 5, 1, 2, 1, 3, 1],
           [4, 0, 1, 4, 3, 2, 2, 3, 9, 7]])
    >>> np.partition(a, 2, axis=0)	# 对各列分区排序,结果第2行为分界线
    array([[1, 0, 0, 2, 0, 1, 0, 1, 2, 1],
           [2, 0, 1, 2, 3, 1, 2, 2, 3, 2],
           [3, 2, 2, 4, 5, 2, 2, 3, 6, 7],
           [9, 6, 7, 8, 5, 7, 7, 3, 6, 9],
           [4, 8, 3, 5, 8, 2, 7, 3, 9, 9]])
    >>> np.partition(a, 5, axis=1)		# 对各行分区排序,结果第5列为分界线
    array([[0, 0, 1, 1, 2, 2, 3, 6, 7, 9],
           [2, 2, 0, 0, 2, 6, 8, 8, 9, 9],
           [2, 2, 2, 3, 3, 5, 5, 6, 7, 7],
           [1, 1, 1, 2, 2, 3, 3, 8, 7, 5],
           [0, 1, 2, 2, 3, 3, 4, 4, 9, 7]])
    

    在这里插入图片描述

numpy.argpartition

返回数组沿指定轴分区/部分排序的索引数组。索引数组中位置k的索引对应的元素不小于位置k之前索引对应的元素,不大于位置k之后索引对应的元素。

  • 语法

    numpy.argpartition(a, kth, axis=-1, kind='introselect', order=None)
    
  • 参数

    • a待排序的数组或类似数组的对象索引
    • kth 第k个为在,可指定多个位置
    • axis 待排序的轴,axis=None,将所有元素排序,返回一维数组;axis为其它整数值,则沿着指定轴排序,默认值-1,沿最后一个轴排序
    • kind 排序方法
    • order 用于指定字段结构化数组排序的参数
  • 例子

    >>> a = np.array([ 8, 10,  3,  2, 14, 9,  7, 11,  0, 13, 6,  1,  5,  4, 12, 15])
    >>> np.argpartition(a, 4)
    array([ 3,  8, 11,  2, 13, 12, 10,  1,  6,  0,  5,  7,  9,  4, 14, 15],
          dtype=int64)
    >>> np.argpartition(a, (4,12))
    array([ 3,  8, 11,  2, 13, 12, 10,  1,  6,  0,  5,  7, 14,  4,  9, 15],
          dtype=int64)
    >>> a[np.argpartition(a, (4,12))]	# 等价于np.partition(a, (4,12))
    array([ 2,  0,  1,  3,  4,  5,  6, 10,  7,  8,  9, 11, 12, 14, 13, 15])
    

参考资料

1.sorted()、sort()函数
2.Python中的a.sort,sorted(a),np_argsort(a)和np.lexsort(b,a)
3.Numpy进阶之排序小技巧
4.numpy排序(sort、argsort、lexsort、partition、sorted)
5.Python——numpy排序(sort、argsort、lexsort、partition、sorted)
6.numpy sort/argsort, lexsort,sort_complex,partition
7.numpy中二维数组按照某列、某行排序
8.numpy排序(sort、argsort、lexsort、partition、sorted)
9.NumPy 排序函数
10.Numpy数组的排序与选择:sort, argsort, partition, argpartition, searchsorted, lexsort等
11.numpy排序(sort、argsort、lexsort、partition、sorted)
12.NumPy排序,搜索和计数功能
13.python基于numpy的多维数组排序

PyTorch 深度学习实战(28):对比学习(Contrastive Learning)与自监督表示学习 本文实现了基于对比学习的自监督表征学习框架,通过 InfoNCE 损失在 CIFAR-10 数据集上学习到具有判别性的特征表示。实验表明,仅使用线性分类器即可达到 82% 以上的验证准确率,证明了对比学习的有效性。在下一篇文章中,我们将深入计算机视觉的核心任务之一——《目标检测与 YOLO 实战》,探讨如何利用深度学习实现高效的目标检测系统。 阅读详情

相关推荐

ubuntu18.04美化主题(完整版)

和网上的美化一样,但是我当初跟着博客美化的时候遇到一些问题,按博客的做法无法解决。 所以我自己也写一篇关于ubuntu18.04美化主题的博客。 第一步:安装主题工具:GNOME Tweaks sudo apt-get update sudo apt-get install gnome-tweak-tool 然后安装完成后我们打开Tweaks(截图如下): 第二步,修改窗口按钮位...

B丶atty的博客 7万+

函数解析:argsort()函数

argsort()

m0_74121413的博客 7303

STM32串口接收避坑指南:为什么你的USART_IT_IDLE中断不触发?附完整调试流程

本文深入解析STM32串口接收中断机制,特别是USART_IT_IDLE中断不触发的常见原因及解决方案。通过详细分析中断标志清除顺序、波特率配置、硬件连接等关键因素,并提供完整的调试流程和代码示例,帮助开发者快速定位并解决串口通信问题。

weixin_42519985的博客 347

排序函数大集合

排序函数

Zhlyyds108的博客 3722

OTP/EEPROM 读取与处理

Camera OTP/EEPROM 读取与处理 文章目录Camera OTP/EEPROM 读取与处理1. OTP概念1.1 基本概念1.2 OTP存储类型1.3 OTP烧录信息类型1.4 OTP 作用2. 配置准备2.1 基本认识2.2 OTP/EEPROM配置3. OTP/EEPROM datasheet理解 以及 读取方式3.1 data sheet 内容3.2 一般OTP/EEPROM(e...

Understand-image 1万+

量化回测系统实战指南:从VectorBT到自研框架的深度解析

本文深入解析量化回测系统实战,从VectorBT和Backtesting.py的对比到自研框架miniQuant的设计与优化。探讨如何平衡速度与灵活性,解决实盘适配性问题,并提供性能调优技巧和可视化分析工具,帮助开发者构建高效可靠的量化交易系统。

weixin_28308325的博客 277

讨论各种排序(sort)函数,全网最全的排序函数总结

今天在这里谈论一些关于一些常见的排序的算法,这里我们由浅入深来谈论有趣的排序的相关算法。

jerryhe_com的博客 2156

简易背单词系统C/C++

#include&lt;stdio.h&gt; #include&lt;string.h&gt; int F=20,N=100,k=0; int score[100]; int count=0; void hello(); void add(); void preview(); void mdelete(); void etc(); void cte(); void mscore(); void...

青木年华 9154

四大排序函数

四大排序函数:ROW_NUMBER函数、RANK函数、DENSE_RANK函数、NTILE函数 1、ROW_NUMBER函数(1-2-3-4) row_number的用途的非常广泛,排序最好用他,它会为查询出来的每一行记录生成一个序号,依次排序且不会重复,注意使用row_number函数时必须要用over子句选择对某一列进行排序才能生成序号。 2、RANK函数(1-2-2-4) rank函...

xqt_161123的博客 6759

泄露测试仪CTS的Sentinel I28使用

本文介绍:泄露测试仪CTS的Sentinel I28使用

qq_34047402的博客 2205

配电网可靠性评估(一)—最小路法和非序贯蒙特卡洛模拟法的matlab实现

电力系统的可靠性研究是相关领域的热点问题。根据研究对象的不同,又可分为发电系统的可靠性,输电系统的可靠性和配电系统的可靠性。配电网在电力系统中处于最末端的位置,直接和用户相连,一旦出现故障情况,就会在用户侧表现为停电事故。因此对配电网的可靠性评估有着重大意义。......

配电网和matlab的博客 1万+

numpy排序(sort、argsort、lexsort、partition、sorted)

numpy排序(sort、argsort、lexsort、partition、sorted) </h1> <div class="clear"></div> <div class="post-categoty-tags"...

qq_38573437的博客 1302

《AI编程实战:深度学习模型改进与系统部署》,改进专栏目录和介绍

专深度学习相关项目都写在本专栏,《一次订阅解锁全部,都有源码,永久观看》,包括车牌识别、股票预测、人脸识别、风格迁移、图像修复、图像超分辨重建、图像分割等任务,Web/PyQt系统实现。引入Transformer、Swin Transformer、DenseNet、ViT、ConvNeXt等算法实战,提供卷积改进,魔改对比实验,模型剪枝,模型蒸馏,模型量化,全网独家整合改进专栏,为不同领域的同学发表论文提供了大力支持。

在职AI算法工程师,擅长计算机视觉,YOLO目标检测、分割等,擅长web、pyqt界面可视化,好内容持续更新中,来这里跟大家一起学习,共同进步 3269

Webots 外部机器人控制器(Extern Robot Controller)完整指南:从 IPC 本地调试到 TCP 远程控制

本文围绕 Webots 仿真器中的 `<extern>` 外部机器人控制器机制展开:当 [Robot](https://link.gitcode.com/i/df80c334eb8837d2a6d15de0d64046cc) 节点的 `controller` 字段被设为 `<extern>` 时,Webots 不再自动启动控制器进程,而是等待用户通过随安装包分发的 `webots-controll

gitblog_00687的博客 397

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

SQL中常用的四个排序函数,你用过几个?

ROW_NUMBER()是排序,当存在相同成绩的学生时,ROW_NUMBER()会依次进行排序,他们序号不相同,而Rank()则不一样。如果出现相同的,他们的排名是一样的。今天就给大家介绍四个你不怎么常用排序函数,他们就是SQL Server排序中经常用到的ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE()这四个好兄弟。RANK()是1-1-3-4-5,而ROW_NUMBER()则还是1-2-3-4-5,这就是RANK()和ROW_NUMBER()的区别了。

伤心的辣条 9926

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

SQL 中最常用的四个排序函数,工作面试都用得到

ROW_NUMBER()是排序,当存在相同成绩的学生时,ROW_NUMBER()会依次进行排序,他们序号不相同,而Rank()则不一样。RANK()是1-1-3-4-5,而ROW_NUMBER()则还是1-2-3-4-5,这就是RANK()和ROW_NUMBER()的区别了。今天就给大家介绍四个你不怎么常用排序函数,他们就是排序中经常用到的ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE()这四个好兄弟。上面是ROW_NUMBER()函数的结果,下面是RANK()函数的结果。

Python数据挖掘 2278

ORDER BY排序太简单?那是因为你还没用过这四大排序函数!

我们在写SQL代码时,只要有排序,首先想到的肯定是ORDER BY,以至于好多小伙伴觉得排序多简单啊。 今天就给大家介绍四个你不怎么常用排序函数,他们就是SQL Server排序中经常用到的ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE()这四个好兄弟。 我们先创建一个测试数据表Scores WITH t AS (SELECT 1 StuID,70 Scor...

SQL数据库开发 1182

Python基本函数:【numpy】np.argsort()函数详细解释运算过程

一、np.argsort()用法: np.argsort(a, axis=-1, kind='quicksort', order=None) 函数功能:将a中的元素从小到大排列,提取其在排列前对应的index(索引)输出。 二、举例: 1、一维数组 先定义一个array数组x,然后使用函数 import numpy as np x=np.array([1,4,3,-1,6,9]) y=np.argsort(x) print('一维数组的排序结果:{}'.format(y)) 输出结果如下: 一维数组的排

tianxinyiru的博客 9284

【Python】argsort()函数作用

argsort()函数作用: 按照升序(从小到大)进行快速排序,返回的是原数组的下标。 比如,x = array([30, 10, 20]),输入x.argsort() 升序排序后应该是[10,20,30],返回的下标排序是[1,2,0,] 【注】Python的第一位下标是0 转载于:https://www.cnblogs.com/...

weixin_30478757的博客 316

numpy 多级排序 :lexsort 函数详解

import numpy as np a = [[3, 3, 8], [6, 9, 7], [3, 3, 2]] b = np.array(a) """ step-1、首先按照第一列数值升序顺序对每行的位置进行重排, 第一行有两个值相同, 转到step-2 [[3, 3, 8], [[3, 3, 8], [6, 9, 7], --> [3, 3, 2], [3, 3, 2..

Flag_ing的博客 1万+

Python中argsort()、lexsort()排序的逻辑

Python中argsort()、lexsort()排序的用法

Mooc_WQ的博客 8335

SQL中常用的四个排序函数

我们在写SQL代码时,只要有排序,首先想到的肯定是ORDER BY,以至于好多小伙伴觉得排序多简单啊。今天就给大家介绍四个你不怎么常用排序函数,他们就是SQL Server排序中经常用到的ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE()这四个好兄弟。我们先创建一个测试数据表ScoresWITH t AS (SELECT 1 StuID,70...

SQL数据库开发 3768

数据结构与函数使用2:排序函数sort

简单的介绍一下sort函数是什么,如何自定义排序准则使用是什么,如何自定义排序准则使用

qq_35702489的博客 1799

万能的排序函数 (C语言)

有n个整数,需要对他们进行各种排序:升序排序、降序排序、按个位数升序排序、按十位数降序排序,为了提高代码的重用性,可以用指向函数的指针作排序函数的参数。 程序输入n+2个整数: 第一行是整数n,表示后面有n个整数需要排序 第二行是待排序的n个整数 第三行是排序需求,分别是1-4,依次表示升序排序、降序排序、按个位数升序排序、按十位数降序排序 程序的输出是排好序的n个整数,两个整数之间用空格隔开 函数接口定义: 需要实现以下函数: void sort(int *p, int (*compare)(in

ndzyh的博客 6149

Numpy中argsort()函数的用法

argsort()函数的作用是将数组按照从小到大的顺序排序,并按照对应的索引值输出。 argsort()函数中,当axis=0时,按列排列;当axis=1时,按行排列。如果省略默认按行排列。 下边通过例子来说明其用法: #!/usr/bin/env python # -*- coding:utf-8 -*- from numpy import * a = [3, 5, 1] b = ar...

中科院AI算法工程师的博客 1万+

python内的列表的argsort函数功能

代码如下 distances = sqDistances**0.5 print("距离和开方",distances) sortedDistIndices = distances.argsort() #argsort函数返回的是数组值从小到大的索引值 print("从小到大的距离",sortedDistIndices) 运行结果如下 距离和开方 [9.6 ...

qq_33374294的博客 2978
上一篇: Python中的None
下一篇: math.round(),math.ceil(),math.floor()区别
studyeboy
博客等级 码龄15年 365粉丝 329原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值