u10-slides-numpy Flashcards

1
Q

What is the main purpose of NumPy?

A

It’s the go-to module for numerical computations in Python, providing optimized functions for scientific computations and array data handling

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is NumPy faster than standard Python?

A

NumPy functions are highly optimized and implemented in C, and arrays store elements in contiguous memory blocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are elements stored in NumPy arrays vs Python lists?

A

In NumPy arrays, elements are stored as direct bit patterns in contiguous memory; in Python lists, elements are stored as references to objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a key difference in data types between NumPy arrays and Python lists?

A

NumPy arrays have fixed data types for all elements, while Python lists can contain elements of different types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is meant by ‘contiguous memory’ in NumPy arrays?

A

Elements are stored as one block with consecutive memory addresses, making access faster

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is row-major order in NumPy?

A

The default storage method where consecutive elements of a row reside next to each other in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is column-major order in NumPy?

A

A storage method where consecutive elements of a column reside next to each other in memory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you access elements in a multi-dimensional NumPy array?

A

Using comma-separated indices, e.g., my_array[row, col]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the shape of a NumPy array?

A

It defines the multi-dimensionality of an array, specifying the length of each dimension

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an axis in NumPy?

A

A specific dimension of an array; a 2D array has 2 axes, a 3D array has 3 axes, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How are 2D arrays stored in NumPy internally?

A

They’re stored as 1D arrays in a flat manner, with a mapping system to maintain the dimensional structure

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is slicing fast in NumPy arrays?

A

Because elements are stored consecutively in memory, making it efficient to access continuous sections

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the indexing options available in NumPy?

A

Integer indexing, slicing, multi-dimensional indexing, boolean masking, and list of indices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is memory calculated for accessing elements in a 2D array?

A

For position [r,c] in an array with n columns: index = n * r + c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the main advantage of NumPy’s storage method?

A

Operations on elements can be optimized and are faster due to fixed data types and contiguous memory storage

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the primary data structure in NumPy?

A

The numpy.ndarray object (N-dimensional array)

17
Q

Why does NumPy require all elements in an array to have the same data type?

A

For optimization purposes - operations can be performed faster when the data type is fixed and known

18
Q

What happens when you reshape a NumPy array?

A

The same data is viewed in a different dimensional structure, without changing the underlying memory layout

19
Q

How does NumPy’s memory efficiency compare to Python lists?

A

NumPy is more memory efficient as it stores values directly rather than object references

20
Q

What is the relationship between dimensions and axes in NumPy?

A

They are equivalent - the number of dimensions equals the number of axes