Note : This note-book is crate for placement preparation. This note book is crate from multiple resource of from internet. The purpose of this note book is that it can me help me in my journey. It is only for educational purpose by me.¶
NumPy ufuncs¶
What are ufuncs?¶
- ufuncs stands for "Universal Functions" and they are NumPy functions that operate on the ndarray object.
Why use ufuncs?¶
ufuncs are used to implement vectorization in NumPy which is way faster than iterating over elements.
They also provide broadcasting and additional methods like reduce, accumulate etc. that are very helpful for computation.
ufuncs also take additional arguments, like:
where boolean array or condition defining where the operations should take place.
dtype defining the return type of elements.
out output array where the return value should be copied.
What is Vectorization?¶
Converting iterative statements into a vector based operation is called vectorization.
It is faster as modern CPUs are optimized for such operations.
Add the Elements of Two Lists
list 1: [1, 2, 3, 4]
list 2: [4, 5, 6, 7]
`
- One way of doing it is to iterate over both of the lists and then sum each elements.
#Without ufunc, we can use Python's built-in zip() method:
x = [1, 2, 3, 4]
y = [4, 5, 6, 7]
z = []
for i, j in zip(x, y):
z.append(i + j)
print(z)
[5, 7, 9, 11]
- NumPy has a ufunc for this, called add(x, y) that will produce the same result.
# With ufunc, we can use the add() function:
x = [1, 2, 3, 4]
y = [4, 5, 6, 7]
z = np.add(x, y)
print(z)
[ 5 7 9 11]
Create Your Own ufunc¶
How To Create Your Own ufunc¶
To create your own ufunc, you have to define a function, like you do with normal functions in Python, then you add it to your NumPy ufunc library with the frompyfunc() method.
The frompyfunc() method takes the following arguments:
- function - the name of the function.
- inputs - the number of input arguments (arrays).
- outputs - the number of output arrays.
# Create your own ufunc for addition:
def myadd(x, y):
return x+y
myadd = np.frompyfunc(myadd, 2, 1)
print(myadd([1, 2, 3, 4], [5, 6, 7, 8]))
[6 8 10 12]
Check if a Function is a ufunc¶
Check the type of a function to check if it is a ufunc or not.
A ufunc should return
<class 'numpy.ufunc'>.If it is not a ufunc, it will return another type, like this built-in NumPy function for joining two or more arrays
If the function is not recognized at all, it will return an error
#Check if a function is a ufunc:
print(type(np.add))
<class 'numpy.ufunc'>
# Check the type of another function: concatenate():
print(type(np.concatenate))
<class 'function'>
# Check the type of something that does not exist. This will produce an error:
#print(type(np.blahblah))
- To test if the function is a ufunc in an if statement, use the numpy.ufunc value (or np.ufunc if you use np as an alias for numpy):
# Use an if statement to check if the function is a ufunc or not:
if type(np.add) == np.ufunc:
print('add is ufunc')
else:
print('add is not ufunc')
add is ufunc
NumPy - Broadcasting¶
- The term broadcasting refers to the ability of NumPy to treat arrays of different shapes during arithmetic operations. Arithmetic operations on arrays are usually done on corresponding elements. If two arrays are of exactly the same shape, then these operations are smoothly performed.
import numpy as np
a = np.array([1,2,3,4])
b = np.array([10,20,30,40])
c = a * b
print(c)
[ 10 40 90 160]
If the dimensions of two arrays are dissimilar, element-to-element operations are not possible. However, operations on arrays of non-similar shapes is still possible in NumPy, because of the broadcasting capability. The smaller array is broadcast to the size of the larger array so that they have compatible shapes.
Broadcasting is possible if the following rules are satisfied :
- Array with smaller ndim than the other is prepended with '1' in its shape.
- Size in each dimension of the output shape is maximum of the input sizes in that dimension.
- An input can be used in calculation, if its size in a particular dimension matches the output size or its value is exactly 1.
- If an input has a dimension size of 1, the first data entry in that dimension is used for all calculations along that dimension.
A set of arrays is said to be broadcastable if the above rules produce a valid result and one of the following is true :
- Arrays have exactly the same shape.
- Arrays have the same number of dimensions and the length of each dimension is either a common length or 1.
- Array having too few dimensions can have its shape prepended with a dimension of length 1, so that the above stated property is true.
a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])
b = np.array([1.0,2.0,3.0])
print(a + b)
[[ 1. 2. 3.] [11. 12. 13.] [21. 22. 23.] [31. 32. 33.]]
- The following figure demonstrates how array b is broadcast to become compatible with a.
Simple Arithmetic¶
Simple Arithmetic¶
You could use arithmetic operators
+ - * /directly between NumPy arrays, but this section discusses an extension of the same where we have functions that can take any array-like objects e.g. lists, tuples etc. and perform arithmetic conditionally.Arithmetic Conditionally: means that we can define conditions where the arithmetic operation should happen.
All of the discussed arithmetic functions take a where parameter in which we can specify that condition.
Addition¶
- The add() function sums the content of two arrays, and return the results in a new array.
arr1 = np.array([10, 11, 12, 13, 14, 15])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.add(arr1, arr2)
print(newarr)
[30 32 34 36 38 40]
Subtraction¶
- The subtract() function subtracts the values from one array with the values from another array, and return the results in a new array.
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.subtract(arr1, arr2)
print(newarr)
[-10 -1 8 17 26 35]
Multiplication¶
- The multiply() function multiplies the values from one array with the values from another array, and return the results in a new array.
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([20, 21, 22, 23, 24, 25])
newarr = np.multiply(arr1, arr2)
print(newarr)
[ 200 420 660 920 1200 1500]
Division¶
- The divide() function divides the values from one array with the values from another array, and return the results in a new array.
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 10, 8, 2, 33])
newarr = np.divide(arr1, arr2)
print(newarr)
[ 3.33333333 4. 3. 5. 25. 1.81818182]
Power¶
- The power() function rises the values from the first array to the power of the values of the second array, and return the results in a new array.
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 5, 6, 8, 2, 33])
newarr = np.power(arr1, arr2)
print(newarr)
[ 1000 3200000 729000000 6553600000000 2500
0]
Remainder¶
Both the mod() and the remainder() functions return the remainder of the values in the first array corresponding to the values in the second array, and return the results in a new array.
This function returns the remainder of division of the corresponding elements in the input array. The function numpy.remainder() also produces the same result.
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.mod(arr1, arr2)
print(newarr)
[ 1 6 3 0 0 27]
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.remainder(arr1, arr2)
print(newarr)
[ 1 6 3 0 0 27]
Quotient and Mod¶
- The divmod() function return both the quotient and the the mod. The return value is two arrays, the first array contains the quotient and second array contains the mod.
arr1 = np.array([10, 20, 30, 40, 50, 60])
arr2 = np.array([3, 7, 9, 8, 2, 33])
newarr = np.divmod(arr1, arr2)
print(newarr)
(array([ 3, 2, 3, 5, 25, 1]), array([ 1, 6, 3, 0, 0, 27]))
Absolute Values¶
- Both the absolute() and the abs() functions do the same absolute operation element-wise but we should use absolute() to avoid confusion with python's inbuilt math.abs()
arr = np.array([-1, -2, 1, 2, 3, -4])
newarr = np.absolute(arr)
print(newarr)
[1 2 1 2 3 4]
numpy.reciprocal()¶
- This function returns the reciprocal of argument, element-wise. For elements with absolute values larger than 1, the result is always 0 because of the way in which Python handles integer division. For integer 0, an overflow warning is issued.
import numpy as np
a = np.array([0.25, 1.33, 1, 0, 100])
print(np.reciprocal(a))
b = np.array([100], dtype = int)
print(np.reciprocal(b))
[4. 0.7518797 1. inf 0.01 ] [0]
<ipython-input-54-f6e4f7029803>:3: RuntimeWarning: divide by zero encountered in reciprocal print(np.reciprocal(a))
- The following functions are used to perform operations on array with complex numbers.
- numpy.real() : returns the real part of the complex data type argument.
- numpy.imag() : returns the imaginary part of the complex data type argument.
- numpy.conj() : returns the complex conjugate, which is obtained by changing the sign of the imaginary part.
- numpy.angle() : returns the angle of the complex argument. The function has degree parameter. If true, the angle in the degree is returned, otherwise the angle is in radians.
import numpy as np
a = np.array([-5.6j, 0.2j, 11. , 1+1j])
print('Our array is:')
print(a)
print('\n')
print('Applying real() function:')
print(np.real(a))
print('\n')
print('Applying imag() function:')
print(np.imag(a))
print('\n')
print('Applying conj() function:')
print(np.conj(a))
print('\n')
print('Applying angle() function:')
print(np.angle(a))
print('\n')
print('Applying angle() function again (result in degrees)')
print(np.angle(a, deg = True))
Our array is: [-0.-5.6j 0.+0.2j 11.+0.j 1.+1.j ] Applying real() function: [-0. 0. 11. 1.] Applying imag() function: [-5.6 0.2 0. 1. ] Applying conj() function: [-0.+5.6j 0.-0.2j 11.-0.j 1.-1.j ] Applying angle() function: [-1.57079633 1.57079633 0. 0.78539816] Applying angle() function again (result in degrees) [-90. 90. 0. 45.]
Rounding Decimals¶
Rounding Decimals¶
- There are primarily five ways of rounding off decimals in NumPy:
➡ truncation
➡ fix
➡ rounding
➡ floor
➡ ceil
Truncation¶
- Remove the decimals, and return the float number closest to zero. Use the trunc() and fix() functions.
# Truncate elements of following array:
arr = np.trunc([-3.1666, 3.6667])
print(arr)
[-3. 3.]
#Same example, using fix():
arr = np.fix([-3.1666, 3.6667])
print(arr)
[-3. 3.]
Rounding¶
The around() function increments preceding digit or decimal by 1 if >=5 else do nothing.
E.g. round off to 1 decimal point, 3.16666 is 3.2
This is a function that returns the value rounded to the desired precision.
numpy.around(a,decimals)
| Parameter | Description |
|---|---|
| a | Input data |
| decimals | The number of decimals to round to. Default is 0. If negative, the integer is rounded to position to the left of the decimal point |
# Round off 3.1666 to 2 decimal places:
arr = np.around(3.1666, 2)
print(arr)
3.17
Floor¶
The floor() function rounds off decimal to nearest lower integer.
E.g. floor of 3.166 is 3.
This function returns the largest integer not greater than the input parameter. The floor of the scalar x is the largest integer i, such that i <= x. Note that in Python, flooring always is rounded away from 0.
# Floor the elements of following array:
arr = np.floor([-3.1666, 3.6667])
print(arr)
[-4. 3.]
Ceil¶
The ceil() function returns the ceiling of an input value, i.e. the ceil of the scalar x is the smallest integer i, such that i >= x.
The ceil() function rounds off decimal to nearest upper integer.
E.g. ceil of 3.166 is 4.
# Ceil the elements of following array:
arr = np.ceil([-3.1666, 3.6667])
print(arr)
[-3. 4.]
NumPy Logs¶
Logs¶
-NumPy provides functions to perform log at the base 2, e and 10.
We will also explore how we can take log for any base by creating a custom ufunc.
All of the log functions will place -inf or inf in the elements if the log can not be computed.
Log at Base 2¶
- Use the log2() function to perform log at the base 2.
- Note: The arange(1, 10) function returns an array with integers starting from 1 (included) to 10 (not included).
# Find log at base 2 of all elements of following array:
arr = np.arange(1, 10)
print(np.log2(arr))
[0. 1. 1.5849625 2. 2.32192809 2.5849625 2.80735492 3. 3.169925 ]
Log at Base 10¶
- Use the log10() function to perform log at the base 10.
# Find log at base 10 of all elements of following array:
arr = np.arange(1, 10)
print(np.log10(arr))
[0. 0.30103 0.47712125 0.60205999 0.69897 0.77815125 0.84509804 0.90308999 0.95424251]
Natural Log, or Log at Base e¶
- Use the log() function to perform log at the base e.
# Find log at base e of all elements of following array:
arr = np.arange(1, 10)
print(np.log(arr))
[0. 0.69314718 1.09861229 1.38629436 1.60943791 1.79175947 1.94591015 2.07944154 2.19722458]
Log at Any Base¶
- NumPy does not provide any function to take log at any base, so we can use the frompyfunc() function along with inbuilt function math.log() with two input parameters and one output parameter
from math import log
import numpy as np
nplog = np.frompyfunc(log, 2, 1)
print(nplog(100, 15))
1.7005483074552052
NumPy Summations¶
Summations¶
What is the difference between summation and addition?
Addition is done between two arguments whereas summation happens over n elements.
arr1 = np.array([1, 2, 3])
arr2 = np.array([1, 2, 3])
newarr = np.add(arr1, arr2)
print(newarr)
[2 4 6]
# Sum the values in arr1 and the values in arr2:
arr1 = np.array([1, 2, 3])
arr2 = np.array([1, 2, 3])
newarr = np.sum([arr1, arr2])
print(newarr)
12
Summation Over an Axis¶
- If you specify axis=1, NumPy will sum the numbers in each array.
# Perform summation in the following array over 1st axis:
arr1 = np.array([1, 2, 5])
arr2 = np.array([1, 2, 3])
newarr = np.sum([arr1, arr2], axis=1)
print(newarr)
[8 6]
Cummulative Sum¶
Cummulative sum means partially adding the elements in array.
E.g. The partial sum of [1, 2, 3, 4] would be [1, 1+2, 1+2+3, 1+2+3+4] = [1, 3, 6, 10].
Perfom partial sum with the cumsum() function.
# Perform cummulative summation in the following array:
arr = np.array([1, 2, 3])
newarr = np.cumsum(arr)
print(newarr)
[1 3 6]
NumPy Products¶
Products¶
- To find the product of the elements in an array, use the prod() function.
#Find the product of the elements of this array:
arr = np.array([1, 2, 3, 4])
x = np.prod(arr)
print(x)
24
# Find the product of the elements of two arrays:
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
x = np.prod([arr1, arr2])
print(x)
#Returns: 40320 because 1*2*3*4*5*6*7*8 = 40320
40320
Product Over an Axis¶
- If you specify axis=1, NumPy will return the product of each array.
# Perform summation in the following array over 1st axis
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])
newarr = np.prod([arr1, arr2], axis=1)
print(newarr)
[ 24 1680]
Cummulative Product¶
Cummulative product means taking the product partially.
E.g. The partial product of [1, 2, 3, 4] is [1, 12, 123, 1234] = [1, 2, 6, 24]
Perfom partial sum with the cumprod() function.
# Take cummulative product of all elements for following array:
arr = np.array([5, 6, 7, 8])
newarr = np.cumprod(arr)
print(newarr)
[ 5 30 210 1680]
NumPy Differences¶
Differences¶
A discrete difference means subtracting two successive elements.
E.g. for [1, 2, 3, 4], the discrete difference would be [2-1, 3-2, 4-3] = [1, 1, 1]
To find the discrete difference, use the diff() function.
# Compute discrete difference of the following array:
arr = np.array([10, 15, 25, 5])
newarr = np.diff(arr)
print(newarr)
[ 5 10 -20]
We can perform this operation repeatedly by giving parameter n.
E.g. for [1, 2, 3, 4], the discrete difference with n = 2 would be [2-1, 3-2, 4-3] = [1, 1, 1] , then, since n=2, we will do it once more, with the new result: [1-1, 1-1] = [0, 0]
#Compute discrete difference of the following array twice:
arr = np.array([10, 15, 25, 5])
newarr = np.diff(arr, n=2)
print(newarr)
[ 5 -30]
NumPy LCM Lowest Common Multiple¶
Finding LCM (Lowest Common Multiple)¶
- The Lowest Common Multiple is the smallest number that is a common multiple of two numbers.
# Find the LCM of the following two numbers:
num1 = 4
num2 = 6
x = np.lcm(num1, num2)
print(x)
12
Finding LCM in Arrays¶
To find the Lowest Common Multiple of all values in an array, you can use the reduce() method.
The reduce() method will use the ufunc, in this case the lcm() function, on each element, and reduce the array by one dimension.
# Find the LCM of the values of the following array:
arr = np.array([3, 6, 9])
x = np.lcm.reduce(arr)
print(x)
18
# Find the LCM of all values of an array where the array contains all integers from 1 to 10:
arr = np.arange(1, 11)
x = np.lcm.reduce(arr)
print(x)
2520
NumPy GCD Greatest Common Denominator¶
Finding GCD (Greatest Common Denominator)¶
- The GCD (Greatest Common Denominator), also known as HCF (Highest Common Factor) is the biggest number that is a common factor of both of the numbers.
# Find the HCF of the following two numbers:
num1 = 6
num2 = 9
x = np.gcd(num1, num2)
print(x)
3
Finding GCD in Arrays¶
- To find the Highest Common Factor of all values in an array, you can use the reduce() method.
The reduce() method will use the ufunc, in this case the gcd() function, on each element, and reduce the array by one dimension.
# Find the GCD for all of the numbers in the following array:
arr = np.array([20, 8, 32, 36, 16])
x = np.gcd.reduce(arr)
print(x)
4
NumPy Trigonometric Functions¶
Trigonometric Functions¶
- NumPy provides the ufuncs sin(), cos() and tan() that take values in radians and produce the corresponding sin, cos and tan values.
import numpy as np
#Find sine value of PI/2:
x = np.sin(np.pi/2)
print(x)
x = np.cos(np.pi/2)
print(x)
x = np.tan(np.pi/2)
print(x)
1.0 6.123233995736766e-17 1.633123935319537e+16
# Find sine values for all of the values in arr
arr = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5])
x = np.sin(arr)
print(x)
[1. 0.8660254 0.70710678 0.58778525]
Convert Degrees Into Radians¶
By default all of the trigonometric functions take radians as parameters but we can convert radians to degrees and vice versa as well in NumPy.
Note: radians values are pi/180 * degree_values.
# Convert all of the values in following array arr to radians:
arr = np.array([90, 180, 270, 360])
x = np.deg2rad(arr)
print(x)
[1.57079633 3.14159265 4.71238898 6.28318531]
Radians to Degrees¶
# Convert all of the values in following array arr to degrees:
arr = np.array([np.pi/2, np.pi, 1.5*np.pi, 2*np.pi])
x = np.rad2deg(arr)
print(x)
[ 90. 180. 270. 360.]
Finding Angles¶
Finding angles from values of sine, cos, tan. E.g. sin, cos and tan inverse (arcsin, arccos, arctan).
NumPy provides ufuncs arcsin(), arccos() and arctan() that produce radian values for corresponding sin, cos and tan values given.
# Find the angle of 1.0:
x = np.arcsin(1.0)
print(x)
1.5707963267948966
Angles of Each Value in Arrays¶
# Find the angle for all of the sine values in the array
arr = np.array([1, -1, 0.1])
x = np.arcsin(arr)
print(x)
[ 1.57079633 -1.57079633 0.10016742]
Hypotenues¶
Finding hypotenues using pythagoras theorem in NumPy.
NumPy provides the hypot() function that takes the base and perpendicular values and produces hypotenues based on pythagoras theorem.
# Find the hypotenues for 4 base and 3 perpendicular:
base = 3
perp = 4
x = np.hypot(base, perp)
print(x)
5.0
NumPy Hyperbolic Functions¶
Hyperbolic Functions¶
- NumPy provides the ufuncs sinh(), cosh() and tanh() that take values in radians and produce the corresponding sinh, cosh and tanh values..
# Find sinh value of PI/2:
x = np.sinh(np.pi/2)
print(x)
2.3012989023072947
# Find cosh values for all of the values in arr:
arr = np.array([np.pi/2, np.pi/3, np.pi/4, np.pi/5])
x = np.cosh(arr)
print(x)
[2.50917848 1.60028686 1.32460909 1.20397209]
Finding Angles¶
Finding angles from values of hyperbolic sine, cos, tan. E.g. sinh, cosh and tanh inverse (arcsinh, arccosh, arctanh).
Numpy provides ufuncs arcsinh(), arccosh() and arctanh() that produce radian values for corresponding sinh, cosh and tanh values given.
# Find the angle of 1.0:
x = np.arcsinh(1.0)
print(x)
0.881373587019543
Angles of Each Value in Arrays¶
# Find the angle for all of the tanh values in array:
arr = np.array([0.1, 0.2, 0.5])
x = np.arctanh(arr)
print(x)
[0.10033535 0.20273255 0.54930614]
NumPy Set Operations¶
What is a Set¶
A set in mathematics is a collection of unique elements.
Sets are used for operations involving frequent intersection, union and difference operations.
Create Sets in NumPy¶
- We can use NumPy's unique() method to find unique elements from any array. E.g. create a set array, but remember that the set arrays should only be 1-D arrays.
# Convert following array with repeated elements to a set:
arr = np.array([1, 1, 1, 2, 3, 4, 5, 5, 6, 7])
x = np.unique(arr)
print(x)
[1 2 3 4 5 6 7]
Finding Union¶
- To find the unique values of two arrays, use the union1d() method.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
newarr = np.union1d(arr1, arr2)
print(newarr)
[1 2 3 4 5 6]
Finding Intersection¶
To find only the values that are present in both arrays, use the intersect1d() method.
Note: the intersect1d() method takes an optional argument assume_unique, which if set to True can speed up computation. It should always be set to True when dealing with sets.
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([3, 4, 5, 6])
newarr = np.intersect1d(arr1, arr2, assume_unique=True)
print(newarr)
[3 4]
Finding Difference¶
To find only the values in the first set that is NOT present in the seconds set, use the setdiff1d() method.
Note: the setdiff1d() method takes an optional argument assume_unique, which if set to True can speed up computation. It should always be set to True when dealing with sets.
set1 = np.array([1, 2, 3, 4])
set2 = np.array([3, 4, 5, 6])
newarr = np.setdiff1d(set1, set2, assume_unique=True)
print(newarr)
[1 2]
Finding Symmetric Difference¶
To find only the values that are NOT present in BOTH sets, use the setxor1d() method.
Note: the setxor1d() method takes an optional argument assume_unique, which if set to True can speed up computation. It should always be set to True when dealing with sets.
set1 = np.array([1, 2, 3, 4])
set2 = np.array([3, 4, 5, 6])
newarr = np.setxor1d(set1, set2, assume_unique=True)
print(newarr)
[1 2 5 6]
NumPy - Statistical Functions¶
numpy.amin() and numpy.amax()¶
- These functions return the minimum and the maximum from the elements in the given array along the specified axis.
import numpy as np
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print('Applying amin() function:')
print(np.amin(a,1))
print('\n')
print('Applying amin() function again:')
print(np.amin(a,0))
print('\n')
print('Applying amax() function:')
print(np.amax(a))
print('\n')
print('Applying amax() function again:')
print(np.amax(a, axis = 0))
Applying amin() function: [3 3 2] Applying amin() function again: [2 4 3] Applying amax() function: 9 Applying amax() function again: [8 7 9]
numpy.ptp()¶
- The numpy.ptp() function returns the range (maximum-minimum) of values along an axis.
a = np.array([[3,7,5],[8,4,3],[2,4,9]])
print('Applying ptp() function:')
print(np.ptp(a))
print('\n')
print('Applying ptp() function along axis 1:')
print(np.ptp(a, axis = 1))
print('\n')
print('Applying ptp() function along axis 0:')
print(np.ptp(a, axis = 0))
Applying ptp() function: 7 Applying ptp() function along axis 1: [4 5 7] Applying ptp() function along axis 0: [6 3 6]
numpy.percentile()¶
- Percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall.
numpy.percentile(a, q, axis)
| Argument | Description |
|---|---|
| a | Input array |
| q | The percentile to compute must be between 0-100 |
| axis | The axis along which the percentile is to be calculated |
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
print('Applying percentile() function:')
print(np.percentile(a,50))
print('\n')
print('Applying percentile() function along axis 1:')
print(np.percentile(a,50, axis = 1))
print('\n')
print('Applying percentile() function along axis 0:')
print(np.percentile(a,50, axis = 0))
Applying percentile() function: 50.0 Applying percentile() function along axis 1: [40. 20. 60.] Applying percentile() function along axis 0: [50. 40. 60.]
numpy.median()¶
- Median is defined as the value separating the higher half of a data sample from the lower half. The numpy.median() function is used as shown in the following program.
a = np.array([[30,65,70],[80,95,10],[50,90,60]])
print('Applying median() function:')
print(np.median(a))
print('\n')
print('Applying median() function along axis 0:')
print(np.median(a, axis = 0))
print('\n')
print('Applying median() function along axis 1:')
print(np.median(a, axis = 1))
Applying median() function: 65.0 Applying median() function along axis 0: [50. 90. 60.] Applying median() function along axis 1: [65. 80. 60.]
numpy.mean()¶
- Arithmetic mean is the sum of elements along an axis divided by the number of elements. The numpy.mean() function returns the arithmetic mean of elements in the array. If the axis is mentioned, it is calculated along it.
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print('Applying mean() function:')
print(np.mean(a))
print('\n')
print('Applying mean() function along axis 0:')
print(np.mean(a, axis = 0))
print('\n')
print('Applying mean() function along axis 1:')
print(np.mean(a, axis = 1))
Applying mean() function: 3.6666666666666665 Applying mean() function along axis 0: [2.66666667 3.66666667 4.66666667] Applying mean() function along axis 1: [2. 4. 5.]
numpy.average()¶
Weighted average is an average resulting from the multiplication of each component by a factor reflecting its importance. The numpy.average() function computes the weighted average of elements in an array according to their respective weight given in another array. The function can have an axis parameter. If the axis is not specified, the array is flattened.
Considering an array [1,2,3,4] and corresponding weights [4,3,2,1], the weighted average is calculated by adding the product of the corresponding elements and dividing the sum by the sum of weights.
Weighted average = (1*4+2*3+3*2+4*1)/(4+3+2+1)
a = np.array([1,2,3,4])
print('Applying average() function:')
print(np.average(a))
print('\n')
# this is same as mean when weight is not specified
wts = np.array([4,3,2,1])
print('Applying average() function again:')
print(np.average(a,weights = wts))
print('\n')
# Returns the sum of weights, if the returned parameter is set to True.
print('Sum of weights')
print(np.average([1,2,3,4],weights = [4,3,2,1], returned = True))
Applying average() function: 2.5 Applying average() function again: 2.0 Sum of weights (2.0, 10.0)
- In a multi-dimensional array, the axis for computation can be specified.
a = np.arange(6).reshape(3,2)
print('Our array is:')
print(a)
print('\n')
print('Modified array:')
wt = np.array([3,5])
print(np.average(a, axis = 1, weights = wt))
print('\n')
print('Modified array:')
print(np.average(a, axis = 1, weights = wt, returned = True))
Our array is: [[0 1] [2 3] [4 5]] Modified array: [0.625 2.625 4.625] Modified array: (array([0.625, 2.625, 4.625]), array([8., 8., 8.]))
Standard Deviation¶
- Standard deviation is the square root of the average of squared deviations from mean.
std = sqrt(mean(abs(x - x.mean())**2))
- If the array is [1, 2, 3, 4], then its mean is 2.5. Hence the squared deviations are [2.25, 0.25, 0.25, 2.25] and the square root of its mean divided by 4, i.e., sqrt (5/4) is 1.1180339887498949.
print(np.std([1,2,3,4]))
1.118033988749895
Variance¶
- Variance is the average of squared deviations, i.e., mean(abs(x - x.mean())**2). In other words, the standard deviation is the square root of variance.
print(np.var([1,2,3,4]))
1.25
NumPy - Matrix Library¶
- NumPy package contains a Matrix library numpy.matlib. This module has functions that return matrices instead of ndarray objects.
matlib.empty()¶
- The matlib.empty() function returns a new matrix without initializing the entries.
numpy.matlib.empty(shape, dtype, order)
| Parameter | Description |
|---|---|
| shape | int or tuple of int defining the shape of the new matrix |
| Dtype | Optional. Data type of the output |
| order | C or F |
import numpy.matlib
import numpy as np
print(np.matlib.empty((2,2)))
# filled with random data
[[2.25 0.25] [0.25 2.25]]
numpy.matlib.zeros()¶
- This function returns the matrix filled with zeros.
import numpy.matlib
import numpy as np
print(np.matlib.zeros((2,2)))
[[0. 0.] [0. 0.]]
numpy.matlib.ones()¶
- This function returns the matrix filled with 1s.
import numpy.matlib
import numpy as np
print(np.matlib.ones((2,2)))
[[1. 1.] [1. 1.]]
numpy.matlib.eye()¶
- This function returns a matrix with 1 along the diagonal elements and the zeros elsewhere.
numpy.matlib.eye(n, M,k, dtype)
| Parameter | Description |
|---|---|
| n | The number of rows in the resulting matrix |
| M | The number of columns, defaults to n |
| k | Index of diagonal |
| dtype | Data type of the output |
import numpy.matlib
import numpy as np
print(np.matlib.eye(n = 3, M = 4, k = 0, dtype = float))
[[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.]]
numpy.matlib.identity()¶
- The numpy.matlib.identity() function returns the Identity matrix of the given size. An identity matrix is a square matrix with all diagonal elements as 1.
import numpy.matlib
import numpy as np
print(np.matlib.identity(5, dtype = float))
[[1. 0. 0. 0. 0.] [0. 1. 0. 0. 0.] [0. 0. 1. 0. 0.] [0. 0. 0. 1. 0.] [0. 0. 0. 0. 1.]]
numpy.matlib.rand()¶
The numpy.matlib.rand() function returns a matrix of the given size filled with random values.
Note that a matrix is always two-dimensional, whereas ndarray is an n-dimensional array. Both the objects are inter-convertible.
import numpy.matlib
import numpy as np
print(np.matlib.rand(3,3))
[[0.42999056 0.8496848 0.67381142] [0.51609161 0.84565648 0.56233514] [0.77386372 0.32308022 0.96126271]]
import numpy.matlib
import numpy as np
i = np.matrix('1,2;3,4')
print(i)
[[1 2] [3 4]]
j = np.asarray(i)
print(j)
[[1 2] [3 4]]
k = np.asmatrix (j)
print(k)
[[1 2] [3 4]]
NumPy - Linear Algebra¶
- NumPy package contains numpy.linalg module that provides all the functionality required for linear algebra.
| Function | Description |
|---|---|
| dot | Dot product of the two arrays |
| vdot | Dot product of the two vectors |
| inner | Inner product of the two arrays |
| matmul | Matrix product of the two arrays |
| determinant | Computes the determinant of the array |
| solve | Solves the linear matrix equation |
| inv | Finds the multiplicative inverse of the matrix |
numpy.dot()¶
- This function returns the dot product of two arrays. For 2-D vectors, it is the equivalent to matrix multiplication. For 1-D arrays, it is the inner product of the vectors. For N-dimensional arrays, it is a sum product over the last axis of a and the second-last axis of b.
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
np.dot(a,b)
# [[1*11+2*13, 1*12+2*14],[3*11+4*13, 3*12+4*14]]
array([[37, 40],
[85, 92]])
numpy.vdot()¶
- This function returns the dot product of the two vectors. If the first argument is complex, then its conjugate is used for calculation. If the argument id is multi-dimensional array, it is flattened.
a = np.array([[1,2],[3,4]])
b = np.array([[11,12],[13,14]])
print(np.vdot(a,b))
# Note − 1*11 + 2*12 + 3*13 + 4*14 = 130
130
numpy.inner()¶
- This function returns the inner product of vectors for 1-D arrays. For higher dimensions, it returns the sum product over the last axes.
print(np.inner(np.array([1,2,3]),np.array([0,1,0])))
# Equates to 1*0+2*1+3*0
2
# Multi-dimensional array example
a = np.array([[1,2], [3,4]])
b = np.array([[11, 12], [13, 14]])
print(np.inner(a,b))
#1*11+2*12, 1*13+2*14
#3*11+4*12, 3*13+4*14
[[35 41] [81 95]]
numpy.matmul()¶
The numpy.matmul() function returns the matrix product of two arrays. While it returns a normal product for 2-D arrays, if dimensions of either argument is >2, it is treated as a stack of matrices residing in the last two indexes and is broadcast accordingly.
On the other hand, if either argument is 1-D array, it is promoted to a matrix by appending a 1 to its dimension, which is removed after multiplication.
# For 2-D array, it is matrix multiplication
a = [[1,0],[0,1]]
b = [[4,1],[2,2]]
print(np.matmul(a,b))
[[4 1] [2 2]]
# 2-D mixed with 1-D
a = [[1,0],[0,1]]
b = [1,2]
print(np.matmul(a,b))
print(np.matmul(b,a))
[1 2] [1 2]
# one array having dimensions > 2
a = np.arange(8).reshape(2,2,2)
b = np.arange(4).reshape(2,2)
print(np.matmul(a,b))
[[[ 2 3] [ 6 11]] [[10 19] [14 27]]]
NumPy - Determinant¶
Determinant is a very useful value in linear algebra. It calculated from the diagonal elements of a square matrix. For a 2x2 matrix, it is simply the subtraction of the product of the top left and bottom right element from the product of other two.
In other words, for a matrix [[a,b], [c,d]], the determinant is computed as ‘ad-bc’. The larger square matrices are considered to be a combination of 2x2 matrices.
The numpy.linalg.det() function calculates the determinant of the input matrix.
a = np.array([[1,2], [3,4]])
print(np.linalg.det(a))
-2.0000000000000004
b = np.array([[6,1,1], [4, -2, 5], [2,8,7]])
print(np.linalg.det(b))
print(6*(-2*7 - 5*8) - 1*(4*7 - 5*2) + 1*(4*8 - -2*2))
-306.0 -306
numpy.linalg.solve()¶
The numpy.linalg.solve() function gives the solution of linear equations in the matrix form.
Considering the following linear equations −
x + y + z = 6
2y + 5z = -4
2x + 5y - z = 27
They can be represented in the matrix form as −
$$\begin{bmatrix}1 & 1 & 1 \\0 & 2 & 5 \\2 & 5 & -1\end{bmatrix} \begin{bmatrix}x \\y \\z \end{bmatrix} = \begin{bmatrix}6 \\-4 \\27 \end{bmatrix}$$If these three matrices are called A, X and B, the equation becomes −
A*X = B
Or
X = A-1B
numpy.linalg.inv()¶
- We use numpy.linalg.inv() function to calculate the inverse of a matrix. The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix.
x = np.array([[1,2],[3,4]])
y = np.linalg.inv(x)
print(x)
print(y)
print(np.dot(x,y))
[[1 2] [3 4]] [[-2. 1. ] [ 1.5 -0.5]] [[1.0000000e+00 0.0000000e+00] [8.8817842e-16 1.0000000e+00]]
- The same result can be obtained by using the function −
- x = np.dot(ainv,b)
a = np.array([[1,1,1],[0,2,5],[2,5,-1]])
ainv = np.linalg.inv(a)
print('Inverse of a:')
print(ainv)
print('Matrix B is:')
b = np.array([[6],[-4],[27]])
print(b)
print('Compute A-1B:')
x = np.linalg.solve(a,b)
print(x)
# this is the solution to linear equations x = 5, y = 3, z = -2
Inverse of a: [[ 1.28571429 -0.28571429 -0.14285714] [-0.47619048 0.14285714 0.23809524] [ 0.19047619 0.14285714 -0.0952381 ]] Matrix B is: [[ 6] [-4] [27]] Compute A-1B: [[ 5.] [ 3.] [-2.]]
