u10-slides-numpy Flashcards
What is the main purpose of NumPy?
It’s the go-to module for numerical computations in Python, providing optimized functions for scientific computations and array data handling
Why is NumPy faster than standard Python?
NumPy functions are highly optimized and implemented in C, and arrays store elements in contiguous memory blocks
How are elements stored in NumPy arrays vs Python lists?
In NumPy arrays, elements are stored as direct bit patterns in contiguous memory; in Python lists, elements are stored as references to objects
What is a key difference in data types between NumPy arrays and Python lists?
NumPy arrays have fixed data types for all elements, while Python lists can contain elements of different types
What is meant by ‘contiguous memory’ in NumPy arrays?
Elements are stored as one block with consecutive memory addresses, making access faster
What is row-major order in NumPy?
The default storage method where consecutive elements of a row reside next to each other in memory
What is column-major order in NumPy?
A storage method where consecutive elements of a column reside next to each other in memory
How do you access elements in a multi-dimensional NumPy array?
Using comma-separated indices, e.g., my_array[row, col]
What is the shape of a NumPy array?
It defines the multi-dimensionality of an array, specifying the length of each dimension
What is an axis in NumPy?
A specific dimension of an array; a 2D array has 2 axes, a 3D array has 3 axes, etc.
How are 2D arrays stored in NumPy internally?
They’re stored as 1D arrays in a flat manner, with a mapping system to maintain the dimensional structure
Why is slicing fast in NumPy arrays?
Because elements are stored consecutively in memory, making it efficient to access continuous sections
What are the indexing options available in NumPy?
Integer indexing, slicing, multi-dimensional indexing, boolean masking, and list of indices
How is memory calculated for accessing elements in a 2D array?
For position [r,c] in an array with n columns: index = n * r + c
What is the main advantage of NumPy’s storage method?
Operations on elements can be optimized and are faster due to fixed data types and contiguous memory storage