Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

dot product python

A = [1,2,3,4,5,6]
B = [2,2,2,2,2,2]

# with numpy
import numpy as np
np.dot(A,B) # 42
np.sum(np.multiply(A,B)) # 42
#Python 3.5 has an explicit operator @ for the dot product
np.array(A)@np.array(B)# 42
# without numpy
sum([A[i]*B[i] for i in range(len(B))]) # 42
Comment

dot product python

def dot_product(vector_a, vector_b):
	#base case
    #error message if the vectors are not of the same length
	if len(vector_a) != len(vector_b):
		return "ERROR: both input vectors must be of the same length"

    #multiply vector_a at position i with vector_b at position i
    #sum the vector made
    #return that vector
	return sum([vector_a[i] * vector_b[i] for i in range(len(vector_a))])
Comment

dot product 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 :: pyaudio install error ubuntu 
Python :: pyspark correlation 
Python :: rename a column in python 
Python :: how to change a thread name in python 
Python :: Get Key From value in dictionary 
Python :: finding the format of an image in cv2 
Python :: python timedelta 
Python :: python udp receive 
Python :: python print 
Python :: run file as administrator python 
Python :: pillow create image 
Python :: hot reloading flask 
Python :: median in python 
Python :: python lowercase 
Python :: Update label text after pressing a button in Tkinter 
Python :: timer pythongame 
Python :: How to perform insertion sort, in Python? 
Python :: dataframe change specicf values in column 
Python :: find nth root of m using python 
Python :: cprofile usage python 
Python :: TypeError: dict is not a sequence 
Python :: python pandas dataframe from csv index column 
Python :: python convert remove spaces from beginning of string 
Python :: define variable with if statement python 
Python :: pickle.loads in python 
Python :: punctuation list python 
Python :: python list remove spaces 
Python :: pygame.display.flip vs update 
Python :: pyplot bar plot colur each bar custom 
Python :: is prime in python 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =