1. Learn
  2. /
  3. 课程
  4. /
  5. 高效编写 Python 代码

Connected

道练习

综合练习:宝可梦 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))

说明 1 / 共 3 个

undefined XP
    1
    2
    3
  • 使用 NumPy 消除用于创建 z 分数的 for 循环。
  • 然后,将 names、hps 和 z_scores 合并为名为 poke_zscores2 的列表。