Fundamentals 7 min read

Master Numpy: From Array Creation to Advanced Matrix Operations

This guide introduces NumPy fundamentals—including installation, array creation, indexing, slicing, universal functions, and linear‑algebra operations—providing concise code examples that demonstrate how to generate, manipulate, and compute with multidimensional arrays and matrices in Python.

Model Perspective
Model Perspective
Model Perspective
Master Numpy: From Array Creation to Advanced Matrix Operations

1. Numpy

In Python we commonly use the NumPy library for array creation and numerical operations. It can be installed via pip install numpy or is already available in Anaconda environments. The library is typically imported as np .

<code>import numpy as np</code>

2. Basic Data Types – array

NumPy’s fundamental data structure is the array , which is similar to Python’s list but more powerful.

2.1 Generating arrays

Arrays can be created from Python lists:

<code>>> np.array([1,2,3])
array([1, 2, 3])</code>

Multi‑dimensional arrays are also supported:

<code>>> np.array([[1,2,3],[4,5,6]])  # 2 rows, 3 columns
array([[1, 2, 3],
       [4, 5, 6]])</code>

2.2 Creating arithmetic sequences

Use linspace to generate a sequence with a specified number of elements, or arange to specify step size:

<code>>> np.linspace(1, 10, 5)
array([ 1.  ,  3.25,  5.5 ,  7.75, 10.  ])

>>> np.arange(1, 10, 2)
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</code>

The array can be reshaped, e.g., to 3 rows and 2 columns:

<code>>> a.reshape((3,2))
array([[1, 2],
       [3, 4],
       [5, 6]])</code>

2.4 Indexing

<code>>> a[1,2]          # element in second row, third column
6
>>> a[:,2]          # third column
array([3, 6])
>>> a[:,[1,2]]      # second and third columns
array([[2, 3],
       [5, 6]])</code>

2.5 Filtering

<code>>> a[a > 2]
array([3, 4, 5, 6])
>>> a[(a > 2) & (a < 6)]
array([3, 4, 5])</code>

3. Universal Functions (ufuncs)

NumPy’s universal functions operate element‑wise on arrays and are more efficient than Python’s built‑in math functions.

<code>>> import math, numpy as np
>>> math.log(20)
2.995732273553991
>>> np.log(20)
2.995732273553991
>>> math.log([1,2,3])   # raises TypeError
>>> np.log([1,2,3])
array([0.        , 0.69314718, 1.09861229])</code>

4. Matrix Generation and Operations

NumPy’s linalg module provides linear‑algebra utilities.

4.1 Common matrix creation

<code>>> np.eye(3)          # identity matrix
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])
>>> np.diag([1,2,3,4]) # diagonal matrix
array([[1, 0, 0, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])
>>> np.zeros((3,4))   # 3×4 zero matrix
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])</code>

4.2 Matrix arithmetic

<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
array([[4, 6, 3],
       [9, 3, 11],
       [4, 5, 4]])
>>> A - B   # subtraction
array([[-2, 0, 1],
       [-1,-1,-1],
       [ 2, 1,-2]])
>>> A @ B   # matrix multiplication
array([[20, 13, 25],
       [22, 24, 25],
       [25, 17, 24]])
>>> np.linalg.inv(A)   # inverse of A
array([[-0.37837838,  0.08108108,  0.35135135],
       [ 0.2972973 , -0.13513514,  0.08108108],
       [ 0.24324324,  0.16216216, -0.2972973 ]])</code>

4.3 Determinant, eigenvalues, eigenvectors

<code>>> np.linalg.det(A)
37.0
>>> values, vectors = np.linalg.eig(A)
>>> values   # eigenvalues
array([ 7.59286438, -1.66349894, -2.92936543])
>>> vectors  # eigenvectors
array([[-0.47899104, -0.80079204,  0.50689474],
       [-0.69615171,  0.4402157 , -0.82670395],
       [-0.53473393,  0.40613082,  0.2441686 ]])</code>
PythonarraymatrixNumPylinear algebraufunc
Model Perspective
Written by

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".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.