Numpy Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How one can slice a ndarray and change its data without affecting the original array?

A

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()

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

Given how axes work in ndarray, how to interpret axis 0 and 1 in a 2d array?

A

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.

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

Does indexing yield a copy or a view from the original ndarray?

What about slicing?

A

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.

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

Describe what is slicing and indexing

A

Indexing is used to select a single element from the array while slicing allows one to select a subset of elements from the array

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

In a 2d-array with shape (2,2), what would be the output from arr[:2, 1:]?

A

It would yield the first rows and the last two columns of the array, hence it will return another 2d-array.

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

What does ‘ndarray’ stands for?

A

N-dimensional array

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

Describe how indexing works in a ndarray,

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to check the version installed of numpy?

A

IN: print(np.__version__)

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

How does boolean indexing work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to combine multiple boolean conditions when boolean indexing?

A

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 (…)

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

The output from boolean indexing is a view of a copy of the original array? What are the consequences then?

A

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.

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

The output from ‘regular’ indexing (not boolean, fancy, etc) is a view of a copy of the original array? What are the consequences then?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to do boolean indexing by negating a given condition?

A

Use the ~ (til) operator.
For example:
IN: mask = (arr == ‘Bob’) | (arr == ‘John’)
IN: array[~mask]

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

What is Fancy Indexing? How does it work when the array has more than 1 dimension and to do this?

A

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 ] ]

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

Does Fancy |ndexing create a copy of a view from the array being indexed?

A

Creates a copy from the original array. Fancy indexing works differently than ‘regular ‘indexing.

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

What is the advantage of creating a view whenever a ndarray is sliced?

A

A boost in performance is achieved by avoiding having to copy data every time a ndarray is sliced.

17
Q

When is Fancy Indexing useful?

A

Whenever I want to select elements from a ndarray in a particular order.

18
Q

What is a universal function (a.k.a ufunc) in Numpy?

A

Function that performs element-wise operations on data in ndarrays.

19
Q

What is the difference between a unary and a binary ufunc? Provide an example for each one of them.

A

Unary - takes just 1 array
IN: np.sqrt ( arr22 )

Binary - takes 2 arrays
IN: arr_13 = np.add ( arr11, arr12 )