Python Numpy 101: How to Calculate Variance of Numpy Arrays?

Daily Data Science Puzzle

import numpy as np

# Goals in five matches
goals_croatia = np.array(
    [0,2,2,0,2])
goals_france = np.array(
    [1,0,1,1,0])

c = np.var(goals_croatia)
f = np.var(goals_france)
print(c>f)

What is the output of this puzzle?
*Intermediate Level* (solution below)

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices.

This puzzle introduces a new feature of the numpy library: the variance function. When applied to a 1D numpy array, this function returns the variance of the array values. The variance is the average squared deviation from the mean of the values in the array.

In the puzzle, the variance of the goals of the last five games of Croatia is 0.96 and of France is 0.24. But you do not need to know the exact values to see that the variance of goals shot by Croatia is larger.


Are you a master coder?
Test your skills now!

Related Video

Solution

True

 

Leave a Comment