How to Calculate the Standard Deviation of a Numpy Array?

Daily Data Science Puzzle

import numpy as np

temp_sensor = np.array(
[ 18, 22, 22, 18 ])

mean = np.mean(temp_sensor)
std = np.std(temp_sensor)

print(str(int(mean – std))
+ "-" +
str(int(mean + std)))

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

Numpy is a popular Python library for data science for array, vector, and matrix computations. This puzzle introduces the standard deviation function of the numpy library.

In the puzzle, we have four temperature values as measured by a temperature sensor. The goal is to determine the temperature interval in which 68.2 percent of the temperature values fall. By definition, these are all points that fall within one standard deviation around the mean. In other words, what is the range of normal temperature values based on our data?

The mean value of our data is 20. The standard deviation function calculates the squared distances from each data value to the mean and sums them together. In particular, the function performs the following computation: (i) It sums the squared distances to the mean with the result, i.e., 4+4+4+4=16. (ii) It normalizes the previous result by the number of data values, i.e., 16/4 =4. (iii) It calculates the root of the previous result, i.e., sqrt(4)=2. Thus, the resulting interval ranges from 20-2 to 20+2, i.e., 18-22.

Are you a master coder?
Test your skills now!

Related Video




Leave a Comment

Your email address will not be published. Required fields are marked *