NumPy Advanced Topics

NumPyNumPyBeginner
Practice Now

Introduction

This lab will cover some of the advanced features of NumPy, including linear algebra, random number generation, and masked arrays.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) numpy(("`NumPy`")) -.-> numpy/MathandStatisticsGroup(["`Math and Statistics`"]) numpy(("`NumPy`")) -.-> numpy/SpecialTechniquesGroup(["`Special Techniques`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") numpy/MathandStatisticsGroup -.-> numpy/lin_alg("`Linear Algebra`") numpy/MathandStatisticsGroup -.-> numpy/rand_num("`Random Numbers`") numpy/SpecialTechniquesGroup -.-> numpy/mask_array("`Masked Arrays`") subgraph Lab Skills python/comments -.-> lab-11{{"`NumPy Advanced Topics`"}} python/with_statement -.-> lab-11{{"`NumPy Advanced Topics`"}} python/booleans -.-> lab-11{{"`NumPy Advanced Topics`"}} python/lists -.-> lab-11{{"`NumPy Advanced Topics`"}} python/tuples -.-> lab-11{{"`NumPy Advanced Topics`"}} python/importing_modules -.-> lab-11{{"`NumPy Advanced Topics`"}} python/standard_libraries -.-> lab-11{{"`NumPy Advanced Topics`"}} python/math_random -.-> lab-11{{"`NumPy Advanced Topics`"}} python/numerical_computing -.-> lab-11{{"`NumPy Advanced Topics`"}} python/build_in_functions -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/1d_array -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/multi_array -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/data_array -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/bool_idx -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/fancy_idx -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/lin_alg -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/rand_num -.-> lab-11{{"`NumPy Advanced Topics`"}} numpy/mask_array -.-> lab-11{{"`NumPy Advanced Topics`"}} end

Linear Algebra with NumPy

NumPy has a comprehensive set of functions for linear algebra operations. Here are a few examples:

Open the Python Shell

Open the Python shell by typing the following command in the terminal.

python3

Dot Product

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

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]]

Determinant and Inverse

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]]

Exercise

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

Random Number Generation

NumPy provides several functions to generate random numbers. Here are a few examples:

Generating Random Numbers

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]]

Generating Random Integers

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]]

Generating Normal Distribution

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]]

Exercise

Now please follow the above functions to complete the output of random number, random integer and normal distribution.Please complete this exercise.

Masked Arrays

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.

Creating a Masked Array

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 --]

Applying a Mask

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 -- --]

Masking Invalid Values

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]

Exercise

Now, please use thenp.mamodule 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.

Summary

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!

Other NumPy Tutorials you may like