import numpy as np
- The plus sign (+) used with a list works for concatenation. The plus sign (+) with numpy array works for vector addition.
np.array([1,2,3])+[2,3,4]=array([3,5,7])
- Ways to do a dot product, i.e, multiply each element of an array with corresponding element of the other array and then adding up the results
a = [2,3]
b = [3,2]
c = 0
WAYS:
1.
for d, e in zip(a, b):
c += d*e
2.
c = sum(a*b)
3.
c = np.sum(a, b)
4.
c = a.sum(b)
5.
c = b.sum(a)
6.
c = (a*b).sum()
7.
c = np.dot(a, b)
8.
c = a.dot(b)
9.
c = b.dot(a)
- Finding magnitude, cos angle, and angle
mag_b = np.linalg.norm(b)
cos_angle = np.dot(a, b) / (mag_a * mag_b)
angle = np.arccos(cos_angle)
Comments
Post a Comment