Page 1: Fundamentals and Core Architecture
What is NumPy and why is it preferred over Python lists?
NumPy (Numerical Python) is a foundational library for scientific computing. Unlike
Python lists, which store heterogeneous data as an array of references, NumPy
uses contiguous memory blocks for homogeneous data types. This results in:
o Performance: Faster element access and operations due to optimized C/C++
backends.
o Memory Efficiency: No overhead for storing type information per element.
o Vectorization: Capability to perform element-wise operations without
explicit for loops.
What is an ndarray ?
The ndarray (n-dimensional array) is the core object in NumPy. It encapsulates n-
dimensional arrays of homogeneous data types.
How do you inspect array properties?
Use the following attributes:
o .shape : Returns a tuple representing the size of each dimension.
o .size : Returns the total number of elements in the array.
o .dtype : Returns the data type of the elements (e.g., int64 , float32 ).
Page 2: Array Creation and Manipulation
Differentiate between [Link]() , [Link]() , and [Link]() .
o [Link]() : Converts existing lists or tuples into arrays.
o [Link](start, stop, step) : Creates an array with evenly spaced values
based on a step size.
o [Link](start, stop, num) : Creates an array with a specified number of
elements between two values.
What is the difference between flatten() and ravel() ?
o flatten() : Always returns a copy of the original array in 1D.
o ravel() : Returns a view of the original array whenever possible, which is more
memory-efficient.
Explain "Copy" vs. "View".
o View: A new array that looks at the same data as the original. Modifying a view
affects the original.
o Copy (Shallow/Deep): Creates a new array with its own data. Changes to a copy do
not affect the original.
Page 3: Broadcasting and Vectorization
What is Broadcasting?
Broadcasting is the mechanism that allows NumPy to perform arithmetic operations
on arrays of different shapes. It "expands" the smaller array to match the shape of
the larger one without actually duplicating data in memory.
o Rules: Dimensions must either be equal or one of them must be 1.
What is Vectorization?
Vectorization refers to the process of offloading mathematical operations to highly
optimized, pre-compiled C-code. This eliminates the overhead of Python loops and
makes code significantly faster.
Example: Element-wise Operation vs. Loop
python
# Loop approach (slow)
result = [x + 5 for x in my_list]
# Vectorized approach (fast)
result = my_array + 5
Use code with caution.
Page 4: Advanced Indexing and Statistics
How does Boolean Indexing work?
It allows you to select elements based on a condition.
python
arr = [Link]([1, 10, 5, 20])
filtered = arr[arr > 8] # Returns [10, 20]
Use code with caution.
What are Universal Functions (ufuncs)?
Ufuncs are functions that operate element-wise on ndarrays . Examples
include [Link]() , [Link]() , and [Link]() .
Statistical Aggregations:
o [Link]() : Arithmetic mean.
o [Link]() : Median value.
o [Link]() : Standard deviation.
o Axis Parameter: Setting axis=0 performs the operation column-wise,
while axis=1 does it row-wise.
Page 5: Linear Algebra and Machine Learning Integration
Matrix Multiplication: Use [Link](A, B) or the @ operator. [Link]() is
preferred for higher-dimensional arrays.
Matrix Inversion: Handled by the [Link]() function.
Handling Missing Values: Use [Link]() to identify NaN values. Functions
like [Link]() allow calculating statistics while ignoring these missing values.
Stacking Arrays:
o [Link]() : Vertical stacking (row-wise).
o [Link]() : Horizontal stacking (column-wise).
Pro-tip for Word: Use "Consolas" or "Courier New" font for code snippets to
make them readable.
Would you like me to expand on any specific section, such as Performance
Optimization or Linear Algebra, to add more depth to the document?
Page 1: Architecture & Memory Management
What is the fundamental difference between a Python List and a NumPy Array?
o Memory: NumPy arrays use a contiguous block of memory and store homogeneous
data types. Python lists store pointers to objects scattered in memory.
o Performance: NumPy operations are implemented in C, avoiding the overhead of
Python’s global interpreter lock (GIL) for mathematical tasks.
o Functionality: NumPy supports "Vectorization," allowing operations on entire arrays
without explicit for loops.
Explain the ndarray object and its metadata.
An ndarray consists of a pointer to data, a data type ( dtype ), a shape (dimensions),
and strides (number of bytes to skip to get to the next element).
What are Strides in NumPy?
Strides are the number of bytes to step in each dimension when traversing an array.
For a 1D array of int64 (8 bytes), the stride is (8,). For a 2D array, it defines how
many bytes to skip to move to the next row.
How do you check the memory size of an array?
Use [Link] to get the total bytes consumed by the elements of the array.
Page 2: Array Creation & Basic Manipulation
How do you create arrays with specific patterns?
o [Link]((3,3)) : Creates a 3x3 matrix of zeros.
o [Link]((2,2)) : Creates a 2x2 matrix of ones.
o [Link](4) : Creates a 4x4 Identity matrix.
o [Link]((2,2), 7) : Creates a matrix filled with the number 7.
What is the difference between [Link]() and [Link]() ?
o arange(start, stop, step) defines the interval between values.
o linspace(start, stop, num) defines the total number of samples you want
between two points.
Explain "Reshaping" vs. "Resizing".
o .reshape() : Returns a new view with a different shape but the same total number of
elements.
o .resize() : Modifies the array in-place and can change the total number of elements
(filling with zeros if larger).
Page 3: The Power of Indexing & Slicing
What is Slicing in NumPy?
Slicing uses the [start:stop:step] syntax. Crucially, a slice is a view, not a copy.
Changing a slice changes the original array.
How does Boolean Indexing work?
It allows filtering data based on conditions.
python
arr = [Link]([1, 10, 15, 20])
mask = arr > 10
print(arr[mask]) # Output: [15, 20]
Use code with caution.
What is Fancy Indexing?
Passing an array of integers to index an array. Unlike slicing, fancy indexing always
returns a copy of the data.
Explain the use of [Link]() .
It acts as a vectorized ternary operator: [Link](condition, value_if_true,
value_if_false) . It is highly efficient for data transformation.
Page 4: Universal Functions (ufuncs) & Broadcasting
What are Universal Functions?
ufuncs are functions that operate element-wise on ndarrays . They support array
broadcasting, type casting, and several other standard features.
Examples: [Link]() , [Link]() , [Link]() .
What are the rules of Broadcasting?
Broadcasting allows operations between arrays of different shapes if:
1. The dimensions are equal, OR
2. One of the dimensions is 1.
If neither condition is met, a ValueError is raised.
How do you perform element-wise vs. matrix multiplication?
o Element-wise: Use * or [Link]() .
o Matrix Product: Use @ or [Link]() .
What is the axis parameter?
In most NumPy aggregations ( sum , mean , max ):
o axis=0 : Operation performed down columns.
o axis=1 : Operation performed across rows.
Page 5: Advanced Computation & Optimization
How do you handle Missing Values in NumPy?
NumPy uses [Link] (Not a Number). Since NaN is a float, any integer array
containing NaN will be upcast to float. Use [Link](arr) to find them.
Explain the difference between flatten() and ravel() .
o ravel() : Returns a flattened view (no memory overhead unless necessary).
o flatten() : Returns a flattened copy (uses new memory).
How do you stack multiple arrays?
o [Link]() : Stack arrays vertically.
o [Link]() : Stack arrays horizontally.
o [Link]() : Join a sequence of arrays along an existing axis.
How can you speed up NumPy operations?
o Avoid Python loops.
o Use in-place operations ( a += b instead of a = a + b ) to save memory.
o Use specialized functions like [Link]() for complex matrix operations.