Comprehensive Guide to NumPy Array Creation, Operations, and Manipulation
This article provides an extensive tutorial on NumPy, covering array creation functions such as array, linspace, arange, and random generators, a wide range of array operations including reshaping, slicing, statistical calculations, set operations, splitting, stacking, comparison, repetition, and data persistence, all illustrated with clear Python code examples.
This guide introduces NumPy as one of the most useful tools in Python for handling large datasets and demonstrates the most important functions for data science.
Array Creation
Functions covered include numpy.array for creating one‑dimensional or multi‑dimensional arrays, numpy.linspace for generating evenly spaced floating‑point numbers, numpy.arange for integer ranges, numpy.random.uniform , numpy.random.randint , numpy.random.random , and numpy.logspace . Examples show how to import NumPy and create arrays with specific values.
Array Operations
Common operations such as np.min , np.max , np.unique , np.mean , np.median , np.digitize , np.reshape , np.expand_dims , np.squeeze , np.count_nonzero , np.argwhere , np.argmax , np.argmin , np.sort , np.abs , np.round , and np.clip are explained with code snippets and output.
Array Comparison
Functions np.allclose and np.equal are used to compare arrays element‑wise or within a tolerance.
Set Operations
Set‑like functions np.intersect1d , np.setdiff1d , np.setxor1d , and np.union1d return common, different, exclusive, or merged elements of two arrays.
Array Splitting and Stacking
Horizontal and vertical splitting with np.hsplit and np.vsplit , and stacking with np.hstack and np.vstack are demonstrated.
Advanced Operations
The Einstein summation convention is illustrated using np.einsum . Statistical functions such as np.histogram , np.percentile , np.std , and np.var compute histograms, percentiles, standard deviation, and variance.
Printing and Persistence
Printing options are customized with np.set_printoptions . Saving and loading arrays to/from text files are shown using np.savetxt and np.loadtxt .
Throughout the article, each code block is presented inside pre tags to preserve formatting.
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.