NumPy Arrays (ndarray) and Core Concepts#
đ Data Analysis Using Python đź NumPy & pandas Lesson 028
â Previous ¡ Next ✠¡ â Section ¡ â Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
The array in depth#
NumPyâs central structure is the ndarray (n-dimensional array), and understanding it â how it differs from a list, its key properties, and its core operations â is essential to using NumPy and pandas well. This lesson covers the ndarray and NumPyâs core concepts, deepening the array foundation.
The ndarray versus a list#
A NumPy array (ndarray) looks list-like but differs crucially:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
Homogeneous â all elements are the same type (all integers, all floats), unlike a listâs mixed types. This uniformity is what enables NumPyâs speed.
Fixed size â an array has a set size; you do not append to it as you do a list (you create new arrays instead).
Vectorized â operations apply to the whole array (the previous lesson), which lists do not support (
[1,2,3] * 2repeats the list rather than doubling elements).Fast and compact â arrays store data efficiently and operate on it in optimised code, far outperforming lists for numeric work.
The array trades the listâs flexibility (mixed types, easy resizing) for speed and vectorization on uniform numeric data â the right trade for numerical analysis.
Core array properties and creation#
Arrays have properties describing their structure, and several creation functions:
arr = np.array([[1, 2, 3], [4, 5, 6]]) # a 2D array (from nested lists)
arr.shape # (2, 3) â 2 rows, 3 columns
arr.ndim # 2 â number of dimensions
arr.size # 6 â total elements
arr.dtype # the element data type (e.g. int64)
np.zeros(5) # array of five 0.0s
np.arange(0, 10, 2) # array([0, 2, 4, 6, 8]) â like range()
np.linspace(0, 1, 5) # 5 evenly spaced values from 0 to 1
The shape (dimensions) is central â arrays can be 1D (a vector), 2D (a matrix, like a table), or more, and the shape describes that structure. NumPyâs creation functions build common arrays without manual listing.
Indexing, slicing, and operations#
Arrays are indexed and sliced like lists (zero-based, exclusive stop), extended to multiple dimensions, and support rich vectorized operations:
arr = np.array([10, 20, 30, 40, 50])
arr[0] # 10
arr[1:3] # array([20, 30])
arr[arr > 25] # array([30, 40, 50]) â boolean indexing!
arr.sum(); arr.mean(); arr.max(); arr.std() # aggregate functions
The last â arr[arr > 25] â is boolean indexing: selecting elements by a condition,
which returns only the matching elements. This is exactly the filtering idea from SQL
WHERE and spreadsheet filters, vectorized â and it is the foundation of the boolean
masking that pandas uses to filter data (an upcoming lesson).
Why the ndarray matters#
The ndarray is the foundation of numerical Python â NumPyâs own operations, and pandas (whose columns are essentially arrays), all rest on it. Understanding arrays â homogeneous, vectorized, shaped, boolean-indexable â is understanding the substrate of all Python data analysis. The concepts here (vectorization, boolean indexing, aggregation) reappear directly in pandas, so the array is where the idioms of data analysis in Python are first and most clearly learned.
The caveat#
The ndarrayâs constraints are real and occasionally surprising. Its homogeneity means mixing types coerces them (putting a float in an int array converts, and mixing numbers and strings makes everything strings), which can silently change data â the array assumes uniform type. Its fixed size means âgrowingâ an array actually creates a new one, so list-style appending in a loop is both wrong-idiom and slow (build the data as a list then convert, or use vectorized construction). And array operations require compatible shapes â operating on mismatched shapes errors or triggers broadcasting rules that surprise the unwary. For the flat, uniform numeric data of typical analysis these rarely bite, but the array is a stricter, more structured thing than a list â which is exactly what makes it fast. The next lessons build on the array to reach pandas, the analystâs primary Python tool.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/numpy-arrays-ndarray-and-core-concepts/ (insightful-data-lab.com).