Python - Numpy Library Flashcards
What are numpy arrays and how are they different than lists?
Numpy arrays are grids that contain values of the same type.
On a structural level, they are composed of nothing but pointers.
What are the 4 components of a numpy array?
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
Make an array of
1 2 3
4 5 6
my_arrray = np.array ( [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] )
*notice the nested list-like arrangement
Create a 3x4 array of ones
np. ones ( (3,4) )
* notice how the input to the function is in tuple-format
Create a 2x3x4 array of zeros of type int16
np.zeros ( (2,3,4) , dtype=np.int16 )
Create a 2x2 array with random values
np.random.random ( (2,2) )
Create an empty 3x2 array
np.empty ( (3,2) )
Create a full 2x2 array of 7s
np.full ( (2,2), 7 )
Create an array of evenly-spaced values starting from 4 going no further than 20, and incrementing by 3 each time
np.arange ( 4, 20, 3 )
_____
[ 4 7 10 13 16 19 ]
*notice the arguments are not in tuple-format, just regular old argument format
Create an array of evenly-spaced values starting at 0, ending at 2, and having 9 values total
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
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)
np. eye()
- or-
np. identity()
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
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 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
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 would you save the following array to a text file?
_____
import numpy as np
x = np.arange(0.0,5.0,1.0)
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.
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
1 - save()
2 - savez()
3 - savez_compressed()
What is the attribute to print the number of array dimensions?
print( my_array.ndim )
What is the attribute to print the number of array elements?
print( my_array.size )
What is the attribute to print the length of one array in bytes?
print( my_array.itemsize )
How do you change the data type of the array elements?
my_array.astype( float )
At a high-level, what is Broadcasting?
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.
If the dimensions of the arrays are not compatible/equal, what kind of error do you get?
ValueError
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…
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
Function to take array-wise sum…
a.sum()
Function to take array-wise minimum value…
a.min()
Function to take max value of an array row…
a.max( axis=0 )
Function to take the cumulative sum of elements…
a.cumsum( axis=1 )
Function to take the mean of an array…
a.mean()
Function to take the median of an array…
a.median()
Function to take the correlational coefficient of an array…
a.corrcoef()
Function to take standard deviation of array…
np.std( a )
Function to check whether all the elements of two arrays are the same…
np.array_equal()
Functions to do logical operations on all elements of the array…
np. logical_or()
np. logical_not()
np. logical_and()
Select the element of my_array at 1st index
print( my_array [1] )
Select the element of my_array at row 1 column 2
print( my_2d_array [1] [2] )
–or–
print( my_2d_array [1,2] )
Select items at index 0 and 1
print( my_array [0:2] )
Select items at row 0 and 1, column 1
print( my_2d_array [ 0:2, 1 ] )
Select items at row 1 in 3D array
print( my_3d_array [ 1 , … ] )
—or—
print( my_3d_array [ 1 , : , : ] )
Select all elements of array that are less than 2
print( my_array [my_array<2] )
Save a 3D array from old version of the array that only has elements that are greater than or equal to 3
# 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)
What does the following do?
my_2d_array [ :, [ 0,1,2,0 ] ]
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
How would you transpose a 2D array?
np.transpose ( my_2d_array )
—or—
my_2d_array.T
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))
np. resize(x, (6,4))
- –or—
x. resize((6,4))
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
# 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)
Append the folllowing 1D array to your my_array
:
[ 7, 8, 9, 10 ]
new_array = np.append( my_array, [7, 8, 9, 10] )
How to insert ‘5’ at index 1 of my_array?
np.insert(my_array, 1, 5)
How to delete the value at index 1 of my_array?
np.delete( my_array, [1] )
Concatentate my_array
and x
…
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
Stack my_array & my_2d_array row-wise…
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.
Stack my_array & my_2d_array horizontally…
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()
Stack my_array & my_2d_array column-wise…
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.
How would you…
Split my_stacked_array
horizontally at the 2nd index
Split my_stacked_array
vertically at the 2nd index
# 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 ) )