Recipe: How to calculate dot product of two vectors?
DATA MUNGING DATA CLEANING PYTHON

How to calculate dot product of two vectors?

This recipe helps you calculate dot product of two vectors
In [1]:
## How to calculate dot product of two vectors
def Kickstarter_Example_14():
    print()
    print(format('How to calculate dot product of two vectors','*^72'))

    # Load library
    import numpy as np

    # Create two vectors
    vectorA = np.array([1,2,3])
    vectorB = np.array([4,5,6])

    # Calculate Dot Product (Method 1)
    print(); print(np.dot(vectorA, vectorB))

    # Calculate Dot Product (Method 2)
    print(); print(vectorA @ vectorB)

Kickstarter_Example_14()
**************How to calculate dot product of two vectors***************

32

32
In [2]:
## How to calculate dot product of two matrices
def Kickstarter_Example_14_1():
    print()
    print(format('How to calculate dot product of two matrices','*^72'))

    # Load library
    import numpy as np

    # Create two vectors
    matrixA = np.array([[2, 3, 23],
                       [5, 6, 25],
                       [8, 9, 28]])
    matrixB = np.array([[1, 2, 3],
                       [4, 5, 6],
                       [7, 8, 9]])

    # Calculate Dot Product (Method 1)
    print(); print(np.dot(matrixA, matrixB))

    # Calculate Dot Product (Method 2)
    print(); print(matrixA @ matrixB)

Kickstarter_Example_14_1()
**************How to calculate dot product of two matrices**************

[[175 203 231]
 [204 240 276]
 [240 285 330]]

[[175 203 231]
 [204 240 276]
 [240 285 330]]
In [3]:
## How to describe a matrix
def Kickstarter_Example_14_2():
    print()
    print(format('How to describe a matrix','*^72'))

    # Load library
    import numpy as np

    # Create a matrix
    matrixA = np.array([[2, 3, 23],
                       [5, 6, 25],
                       [8, 9, 28]])

    # View number of rows and columns
    print(); print("Shape: ", matrixA.shape)

    # View number of elements (rows * columns)
    print(); print("Size: ", matrixA.size)

    # View number of dimensions
    print(); print("Dimention: ", matrixA.ndim)

Kickstarter_Example_14_2()
************************How to describe a matrix************************

Shape:  (3, 3)

Size:  9

Dimention:  2


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from KPMG
1 developer from YASH Technologies
1 developer from Altimetrik
1 developer from LTI
1 developer from ANAC
1 developer from MudraCircle
1 developer from HvH
1 developer from Vodafone