How to Calculate the Standard Deviation in Numpy?

Daily Data Science Puzzle

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))

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

 

Leave a Comment