Numpy Dot Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will explore the dot() function of the Numpy library, which is mainly used to calculate the dot product of two vectors. We will also see how this function can handle 2D arrays as matrices and perform matrix multiplication.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-86429{{"`Numpy Dot Function`"}} python/lists -.-> lab-86429{{"`Numpy Dot Function`"}} python/tuples -.-> lab-86429{{"`Numpy Dot Function`"}} python/importing_modules -.-> lab-86429{{"`Numpy Dot Function`"}} python/numerical_computing -.-> lab-86429{{"`Numpy Dot Function`"}} python/build_in_functions -.-> lab-86429{{"`Numpy Dot Function`"}} numpy/multi_array -.-> lab-86429{{"`Numpy Dot Function`"}} numpy/data_array -.-> lab-86429{{"`Numpy Dot Function`"}} numpy/bool_idx -.-> lab-86429{{"`Numpy Dot Function`"}} numpy/fancy_idx -.-> lab-86429{{"`Numpy Dot Function`"}} end

Understand the Syntax of numpy.dot()

The syntax required for using the dot() function is as follows:

numpy.dot(a, b, out=None)

Where:

  • a is the first parameter. If "a" is complex, then its complex conjugate is used for the calculation of the dot product.
  • b is the second parameter. If "b" is complex, then its complex conjugate is used for the calculation of the dot product.
  • out is the output argument. If it is not used, then it must have the exact kind that would be returned. Otherwise, it must be C-contiguous and its dtype must be the dtype that would be returned for dot(a, b).

Calculate the Dot Product of Scalars and 1D Arrays

In this step, we will use the dot() function to calculate the dot product of scalars and 1D arrays.

import numpy as np

## Calculate the dot product of scalar values
a = np.dot(8, 4)
print("The dot product of the above given scalar values is: ")
print(a)

## Calculate the dot product of two 1D arrays
vect_a = 4 + 3j
vect_b = 8 + 5j

dot_product = np.dot(vect_a, vect_b)
print("The dot product of two 1D arrays is: ")
print(dot_product)

Perform Matrix Multiplication with 2D Arrays

In this step, we will use the dot() function to perform matrix multiplication with 2D arrays.

import numpy as np

a = np.array([[50,100],[12,13]])
print("Matrix a is:")
print(a)

b = np.array([[10,20],[12,21]])
print("Matrix b is:")
print(b)

dot = np.dot(a, b)
print("The dot product of matrices a and b is:")
print(dot)

Error Handling

In this step, we will explore the ValueError that is raised when the last dimension of a is not the same size as the second-to-last dimension of b.

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([[7, 8], [9, 10], [11, 12], [13, 14]])

## Error handling
error = np.dot(a, b)
print(error)

Summary

In this lab, we covered the dot() function of the Numpy library. We learned how to use this function with its syntax, and the values returned by the function were explained with the help of code examples. We also explored the error handling of the function.

Other Python Tutorials you may like