Python - Numpy Library Flashcards

1
Q

What are numpy arrays and how are they different than lists?

A

Numpy arrays are grids that contain values of the same type.

On a structural level, they are composed of nothing but pointers.

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

What are the 4 components of a numpy array?

A

1 - Data pointer - the memory address of the first byte in an array
ex) my_array.data

2 - Data-type pointer (dtype) - describes the kind of elements in the array
ex) my_array.dtype

3 - Shape - indicates the shape of an array (row, col)
ex) my_array.shape

4 - Stride - number of bytes that should be skipped in memory to get to next element; a stride of (10,1) means you need to proceed 10 bytes to get to next row and one byte to locate next column
ex) my_array.strides

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

Make an array of
1 2 3
4 5 6

A

my_arrray = np.array ( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] )

*notice the nested list-like arrangement

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

Create a 3x4 array of ones

A

np. ones ( (3,4) )

* notice how the input to the function is in tuple-format

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

Create a 2x3x4 array of zeros of type int16

A

np.zeros ( (2,3,4) , dtype=np.int16 )

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

Create a 2x2 array with random values

A

np.random.random ( (2,2) )

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

Create an empty 3x2 array

A

np.empty ( (3,2) )

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

Create a full 2x2 array of 7s

A

np.full ( (2,2), 7 )

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

Create an array of evenly-spaced values starting from 4 going no further than 20, and incrementing by 3 each time

A

np.arange ( 4, 20, 3 )
_____
[ 4 7 10 13 16 19 ]

*notice the arguments are not in tuple-format, just regular old argument format

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

Create an array of evenly-spaced values starting at 0, ending at 2, and having 9 values total

A

np.linspace ( 0, 2, 9 )
_____
[ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ]

*notice how this is different than arange since you can specify the exact ending and how many values you want, where in arange you specify the max endpoint and the step/increment size

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

Create an identity matrix

(this is a matrix where all elements in the principal diagonal are ones and all other elements are zero…when you multiply a matrix with an identity matrix you get the same matrix unchanged)

A

np. eye()
- or-
np. identity()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
How would you load the following text file (data.txt) into your program as 3 different variables x, y, z:
\_\_\_\_\_
# This is your data in the text file
# Value1  Value2  Value3
# 0.2536  0.1008  0.3857
# 0.4839  0.4536  0.3561
# 0.1292  0.6875  0.5929
# 0.1781  0.3049  0.8928
# 0.6253  0.3486  0.8791
A

x, y, z = np.loadtxt (‘data.txt’,
skiprows=1,
unpack=True)

____
Where first arg is file name,
second arg is skipping first row,
third arg returns cols as separate arrays
(there is also a delimiter arg too in case for commas, etc)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
How would you load the following text file (data.txt) into your program as 3 different variables x, y, z (but now handling MISSING VALUES):
\_\_\_\_\_
# Your data in the text file
# Value1  Value2  Value3
# 0.4839  0.4536  0.3561
# 0.1292  0.6875  MISSING
# 0.1781  0.3049  0.8928
# MISSING 0.5801  0.2038
# 0.5993  0.4357  0.7410
A

my_array2 = np.genfromtxt(‘data2.txt’,
skip_header=1,
filling_values=-999)

____
By default, this function converts character strings that happen to be in numeric columns into NAN values. You can specify that instead you convert these to a number like -999.

If by any chance, you have values that don’t get converted to nan by genfromtxt(), there’s always the missing_values argument that allows you to specify what the missing values of your data exactly are.

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

How would you save the following array to a text file?
_____
import numpy as np
x = np.arange(0.0,5.0,1.0)

A
import numpy as np
x = np.arange(0.0,5.0,1.0)
np.savetxt('test.out', x, delimiter=',')
\_\_\_\_\_
*Remember that np.arange() creates a NumPy array of evenly-spaced values. The third value that you pass to this function is the step value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What 3 functions allow you to save numpy arrays to text files as…
1 - binary file .npy
2 - uncompressed .npz archive
3 - compressed .npz archive

A

1 - save()
2 - savez()
3 - savez_compressed()

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

What is the attribute to print the number of array dimensions?

A

print( my_array.ndim )

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

What is the attribute to print the number of array elements?

A

print( my_array.size )

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

What is the attribute to print the length of one array in bytes?

A

print( my_array.itemsize )

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

How do you change the data type of the array elements?

A

my_array.astype( float )

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

At a high-level, what is Broadcasting?

A

It’s a mechanism that allows NumPy to work with arrays of different shapes when you’re performing arithmetic operations.

To put it in a more practical context, you often have an array that’s somewhat larger and another one that’s slightly smaller. Ideally, you want to use the smaller array multiple times to perform an operation (such as a sum, multiplication, etc.) on the larger array.

However, to make sure that the broadcasting is successful, the dimensions of your arrays need to be compatible. Two dimensions are compatible when they are equal.

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

If the dimensions of the arrays are not compatible/equal, what kind of error do you get?

A

ValueError

22
Q

You can either just use +, -, *, / or % to add, subtract, multiply, divide or calculate the remainder of two (or more) arrays.

Or you can use the equivalent functions which are…

A

np.add()
np.subtract()
np.multiply()
np.divide()
np.remainder()
_____
Other useful functions are:
np.sin()
np.cos()
np.log()
np.dot()

*last one is the dot product

23
Q

Function to take array-wise sum…

A

a.sum()

24
Q

Function to take array-wise minimum value…

A

a.min()

25
Q

Function to take max value of an array row…

A

a.max( axis=0 )

26
Q

Function to take the cumulative sum of elements…

A

a.cumsum( axis=1 )

27
Q

Function to take the mean of an array…

A

a.mean()

28
Q

Function to take the median of an array…

A

a.median()

29
Q

Function to take the correlational coefficient of an array…

A

a.corrcoef()

30
Q

Function to take standard deviation of array…

A

np.std( a )

31
Q

Function to check whether all the elements of two arrays are the same…

A

np.array_equal()

32
Q

Functions to do logical operations on all elements of the array…

A

np. logical_or()
np. logical_not()
np. logical_and()

33
Q

Select the element of my_array at 1st index

A

print( my_array [1] )

34
Q

Select the element of my_array at row 1 column 2

A

print( my_2d_array [1] [2] )
–or–
print( my_2d_array [1,2] )

35
Q

Select items at index 0 and 1

A

print( my_array [0:2] )

36
Q

Select items at row 0 and 1, column 1

A

print( my_2d_array [ 0:2, 1 ] )

37
Q

Select items at row 1 in 3D array

A

print( my_3d_array [ 1 , … ] )
—or—
print( my_3d_array [ 1 , : , : ] )

38
Q

Select all elements of array that are less than 2

A

print( my_array [my_array<2] )

39
Q

Save a 3D array from old version of the array that only has elements that are greater than or equal to 3

A
# Specify a condition
bigger_than_3 = (my_3d_array >= 3)

Use the condition to index our 3d array
print(my_3d_array[bigger_than_3])
_____
Note that you can also chain together conditions with logical operators like this:

bigger_than_3 = (my_3d_array > 3) | (my_3d_array == 3)

40
Q

What does the following do?

my_2d_array [ :, [ 0,1,2,0 ] ]

A

It tells you that you want to keep all the rows of this result, but that you want to change the order of the columns around a bit. You want to display the columns 0, 1, and 2 as they are right now, but you want to repeat column 0 as the last column instead of displaying column number 3

41
Q

How would you transpose a 2D array?

A

np.transpose ( my_2d_array )
—or—
my_2d_array.T

42
Q

How would you RESIZE an array where if you make it bigger in any one dimension, it just fills in the extra stuff with 0’s.

Resize x to ((6,4))

A

np. resize(x, (6,4))
- –or—
x. resize((6,4))

43
Q

How would you RESHAPE an array to do following?

Print the size of x to see what’s possible

Reshape x to (2,6)

Flatten x

Print z

A
# Print the size of `x` to see what's possible
print( x.size )
# Reshape `x` to (2,6)
print( x.reshape( (2,6) ) )
# Flatten `x`
z = x.ravel()
# Print `z`
print(z)
44
Q

Append the folllowing 1D array to your my_array:

[ 7, 8, 9, 10 ]

A

new_array = np.append( my_array, [7, 8, 9, 10] )

45
Q

How to insert ‘5’ at index 1 of my_array?

A

np.insert(my_array, 1, 5)

46
Q

How to delete the value at index 1 of my_array?

A

np.delete( my_array, [1] )

47
Q

Concatentate my_array and x

A

np.concatenate( (my_array,x) )

*Notice that you need to pass in a tuple
____
The number of dimensions needs to be the same if you want to concatenate two arrays

48
Q

Stack my_array & my_2d_array row-wise…

A

np.vstack( (my_array, my_2d_array) )

  • Notice that you need to pass in a tuple
  • –or—
    np. r_[my_resized_array, my_2d_array]

____
You just have to make sure that, as you’re stacking the arrays row-wise, that the number of columns in both arrays is the same. As such, you could also add an array with shape (2,4) or (3,4) to my_2d_array, as long as the number of columns matches.

49
Q

Stack my_array & my_2d_array horizontally…

A

np.hstack( (my_resized_array, my_2d_array) )
____
You have to make sure that the number of dimensions is the same and that the number of rows in both arrays is the same. That means that you could stack arrays such as (2,3) or (2,4) to my_2d_array, which itself as a shape of (2,4). Anything is possible as long as you make sure that the number of rows matches. This function is still supported by NumPy, but you should prefer np.concatenate() or np.stack()

50
Q

Stack my_array & my_2d_array column-wise…

A

np.column_stack( (my_resized_array, my_2d_array) )

  • Notice that you need to pass in a tuple
  • –or—
    np. c_[my_resized_array, my_2d_array]

_____
You have to make sure that the arrays that you input have the same first dimension.

51
Q

How would you…

Split my_stacked_array horizontally at the 2nd index

Split my_stacked_array vertically at the 2nd index

A
# Split `my_stacked_array` horizontally at the 2nd index
print( np.hsplit( my_stacked_array, 2) )
# Split `my_stacked_array` vertically at the 2nd index
print( np.vsplit( my_stacked_array, 2 ) )