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
<code>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 numbers
</code>Array Indexing and Slicing
<code>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]
</code>Array Transformations and Combination
<code>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‑arrays
</code>Mathematical and Statistical Functions
<code>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 max
</code>Linear Algebra Module ( numpy.linalg )
<code>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 = b
</code>Random Number Module ( numpy.random )
<code>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 reproducibility
</code>Input/Output Operations
<code>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 file
</code>Summary Mind‑Map (Logical Structure)
<code>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, loadtxt
</code>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.