Comprehensive Guide to Numpy Arrays: shape, Indexing, and Slicing
NumPy arrays are the foundation of Python data analysis, providing efficient multi-dimensional array objects with core operations including array creation, shape manipulation, indexing, and slicing. Creation methods: np.array() is commonly used to generate arrays from lists; zeros/ones create arrays filled with 0s/1s; arange generates sequences similar to Python's range. Shape is the dimension identifier of an array, viewed via .shape. The reshape() method adjusts dimensions (total elements must remain unchanged), with -1 indicating automatic dimension calculation. Indexing: 1D arrays behave like lists (0-based indexing with support for negative indices); 2D arrays use double indexing [i, j]. Slicing: Follows the syntax [start:end:step], with 1D/2D slicing producing subarrays. Slices return views by default (modifications affect the original array), requiring .copy() for independent copies. Mastering shape, indexing, and slicing is essential. Practical exercises are recommended to solidify these fundamental operations.
Read MoreLearning Data Structures from Scratch: What Exactly Is an Array?
An array is an ordered collection of data elements of the same type, accessed via indices (starting from 0), with elements stored contiguously. It is used to efficiently manage a large amount of homogeneous data. For example, class scores can be represented by the array `scores = [90, 85, 95, 78, 92]` instead of multiple individual variables, facilitating overall operations. In Python, array declaration and initialization can be done with `scores = [90, 85, 95, 78, 92]` or `[0] * 5` (declaring an array of length 5). Elements are accessed using `scores[index]`, and it's important to note the index range (0 to length-1), as out-of-bounds indices will cause errors. Basic operations include traversal with loops (`for score in scores: print(score)`), while insertion and deletion require shifting subsequent elements (with a time complexity of O(n)). Core characteristics of arrays are: same element type, 0-based indexing, and contiguous storage. Their advantages include fast access speed (O(1)), but disadvantages are lower efficiency for insertions/deletions and fixed size. As a foundational data structure, understanding the core idea of arrays—"indexed access and contiguous storage"—is crucial for learning more complex structures like linked lists and hash tables, making arrays a fundamental tool for data management.
Read More