Bringing it all together: Pokémon z-scores
A list of 720 Pokémon has been loaded into your session as names
. Each Pokémon's corresponding Health Points is stored in a NumPy array called hps
. You want to analyze the Health Points using the z-score to see how many standard deviations each Pokémon's HP is from the mean of all HPs.
The below code was written to calculate the HP z-score for each Pokémon and gather the Pokémon with the highest HPs based on their z-scores:
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))
This exercise is part of the course
Writing Efficient Python Code
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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')