Numpy Flashcards
How one can slice a ndarray and change its data without affecting the original array?
Either use the np.copy() function or the .copy() method., both will yield a copy from the target array. The .copy() method is usually preferred over the function.
Example using .( )copy method
IN: my_array[0].copy()
Given how axes work in ndarray, how to interpret axis 0 and 1 in a 2d array?
In a 2d-array, data is structured in a tabular form, ie. it is spread across rows and columns. In this case, axis 0 would correspond to the rows while axis 1 would correspond to the columns.
Does indexing yield a copy or a view from the original ndarray?
What about slicing?
In both cases, the output is a VIEW. This means that whatever changes are made to the view will propagate back to the original array.
Example: assign a new variable to a slice of a ndarray. Then, change one of the elements of this new variable through value assignment. When printing the original array, one will see that the value assignment has propagated back to the original array.
Describe what is slicing and indexing
Indexing is used to select a single element from the array while slicing allows one to select a subset of elements from the array
In a 2d-array with shape (2,2), what would be the output from arr[:2, 1:]?
It would yield the first rows and the last two columns of the array, hence it will return another 2d-array.
What does ‘ndarray’ stands for?
N-dimensional array
Describe how indexing works in a ndarray,
One pass a tuple of integers to index the array, for example, arr[0,2,3,…]. The first element of the tuple references ax0, the second element, the ax1 and so on. Indexing always stars from axis 0
How to check the version installed of numpy?
IN: print(np.__version__)
How does boolean indexing work?
IN: my_array > 1.35), there is no explicit reference to OUT: #returns a NEW BOOLEAN ARRAY containing the result from applying to each and every element of the array
IN: my_array [ (my_array > 1.35) ]
OUT: # returns only the elements for which the condition is True.
How to combine multiple boolean conditions when boolean indexing?
Use boolean arithmetic operators such as & (ie. And) or | (ie. Or).
For example, mask = (names == ‘Bob’) | (names == ‘John’)
Note: The keywors and / or will not work with boolean arrays.
Note: Must not forget to put condition between round brackes (…)
The output from boolean indexing is a view of a copy of the original array? What are the consequences then?
Boolean indexing will always create a copy of the data. This means that changing the resulting array by value assignment will not alter the original array.
The output from ‘regular’ indexing (not boolean, fancy, etc) is a view of a copy of the original array? What are the consequences then?
Indexing will result in a view of the original array. This means that any changes to the view, for example by value assignment, will propagate back to the original array.
How to do boolean indexing by negating a given condition?
Use the ~ (til) operator.
For example:
IN: mask = (arr == ‘Bob’) | (arr == ‘John’)
IN: array[~mask]
What is Fancy Indexing? How does it work when the array has more than 1 dimension and to do this?
Fancy Indexing is another name for indexing using integer arrays.
IN: my arr [ [ 1, 4, 5, 2, 3 ] ]
The first array of int will refer to axis 0, the second array will refer to axis 1, and so on.
IN: my arr [ [ 1, 4, 5, 2, 3 ], [ 2, 3, 5, 1, 0 ] ]
Does Fancy |ndexing create a copy of a view from the array being indexed?
Creates a copy from the original array. Fancy indexing works differently than ‘regular ‘indexing.