π·ββοΈ Implementation Notes
Last updated
Last updated
A scalar is considered to have rank zero ββ
Vectorization is used to speed up the Python (or Matlab) code without using loop. Using such a function can help in minimizing the running time of code efficiently. Various operations are being performed over vector such as dot product of vectors, outer products of vectors and element wise multiplication.
Faster execution (allows parallel operations) π¨βπ§
Simpler and more readable code :sparkles:
Finding the dot product of two arrays:
Taking the square root of each element in the array
np.sqrt(x)
Taking the sum over all of the array's elements
np.sum(x)
Taking the absolute value of each element in the array
np.abs(x)
Applying trigonometric functions on each element in the array
np.sin(x)
, np.cos(x)
, np.tan(x)
Applying logarithmic functions on each element in the array
np.log(x)
, np.log10(x)
, np.log2(x)
Applying arithmetic operations on corresponded elements in the arrays
np.add(x, y)
, np.subtract(x, y)
, np.divide(x, y)
, np.multiply(x, y)
Applying power operation on corresponded elements in the arrays
np.power(x, y)
Getting mean of an array
np.mean(x)
Getting median of an array
np.median(x)
Getting variance of an array
np.var(x)
Getting standart deviation of an array
np.std(x)
Getting maximum or minimum value of an array
np.max(x)
, np.min(x)
Getting index of maximum or minimum value of an array
np.argmax(x)
, np.argmin(x)
The term broadcasting describes how numpy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is βbroadcastβ across the larger array so that they have compatible shapes.
Practically:
If you have a matrix A that is (m,n)
and you want to add / subtract / multiply / divide with B matrix (1,n)
matrix then B matrix will be copied m
times into an (m,n)
matrix and then wanted operation will be applied
Similarly: If you have a matrix A that is (m,n)
and you want to add / subtract / multiply / divide with B matrix (m,1)
matrix then B matrix will be copied n
times into an (m,n)
matrix and then wanted operation will be applied
Long story short: Arrays (or matrices) with different sizes can not be added, subtracted, or generally be used in arithmetic. So it is a way to make it possible by stretching shapes so they have compatible shapes :sparkles:
It is recommended not to use rank 1 arrays
Rank 1 arrays may cause bugs that are difficult to find and fix, for example:
Dot operation on rank 1 arrays:
Dot operation on rank 2 arrays:
Conclusion: We have to avoid using rank 1 arrays in order to make our codes more bug-free and easy to debug π
Term
Description
π©βπ§ Vectorization
A way to speed up the Python code without using loop
β Broadcasting
Another technique to make Python code run faster by stretching arrays
π’ Rank of an Array
The number of dimensions it has
1οΈβ£ Rank 1 Array
An array that has only one dimension