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.