Master Numpy: Create Arrays, Perform Operations, and Harness Linear Algebra
This guide introduces Python's Numpy library, covering installation, array creation, indexing, slicing, reshaping, arithmetic operations, universal functions, and linear algebra tools such as matrix generation, multiplication, inversion, determinants, eigenvalues, and eigenvectors, providing code examples for each concept.
1 Numpy
In Python we commonly use the Numpy library for array creation and function operations. It can be installed via the command line (cmd on Windows or Terminal on macOS) with:
<code>pip install numpy</code>If you are using Anaconda, the library is already installed. The library is usually imported as np :
<code>import numpy as np</code>2 Basic data type array
Numpy's basic data type is array , similar to Python's list but more powerful.
2.1 Generating array
Arrays can be created from a list:
<code>np.array([1,2,3])
# output: array(1,2,3)</code>Multi‑dimensional arrays can also be generated:
<code>np.array([[1,2,3],[4,5,6]]) # 2 rows, 3 columns
# output: array([[1, 2, 3],
# [4, 5, 6]])</code>2.2 Generating arithmetic sequences linspace and arange
Use linspace to create an evenly spaced sequence by specifying the number of elements:
<code>np.linspace(1,10,5)
# output: array([ 1. , 3.25, 5.5 , 7.75, 10. ])</code>Or use arange to specify the step size:
<code>np.arange(1,10,2)
# output: array([1, 3, 5, 7, 9])</code>2.3 Shape and size
<code>a = np.array([[1,2,3],[4,5,6]])
a.shape # (2, 3)
a.size # 6
a.reshape((3,2))
# output: array([[1,2],
# [3,4],
# [5,6]])</code>2.4 Indexing data
<code>a = np.array([[1,2,3],[4,5,6]])
a[1,2] # 6
a[:,2] # array([3,6])
a[:,[1,2]] # array([[2,3],
# [5,6]])</code>2.5 Filtering data
<code>a = np.array([[1,2,3],[4,5,6]])
a[a>2] # array([3,4,5,6])
a[(a>2) & (a<6)] # array([3,4,5])</code>3 Universal functions
Numpy's universal functions ( ufuncs ) operate on entire arrays, unlike Python's math module which works on scalars. Example:
<code>import math, numpy as np
math.log(20) # 2.995732...
np.log(20) # 2.995732...
np.log([1,2,3]) # array([0., 0.69314718, 1.09861229])</code>4 Matrix generation and operations
Numpy provides the linalg module for matrix calculations.
4.1 Common matrix generation
<code>np.eye(3) # identity matrix
np.diag([1,2,3,4]) # diagonal matrix
np.zeros((3,4)) # 3×4 zero matrix</code>4.2 Matrix addition, subtraction, multiplication, inversion
<code>A = np.array([[1,3,2],[4,1,5],[3,3,1]])
B = np.array([[3,3,1],[5,2,6],[1,2,3]])
A + B # addition
A - B # subtraction
A @ B # matrix multiplication
np.linalg.inv(A) # inverse of A</code>4.3 Determinant, eigenvalues, eigenvectors
<code>np.linalg.det(A) # determinant
values, vectors = np.linalg.eig(A) # eigenvalues and eigenvectors</code>Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.