The Dot Product in Numpy

Daily Data Science Puzzle

import numpy as np

# simple regression model
W = np.array([0.7, 0.2, 0.1])

# Google stock prices (in US-$)
# [today, yesterday, 2 days ago]
x = np.array([1131, 1142, 1140])

# prediction
y = np.dot(W, x)

# do we expect growing prices?
if y > x[0]:
    print("buy")
else:
    print("sell")

What is the output of this puzzle?

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices.

This puzzle predicts the stock price of the Google stock. We use three-day historical data and store it in the numpy array x.

The numpy array W represents our prediction model. More precisely, W contains the weights for the three past days, i.e., how much each day contributes to the prediction. In machine learning, this array is called the weight vector.

We predict the stock price for tomorrow based on the stock prices of the most recent three days. But today’s stock price should have a higher impact on our prediction than yesterday’s stock price. Thus, we weigh today’s stock price with the factor 0.7.

In the puzzle, the stock prices of the last three days are $1132, $1142, and $1140. The predicted stock price for the next day is y = 0.7 * $1132 + 0.2 * $1142 + 0.1 * $1140 = $1134.8.

We implement this linear combination of the most recent three-days stock prices by using the dot product of the two vectors.

To get the result of the puzzle, you do not have to compute the result of the dot product. It is enough to see that the predicted stock price is higher than today’s stock price.


Are you a master coder?
Test your skills now!

Related Video

Solution

buy

 

Leave a Comment

Your email address will not be published. Required fields are marked *