import numpy as np def makeTwoDim(x): if len(x.shape)==1: x = x.reshape(len(x) // 2, 2) return x # apple stock prices (May 2018) prices = [ 189, 186, 186, 188, 187, 188, 188, 186, 188, 188, 187, 186 ] data = np.array(prices) data = makeTwoDim(data) print(np.average(data[-1]))
Daily Data Science Puzzle
What is the output of this puzzle?
Numpy is a popular Python library for data science focusing on linear algebra.
This advanced puzzle combines multiple language concepts and numpy features. The topic is a miniature stock analysis of the Apple stock.
The puzzle creates a one-dimensional numpy array from raw price data. Note that the shape property of an n-dimensional numpy array is a tuple with n elements. Thus, a one-dimensional numpy array has a shape property of length one.
makeTwoDim(x) uses this to detect one-dimensional numpy arrays. Then, it transforms these into two-dimensional arrays using the reshape function. For each of the two dimensions, the reshape function needs the number of elements of this dimension. In particular, it creates a numpy array with two columns and half the number of rows.
Finally, we access the last row of this numpy array that contains the two numbers 187 and 186 and average them.
Are you a master coder?
Test your skills now!
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.