Indexing, slicing, iterating, shape manipulation, and stacking Flashcards
What is slicing in multi-dimensional arrays?
Slicing allows you to access specific rows, columns, or subarrays. For example, b[0:5, 1] accesses rows from index 0 to index 4, in the second column.
Why can’t you reshape a 1D array into a 3x3 array?
You cannot reshape if the total number of elements doesn’t match. A 3x3 array requires 9 elements.
What happens when using -1 in reshape?
Using -1 allows NumPy to automatically calculate the missing dimension based on the total number of elements.
What is iterating in programming?
Iterating means going through each element of a collection (like an array) one at a time, often using loops.
How can you access a specific element in a 2D array?
Use indexing with two indices, e.g., array[row_index, column_index].
What does b[:, 1] do?
It selects all rows in the second column of the array b.
How can you flatten a multi-dimensional array?
Use the ravel() method to convert a multi-dimensional array into a 1D array. It returns a flattened view of the array without altering the original data structure, allowing for easy manipulation and analysis of the elements in a single line.
What does the reshape function do?
The reshape function changes the shape of an array without altering its data.
What does the T attribute do for arrays?
The T attribute transposes the array, swapping rows and columns.