您现在可以解释一下吗? Python Numpy.arrays 使用常规列表
Can you explaing now this works. Python Numpy.arrays working with regular lists
positions = ['GK', 'M', 'A', 'D', ...]
heights = [191, 184, 185, 180, ...]
列表中的每个元素对应一个玩家。第一个列表 positions 包含代表每个玩家位置的字符串。可能的位置是:'GK'
(守门员)、'M'
(中场)、'A'
(进攻)和 'D'
(防守)。第二个列表 heights
包含代表球员身高的整数(以厘米为单位)。
1) 首先我将位置和高度转换为 numpy 数组:
np_heights = np.array(heights)
np_positions = np.array(positions)
然后我找出守门员的身高:gk_heights
gk_heights = np_heights[np_positions =='GK']
这段代码工作正常并且做了它应该做的事情。
现在是问题。
不是 np_heights
和 np_positions
两个单独的列表。这条线如何:
gk_heights = np_heights[np_positions =='GK']
知道 np_positions 中的某个索引与 np_heights
中的某个索引相关联吗?
这是Data Camp在线课程的最后一个问题:
URL:
https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=16
如有任何帮助,我们将不胜感激。
这个问题的答案与表达式 np_positions=='GK'
的含义有关。您应该仅在这一行尝试 运行 以查看输出是什么。本质上,该行的输出是一个与 np_positions
形状相同的布尔数组,布尔数组的元素是满足条件的 True
和不满足条件的 False
.所以,你应该得到一个像 np_positions=='GK' = array([True,False,False,False...])
这样的数组。然后,当您将此数组用作 np_heights
的索引切片时,表达式 np_heights[np_positions=='GK']
告诉您“挑选出 np_heights 的值,其中内部数组 np_positions=='GK'
,是 True
。因此,它将挑选出 np_heights
的第一个元素。希望这个解释是有道理的。
从这个解释中应该了解到,你的两个数组更好处于相同的顺序。这两个数组不会就哪个玩家被放入哪个索引相互通信。表达式 np_heights[np_positions=='GK']
用人类的话来说:“np_positions
中的哪里是守门员?哦,它在索引 0(或其他?)中 - return 索引 0 np_height
.
的值
因此,如果 goal keeper 是一个数组的第一个元素,它最好是第二个数组的第一个元素(依此类推)以使该表达式正确计算。
positions = ['GK', 'M', 'A', 'D', ...]
heights = [191, 184, 185, 180, ...]
列表中的每个元素对应一个玩家。第一个列表 positions 包含代表每个玩家位置的字符串。可能的位置是:'GK'
(守门员)、'M'
(中场)、'A'
(进攻)和 'D'
(防守)。第二个列表 heights
包含代表球员身高的整数(以厘米为单位)。
1) 首先我将位置和高度转换为 numpy 数组:
np_heights = np.array(heights)
np_positions = np.array(positions)
然后我找出守门员的身高:gk_heights
gk_heights = np_heights[np_positions =='GK']
这段代码工作正常并且做了它应该做的事情。
现在是问题。
不是 np_heights
和 np_positions
两个单独的列表。这条线如何:
gk_heights = np_heights[np_positions =='GK']
知道 np_positions 中的某个索引与 np_heights
中的某个索引相关联吗?
这是Data Camp在线课程的最后一个问题: URL: https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=16
如有任何帮助,我们将不胜感激。
这个问题的答案与表达式 np_positions=='GK'
的含义有关。您应该仅在这一行尝试 运行 以查看输出是什么。本质上,该行的输出是一个与 np_positions
形状相同的布尔数组,布尔数组的元素是满足条件的 True
和不满足条件的 False
.所以,你应该得到一个像 np_positions=='GK' = array([True,False,False,False...])
这样的数组。然后,当您将此数组用作 np_heights
的索引切片时,表达式 np_heights[np_positions=='GK']
告诉您“挑选出 np_heights 的值,其中内部数组 np_positions=='GK'
,是 True
。因此,它将挑选出 np_heights
的第一个元素。希望这个解释是有道理的。
从这个解释中应该了解到,你的两个数组更好处于相同的顺序。这两个数组不会就哪个玩家被放入哪个索引相互通信。表达式 np_heights[np_positions=='GK']
用人类的话来说:“np_positions
中的哪里是守门员?哦,它在索引 0(或其他?)中 - return 索引 0 np_height
.
因此,如果 goal keeper 是一个数组的第一个元素,它最好是第二个数组的第一个元素(依此类推)以使该表达式正确计算。