Daily Data Science Puzzle
[python]
import numpy as np
# daily stock prices
# [open, close]
google = np.array(
[[1239, 1258], # day 1
[1262, 1248], # day 2
[1181, 1205]]) # day 3
# standard deviation
y = np.std(google, axis=1)
print(y[2] == max(y))
[/python]
What is the output of this puzzle?
*Advanced Level* (solution below)
Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices.
This puzzle introduces the standard deviation function of the numpy library. When applied to a 1D numpy array, this function returns its standard deviation. When applied to a 2D numpy array, numpy simply flattens the array. The result is the standard deviation of the flattened 1D array.
In the puzzle, we have a matrix with three rows and two columns. The matrix stores the open and close prices of the Google stock for three consecutive days. The first column specifies the opening price, the second the closing price.
We are interested in the standard deviation of the three days. How much does the stock price deviate from the mean between the opening and the closing price?
Numpy provides this functionality via the axis parameter. In a 2D matrix, the row is specified as axis=0 and the column as axis=1. We want to compute the standard deviation along the column, i.e., axis=1. This results in three standard deviation values – one per each day.
Clearly, on the third day, we have observed the highest standard deviation.
Are you a master coder?
Test your skills now!
Related Video
Solution
True
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.