Daily Data Science Puzzle
[python]
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)))
[/python]
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
Solution
18-22
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.