python dictionary dot product
dot_product = sum(dict_1[key]*dict_2.get(key, 0) for key in dict_1)
python dictionary dot product
dot_product = sum(dict_1[key]*dict_2.get(key, 0) for key in dict_1)
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
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))])
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us