Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dot product of two vectors python

import numpy as np

# input: [[1,2,3,...], [4,5,6,...], ...]
def dot_product(vector, print_time= True):
    if print_time:
        print("----Dot Product----")
    dot_product = []
    for j in range(len(vector[0])):
        col = []
        for i in range(len(vector)):
            col.append(vector[i][j])
        prod_col = np.prod(col)
        dot_product.append(prod_col)
    sum_dot_product = np.sum(dot_product)
    
    if print_time:
        print(f"input vector: {vector}, => dot product = {sum_dot_product}")
        print("================================")
    return sum_dot_product
  
vector1 = [1,2,3]
vector2 = [4,5,6]
vector3 = [2,4,3]
vector4 = [2,4,3]
vector = [vector1, vector2, vector3, vector4]
  
dot_product(vector)
# or
dot_product([vector2, vector4])
# or
# the False parameter, disables the printing in the function.
print(dot_product(vector,False))
Comment

PREVIOUS NEXT
Code Example
Python :: github downloader 
Python :: sub function python 
Python :: python size of list 
Python :: python exit if statement 
Python :: minmax python 
Python :: templates python 
Python :: what is the ternary operator in python 
Python :: /n in python 
Python :: Bellman-Ford 
Python :: class object 
Python :: python 3 string length 
Python :: add python to path windows 10 
Python :: heroku procfile 
Python :: recall calculate confusion matrix .ravel() 
Python :: deactivate pandas warning copy 
Python :: python list of possible paths 
Python :: dataframe python diplay 2 tables on same line 
Python :: Missing Data Plotly Express px.data 
Python :: printing a varible with a varible. python 
Python :: Paraphrasing text with transformers library 
Python :: csv utf-8 to iso-8859-1 python 
Python :: asyncio run in executor 
Python :: flask example 
Python :: python source script custom functions 
Python :: converting from series to dataframe with tabulate 
Python :: python exception vs error 
Python :: cython could not creat pyd file no such file or directory 
Python :: django query column 
Python :: append in dictionary with matrix values 
Python :: python ask for real values until negative value diplay highest and lowest 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =