π·ββοΈ Implementation Notes
π Common Terms
A scalar is considered to have rank zero ββ
π© Vectorization
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.
β Advantages
Faster execution (allows parallel operations) π¨βπ§
Simpler and more readable code :sparkles:
π Simple Visualization
π©βπ» Code Examples
Finding the dot product of two arrays:
π Applying exponential operation on every element of an array (or matrix)
π Vectorized version of sigmoid function
π©βπ» Common Supported Operations in Numpy
π€ΈββοΈ Common single array functions
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)
π€ΈββοΈ Common multiple array functions
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)
β° Common sequential functions
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)
π Broadcasting
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:
π Simple Visualization
π©βπ» Code Examples:
β Adding a (1,n) row vector to a (2,n) matrix
β Subtracting 'a' scalar from a matrix
1οΈβ£ Rank 1 Array
π©βπ» Code Example
It is recommended not to use rank 1 arrays
π€ Why it is recommended not to use 1 rank 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 π
π§ References
Last updated