NumPy – Linear Algebra:
Performing Linear Algebra on several matrices which are stacked as an array.
Below are the some of Linear Algebra functions we are going work on.
dot: This will return the dot product of two arrays.
Example:
>>> import numpy as np >>> np.dot(5,4) 20 >>> a=[[1,2],[3,4]] >>> b=[[1,1],[1,1]] >>> np.dot(a,b) array([[3, 3], [7, 7]])
vdot: This will return the dot product of two vectors.
Example:
>>> import numpy as np >>> np.dot(5,4) 20 >>> a=[[1,2],[3,4]] >>> b=[[1,1],[1,1]] >>> np.vdot(a,b) 10
This is how it works in the above example ==> 1*1+2*1+3*1+4*1
inner: This will return the inner product of two arrays.
Example:
>>> import numpy as np >>> a=[[1,2,3],[0,1,1]] >>> b=[[1,2,3],[0,0,1]] >>> np.inner(a,b) array([[14, 3], [ 5, 1]])
outer: This will return the outer product of two vectors.
Example:
>>> import numpy as np >>> a=[[1,2],[3,4]] >>> b=[[1,1],[1,1]] >>> np.outer(a,b) array([[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]])
matmul: This will return the matrix product two arrays.
Example 1:
>>> import numpy as np >>> a=[[1,2],[3,4]] >>> b=[[1,1],[1,1]] >>> np.matmul(a,b) array([[3, 3], [7, 7]])
Example 2:
>>> import numpy as np >>> a=[[1,2],[3,4]] >>> b=[1,1] >>> np.matmul(a,b) array([3, 7]) >>> np.matmul(b,a) array([4, 6])
tensordot: This will return the computed tensor dot product along specific aces for arrays>=1-D.
Example:
>>> import numpy as np >>> a=np.random.randint(2, size=(1,2,3)) >>> b=np.random.randint(2, size=(3,2,1)) >>> np.tensordot(a,b,axes=((1),(1))).shape (1, 3, 3, 1)
How it works:
we have choosen 1, 1 axes
In a ==> (1,2,3), b===> (3,2,1)===> ignore 2 now ===> (1,3,3,1)