Introduction
This lab will cover some of the advanced features of NumPy, including linear algebra, random number generation, and masked arrays.
This lab will cover some of the advanced features of NumPy, including linear algebra, random number generation, and masked arrays.
NumPy has a comprehensive set of functions for linear algebra operations. Here are a few examples:
Open the Python shell by typing the following command in the terminal.
python3
The dot product of two arrays can be calculated using np.dot()
function. The dot product of two arrays A and B is defined as the sum of the product of corresponding elements of A and B.
import numpy as np
## create two arrays
a = np.array([1, 2])
b = np.array([3, 4])
## calculate dot product
dot_product = np.dot(a, b)
print(dot_product) ## Output: 11
Matrix multiplication can be performed using the @
operator or the np.matmul()
function.
Please read the following examples carefully.
## create two matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
## matrix multiplication
C = A @ B
print(C) ## Output: [[19 22], [43 50]]
You can also get the results in another way.
## create two matrices
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
## matrix multiplication
C = np.matmul(A,B)
print(C) ## Output: [[19 22], [43 50]]
The determinant and inverse of a matrix can be calculated using np.linalg.det()
and np.linalg.inv()
functions respectively.
## create a matrix
A = np.array([[1, 2], [3, 4]])
## calculate determinant and inverse
det_A = np.linalg.det(A)
inv_A = np.linalg.inv(A)
print(det_A) ## Output: -2.0
print(inv_A) ## Output: [[-2. 1. ], [ 1.5 -0.5]]
Now it's your turn to construct two arrays and use the np.dot()
function to calculate the dot product. Use @
or np.matmul()
to calculate matrix multiplication and use np.linalg.det()
and np.linalg.inv()
functions to calculate matrix determinant and inverse matrix.S
NumPy provides several functions to generate random numbers. Here are a few examples:
The np.random.rand()
function can be used to generate random numbers between 0 and 1.
## generate a 2x2 matrix of random numbers
a = np.random.rand(2, 2)
print(a) ## Output: [[0.43584547 0.37752558], [0.08936734 0.65526767]]
The np.random.randint()
function can be used to generate random integers between two specified numbers.
## generate an array of random integers between 1 and 10
a = np.random.randint(1, 10, size=(3, 3))
print(a) ## Output: [[8 7 3], [3 3 7], [8 8 7]]
The np.random.normal()
function can be used to generate numbers from a normal distribution.
## generate an array of numbers from a normal distribution
a = np.random.normal(0, 1, size=(2, 2))
print(a) ## Output: [[ 1.28418331 -0.90564647], [-0.76477896 1.69903421]]
Now please follow the above functions to complete the output of random number, random integer and normal distribution.Please complete this exercise.
Masked arrays are arrays that have a mask attached to them. The mask is an array of boolean values that indicate which elements of the array should be masked (hidden). NumPy provides the np.ma
module for working with masked arrays.
A masked array can be created using the np.ma.masked_array()
function.
## create an array with some values masked
a = np.ma.masked_array([1, 2, 3, 4], mask=[True, False, False, True])
print(a) ## Output: [-- 2 3 --]
A mask can be applied to an array using the np.ma.masked_where()
function.
## create an array
a = np.array([1, 2, 3, 4])
## create a mask
mask = a > 2
## apply the mask
b = np.ma.masked_where(mask, a)
print(b) ## Output: [1 2 -- --]
Masked arrays can be used to handle invalid values such as NaNs (not a number) or infinities.
## create an array with some invalid values
a = np.array([1, np.nan, np.inf, 4])
## create a masked array
b = np.ma.masked_invalid(a)
print(b) ## Output: [1.0 -- -- s4.0]
Now, please use thenp.ma
module provided by numoy to complete the creation of mask array. At the same time, use the np.ma.masked_where()
function to apply the mask to the array, and finally use the np.ma.masked_invalid()
to handle invalid values. Please complete this exercise.
Congratulations on completing this experiment๏ผ
In this tutorial, we have covered some of the advanced topics in NumPy, including linear algebra, random number generation, and masked arrays. These features are useful for many applications, including data analysis and scientific computing.
Please continue to work hard!