Analyzing Stock Data with Numpy – The Reshape and Average Functions

Daily Data Science Puzzle

import numpy as np

# apple stock prices (May 2018)
prices = [ 189, 186, 186, 188,
           187, 188, 188, 186,
           188, 188, 187, 186 ]
prices = np.array(prices)

data_3day = prices.reshape(4,3)

print(int(np.average(data_3day[0])))
print(int(np.average(data_3day[-1])))

What is the output of this puzzle?

Numpy is a popular Python library for data science focusing on linear algebra. This puzzle performs a miniature stock analysis of the Apple stock.

First, we create a numpy array from the raw price data.

Second, we create a new array data_3day for more convenient analysis. This array bundles the price data from three days into each row. We examine some rows in more detail later.

Third, we average the 3-day price data of the first and last row using the numpy average() function. Doing this results in data points that are more robust against outliers. Comparing the first and the last 3-day price period reveals that the Apple stock price remains stable in our mini data set.


Are you a master coder?
Test your skills now!

Related Video

Solution

187
187

 

Leave a Comment