Introduction to NumPy: Core Features, Array Creation, Operations, Indexing, and I/O
This article provides a comprehensive overview of NumPy, covering its high‑performance ndarray object, core functionalities such as broadcasting and vectorized operations, array creation and manipulation methods, mathematical and statistical functions, linear‑algebra utilities, random number generation, and input/output capabilities with practical code examples.
NumPy Overview
NumPy (Numerical Python) is one of the most important scientific‑computing libraries in Python, used for data analysis, scientific computing, and machine learning. Its core component is the high‑performance multidimensional array object ndarray, which supports efficient storage and operations.
Core Features of NumPy
High‑performance multidimensional array object ( ndarray)
Array operations including broadcasting and vectorized computation
Mathematical functions such as linear algebra, Fourier transforms, random number generation, etc.
Array Creation Functions
np.zeros((3, 4)) # array of zeros
np.ones((2, 2)) # array of ones
np.full((2, 3), 7) # array filled with a constant
np.eye(4) # identity matrix
np.arange(0, 10, 2) # similar to range
np.linspace(0, 1, 5) # evenly spaced numbersArray Indexing and Slicing
a = np.array([[1, 2, 3], [4, 5, 6]])
# element access
print(a[0, 1]) # 2
# column extraction
print(a[:, 1]) # [2 5]
# row extraction
print(a[1, :]) # [4 5 6]
# boolean indexing
print(a[a > 3]) # [4 5 6]Array Transformations and Combination
a.reshape(3, 2) # change shape
a.T # transpose
np.vstack([a, a]) # vertical stack
np.hstack([a, a]) # horizontal stack
np.split(a, 2) # split into sub‑arraysMathematical and Statistical Functions
np.sum(a) # sum of all elements
np.mean(a) # average value
np.std(a) # standard deviation
np.min(a), np.max(a) # min and max
np.argmin(a), np.argmax(a) # indices of min and maxLinear Algebra Module ( numpy.linalg )
from numpy.linalg import inv, eig, solve
A = np.array([[1, 2], [3, 4]])
print(inv(A)) # matrix inverse
print(eig(A)) # eigenvalues and eigenvectors
b = np.array([5, 6])
print(solve(A, b)) # solve Ax = bRandom Number Module ( numpy.random )
np.random.rand(3, 2) # uniform [0,1) numbers
np.random.randn(3) # standard normal distribution
np.random.randint(1, 10, 5) # random integers
np.random.choice([1,2,3], 4) # random selection from a list
np.random.seed(42) # set seed for reproducibilityInput/Output Operations
np.save('my_array.npy', a) # save as binary .npy file
np.load('my_array.npy') # load .npy file
np.savetxt('my_array.csv', a, delimiter=',') # save as CSV
np.loadtxt('my_array.csv', delimiter=',') # load CSV fileSummary Mind‑Map (Logical Structure)
NumPy
├── ndarray
│ ├── attributes (shape, dtype, ndim, size)
│ └── creation (array, zeros, ones, arange, linspace)
├── operations
│ ├── arithmetic (+, -, *, /, **, exp, sqrt)
│ └── broadcasting
├── indexing & slicing
│ ├── basic indexing
│ ├── boolean indexing
│ └── fancy indexing
├── array transformation
│ ├── reshape
│ ├── transpose
│ ├── concatenate & split
├── math & statistics
│ ├── sum, mean, std, min, max
│ └── argmin, argmax
├── linear algebra
│ └── inv, eig, solve, dot
├── random module
│ └── rand, randint, choice, seed
└── I/O
└── save, load, savetxt, loadtxtSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.
