Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找NumPy数组中最小值和最大值的索引_Python_Arrays_Numpy - Fatal编程技术网

Python 查找NumPy数组中最小值和最大值的索引

Python 查找NumPy数组中最小值和最大值的索引,python,arrays,numpy,Python,Arrays,Numpy,我正在进行一项小型数据分析任务,其中包括NBA球员数据集和几个属性,如身高、体重等。以下是数据集的一个小样本: players = ['Aaron Gordon', 'Aaron Holiday', 'Abdel Nader', 'Al Horford', 'Al-Farouq Aminu'] years_old = [23, 22, 25, 32, 28] height_inches = [81, 73, 78, 82, 81] weight_pounds = [220, 185, 225,

我正在进行一项小型数据分析任务,其中包括NBA球员数据集和几个属性,如身高、体重等。以下是数据集的一个小样本:

players = ['Aaron Gordon', 'Aaron Holiday', 'Abdel Nader', 'Al Horford', 'Al-Farouq Aminu']
years_old = [23, 22, 25, 32, 28]
height_inches = [81, 73, 78, 82, 81]
weight_pounds = [220, 185, 225, 245, 220]
我正在尝试创建一个程序,在这个数据集中查找最短和最高球员的索引。我已经将这些列表转换成NumPy数组,我能够成功地找到最小和最高的玩家,但我不确定如何获得这些特定玩家的索引

以下是我目前掌握的代码:

np_players = np.array(players)
np_years_old = np.array(years_old)
np_height_inches = np.array(height_inches)
np_weight_pounds = np.array(weight_pounds)

def shortest_player(np_h):
  mask = np.argmin(np_h)
  idx = np.where(mask)
  return idx

def tallest_player(np_h):
  mask = np.argmax(np_h)
  idx = np.where(mask)
  return idx
这是我在运行以下行时尝试测试这些功能时收到的错误<代码>打印(玩家[最短的玩家(np高度米)]):

TypeError回溯(最近一次调用上次) 在()9中返回idx 10->11 打印(球员[最高球员(np身高/米)])类型错误:列表 索引必须是整数或切片,而不是元组

例如,在提供的示例数据集中,函数理想情况下会为最短的玩家返回1,为最高的玩家返回3


有什么见解或建议吗

如果使用numpy数组,只需使用argmax/argmin函数即可:

min_h_idx = np.argmin(np_height_inches)
max_h_idx = np.argmax(np_height_inches)
重量是对称的

以下是您发布的值的简单输出:

>>> height_inches = [81, 73, 78, 82, 81]
>>> np_height_inches = np.array(height_inches)
>>> np.argmin(np_height_inches)
1
>>> np.argmax(np_height_inches)
3
注意

添加错误和尝试执行的行后,传递给函数的参数出现问题
np\u height\u meters
它不会出现在您添加的代码段中。 对于从
np_height_meters
更改为
np_height_inches
并仅使用
argmin/argmax
而不使用
where
功能后的输出:

def shortest_player(np_h):
  idx = np.argmin(np_h)
  return idx

def tallest_player(np_h):
  idx = np.argmax(np_h)
  return idx

print(players[ shortest_player(np_height_inches) ] )
我得到的输出将更改为上述值:

Aaron Holiday

如何调用这些函数,完整的错误消息是什么(包括堆栈跟踪)?
print(players[shortest\u player(np\u height\u meters)])
是我如何调用它的。完整的错误消息是:
---------------------------------------------------------------TypeError Traceback(最近一次调用)in()9返回idx 10-->11打印(玩家[最高玩家(np高度米)])TypeError:列表索引必须是整数或片,not tuple
@Aspire请查看我回答中的编辑当我尝试时,它返回实际最短和最高的球员姓名,而不是他们的索引。@Aspire您可以查看简单的输出,值是索引