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.
The function 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!
Related Video
Solution
186.5

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 that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a 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.