其他分享
首页 > 其他分享> > numpy.argsort(), numpy.argmax(), numpy.argmin()用法

numpy.argsort(), numpy.argmax(), numpy.argmin()用法

作者:互联网

numpy.argsort(), numpy.argmax(), numpy.argmin()用法

功能:将矩阵a按照axis排序,并返回排序后的索引

参数:a为输入矩阵,axis为需要排序的维度,axis=0按列排序,axis=1按行排序

返回值:排序后的索引

# 一维向量
import numpy as np
a = np.array([1, 2, 3])
b = np.argsort(a)
print(b)
>> Out: [0 2 1]

# 二维向量,axis为默认值
import numpy as np
a = np.array([[1, 3, 2],[5, 7, 6]])
b = np.argsort(a)
print(b)
>> Out: [[0 2 1]
         [0 2 1]]

# 二维向量,axis为0
import numpy as np
a = np.array([[1, 3, 2],[5, 7, 6]])
b = np.argsort(a, axis=0)
print(b)
>> Out: [[0 0 0]
         [1 1 1]]

功能:找到指定axis最大值,并返回最大值的索引

参数:a为输入矩阵,axis为寻找最大值的维度,axis=0按列寻找,axis=1按行寻找

返回值:最大值的索引

# 一维向量
import numpy as np
a = np.array([1, 2, 3])
b = np.argmax(a)
print(b)
>> Out: 2

# 二维向量,axis为默认值
import numpy as np
a = np.array([[1, 3, 2],[5, 7, 6]])
b = np.argmax(a)
print(b)
>> Out: 4

# 二维向量,axis为0
import numpy as np
a = np.array([[1, 3, 2],[5, 7, 6]])
b = np.argmax(a, axis=0)
print(b)
>> Out: [1 1 1]
    
# 二维向量,axis为1
import numpy as np
a = np.array([[1, 3, 2],[5, 7, 6]])
b = np.argmax(a, axis=1)
print(b)
>> Out: [1 1]
    
# 三维向量,axis为默认值
import numpy as np
a = np.array([[[1, 3, 2],[5, 7, 6]], [[4, 8, 6],[5, 7, 9]]])
b = np.argmax(a)
print(b)
>> Out: 11
    
# 三维向量,axis为0
import numpy as np
a = np.array([[[1, 3, 2],[5, 7, 6]], [[4, 8, 6],[5, 7, 9]]])
b = np.argmax(a, axis=0)
print(b)
>> Out: [[1 1 1]
         [0 0 1]]

# 三维向量,axis为1
import numpy as np
a = np.array([[[1, 3, 2],[5, 7, 6]], [[4, 8, 6],[5, 7, 9]]])
b = np.argmax(a, axis=1)
print(b)
>> Out: [[1 1 1]
         [1 0 1]]

功能:找到指定axis最小值,并返回最小值的索引

参数:a为输入矩阵,axis为寻找最小值的维度,axis=0按列寻找,axis=1按行寻找

返回值:最小值的索引

标签:argmin,argmax,print,np,import,array,numpy,axis
来源: https://blog.csdn.net/qq_38392644/article/details/104834025