开始使用免费开始使用

综合练习:宝可梦 z 分数

一份包含 720 个宝可梦的列表已作为 names 加载到您的会话中。每个宝可梦对应的生命值(Health Points,HP)存储在名为 hps 的 NumPy 数组中。您希望使用z 分数来分析生命值,查看每个宝可梦的 HP 相对于所有 HP 的均值相差多少个标准差。

下面的代码用于计算每个宝可梦的 HP z 分数,并根据 z 分数收集 HP 最高的宝可梦:

poke_zscores = []

for name,hp in zip(names, hps):
    hp_avg = hps.mean()
    hp_std = hps.std()
    z_score = (hp - hp_avg)/hp_std
    poke_zscores.append((name, hp, z_score))
highest_hp_pokemon = []

for name,hp,zscore in poke_zscores:
    if zscore > 2:
        highest_hp_pokemon.append((name, hp, zscore))

本练习是课程的一部分

高效编写 Python 代码

查看课程

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Calculate the total HP avg and total HP standard deviation
hp_avg = ____.____
hp_std = ____.____

# Use NumPy to eliminate the previous for loop
z_scores = (____ - ____)/____

# Combine names, hps, and z_scores
poke_zscores2 = [*____(names, hps, z_scores)]
print(*poke_zscores2[:3], sep='\n')
编辑并运行代码