This chapter introduces NumPy, a powerful Python library for numerical computation. It covers core concepts such as arrays, indexing, operations, and statistical functions vital for data analysis and scientific computing.
NumPy stands for Numerical Python and is a fundamental package for scientific computing in Python.
pip install numpy.An Array is a data structure that can hold a fixed-size sequential collection of elements of the same type.
array = [10, 20, 30]
ndarray)import numpy as np
array1 = np.array([1, 2, 3])
np.array() function.np.zeros(shape) creates an array filled with zeros.np.ones(shape) creates an array filled with ones.np.arange(start, stop, step) creates arrays with a specified range and interval.ndarray.ndim: Returns number of dimensionsndarray.shape: Returns dimensions (rows, columns)ndarray.size: Total number of elementsndarray.dtype: Data type of elementsndarray.itemsize: Size in bytes of each elementIndexing allows you to access individual elements. NumPy supports:
array[i]array[i, j], where i is the row and j is the column.array[start:end] or array[start:end:step].array[start_row:end_row, start_col:end_col].@ operator).sort() rearranges array elements.array1 + array2 # element-wise addition
array1 * array2 # element-wise multiplication
array1 @ array2 # matrix multiplication
np.concatenate() or stacks such as np.vstack() and np.hstack() for vertical and horizontal combinations.array.reshape(new_shape) to change dimensions while keeping the same data.NumPy offers built-in functions for statistical computations:
np.sum(), np.mean(), np.max(), np.min(), np.std() calculate sum, mean, maximum, minimum, and standard deviation respectively.np.loadtxt() for loading plain text files.np.genfromtxt() for loading data with potential missing values.np.savetxt().In summary, NumPy is a core tool for efficient numerical computing with robust support for handling arrays, mathematical functions, and data processing features.