Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices. An important application of arrays, matrices, and vectors is the dot product. This article will teach you everything you need to know to get started!

The dot product behaves differently for different input arrays.

Table of Contents

## Dot Product 1D array and Scalar

import numpy as np # 1D array & scalar a = np.array([1, 2, 3]) res = np.dot(a, 10) print(res) # [10 20 30]

## Dot Product Two 1D arrays

# 1D array & 1D array a = np.array([1, 2, 3]) b = np.array([-1, -2, -3]) res = np.dot(a, b) print(res) # -14

## Dot Product 1D and 2D arrays

# 1D array & 2D array a = np.array([1, -1]) b = np.array([[2, 2, 2], [1, 1, 1]]) res = np.dot(a, b) print(res) # [1 1 1]

## Dot Product Two 2D arrays

# 2D array & 2D array a = np.array([[2, 2], [1, 1]]) b = np.array([[-1, -1], [1, 1]]) res = np.dot(a, b) print(res) # [[0 0] # [0 0]]

## NumPy Puzzle: How to Use the Dot Product for Linear Regression

Puzzles are a great way to improve your skills—and their fun, too! The following puzzle asks about a relevant application of the dot product: ** linear regression** in machine learning. Can you solve it?

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

** Exercise**:

*What is the output of this puzzle?*

You can solve it interactively on our Finxter puzzle-based learning app:

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

**Do you want to become a NumPy master?** Check out our interactive puzzle book **Coffee Break NumPy** and boost your data science skills! *(Amazon link opens in new tab.)*

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.