Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dot product of lists 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 :: how to check a string in if statement python 
Python :: pandas sum 
Python :: python function arguments 
Python :: pk django 
Python :: python decimal 
Python :: how to create templates in python 
Python :: set() python 
Python :: how to import packages in python 
Python :: python csv to excel 
Python :: create anaconda env 
Python :: how to use variable from another function in python 
Python :: Interfaces 
Python :: Heroku gunicorn flask login is not working properly 
Python :: python click activator 
Python :: isenable selenium python 
Python :: python list of deeper paths 
Python :: how to count the iteration a list python 
Python :: reverse sublist of linklist 
Python :: python - notification messages 
Python :: analyser.polarity_scores get only compound 
Python :: iif python 
Python :: python moref id vsphere 
Python :: double digest fasta files 
Python :: numpy create array with infinities 
Python :: ploting bargraph with value_counts(with title x and y label and name angle) 
Python :: flask run function every minute 
Python :: converting 1d array into upper triangular 
Python :: Summarize text using LED huggingface 
Python :: Are angles of a parallelogram equal? 
Python :: formula e xiaomi 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =