Python I Flashcards
attribute for getting the dimensions of a numpy array (e. g. (3, 4, 2))
arr.shape
attribute for getting the datatype of a numpy array
arr.dtype
attribute for getting the number of dimensions of a numpy array
arr.ndim
attribute for getting the number of all elements of a numpy array
arr.size
creating an array in numpy
np.array(my_list)
creating a range in numpy
np.arange(start, stop, step)
creating ranges of values in numpy
np.linspace(start, stop, numberOfElems, endpoint=True/False)
get multiple elements of a numpy array (index)
one_dim[[3, 4, 6, 15]] (gets elements at index 3, 4, 5, 6 and 15)
get the element of the 3rd row and 4th column of a numpy array
arr[2, 3] or arr[(2, 3)]
get the second row of a multidimensional numpy array
arr[1]
get the second column of a multidimensional numpy array
arr[: , 1]
extract the bottom right 2x2 corner of a 3x4 numpy array
arr[1:3, 2:4]
indexing of numpy arrays is also possible via masks
mask = np.array([[True, False, False, True],
[False, True, False, True],
[False, False, True, True]])
print(“two_dim[mask]”)
print(two_dim[mask])
(Returns flat (1D) array of elements where mask = True)
only extract even number out of a numpy array
mask = (arr % 2) == 0
print(arr[mask])
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
additional information numpy arrays
x2 = two_dim[:][2]
This will first create a slice of the array (in this case, this is simply the entire array again) and then access index 2 in that sliced array, which corresponds to “two_dim[2]”
set all elements from index 2 to 4 to zero in a numpy array
one_dim[2:5] = [0, 0, 0]
or
one_dim[2:5] = 0 (broadcasting)
set all entries of a twodimensional numpy array to 0
two_dim[:] = 0
set the second to third element of the second row of a numpy array to 0
two_dim[1, 1:4] = 0
(also subarrays can be broadcasted, e. g.
two_dim[:, 1:3] = [7, 8])
When can numpy arrays be reshaped?
When the number of elements stays the same.
a = np.arange(24)
a = a.reshape(2, 3, 4)
a = a.reshape(4, 6)
a = a.reshape(6, -1)
add an empty dimension
a1 = a[:, None, :]
a2 = a[:, np.newaxis, :]
Other important numpy functions
print(np.append(a, np.array([1, 2, 3])))
print(np.concatenate([a, a, a]))
print(np.repeat(a, repeats=5))
print(np.tile(a, reps=5))
important: sum in numpy arrays
Sum (similar functions available for: mean, standard deviation, etc.)
arr = np.arange(2 * 3 * 4).reshape((2, 3, 4)) * 3
print(arr.sum(axis=0)) # Sum along first axis/dimension (axis 0 will be “collapsed” -> shape: (3, 4))
print(arr.sum(axis=1)) # Sum along second axis/dimension (axis 1 will be “collapsed” -> shape: (2, 4))
print(arr.sum(axis=2)) # Sum along second axis/dimension (axis 2 will be “collapsed” -> shape: (2, 3))
print(arr.sum(axis=(1, 2))) # Sum along second + third axis (axis 1 + 2 will be “collapsed” -> shape: (2,))
print(arr.sum()) # “Global” sum along all axes/dimensions
Get the (flat!) index of the minimum of a numpy array and transform it into an array-shaped index
i_min_flat = arr.argmin()
i_min_shape = np.unravel_index(i_min_flat, shape=arr.shape)
numpy functions with conditionals
print(np.where(arr % 2 == 0, arr, other)) # If condition is True, take from “arr”, else take from “other”
print((arr > 50).all()) # Check if all elements meet the condition
print((arr > 50).any()) # Check if at least one element meets the condition
random values in numpy
rng = np.random.default_rng(seed=1234)
arr = rng.random((4, 3))
arr = rng.integers(low=1, high=25, size=5)
arr = rng.choice(np.arange(100), size=10, replace=False)
sort a numpy array
np.sort(arr)
or
arr.sort() - in-place
important info for numpy slicing and reshaping
Reshaping and slicing does not copy the original array, it only creates another “view” on the data.
regex - search for the first occurence of a pattern
re.search(pattern, text)
regex - find a pattern at the beginning of the string
re.match(pattern, text)
regex - groups
Groups can be used to only search for subpatterns within a search pattern (e. g. “H(all)o, how (are) you?”)
The found patterns can be accessed like this (0 is the whole pattern, 1 is the first group)
match_object.group(1)
Getting additional information from regex group match objects
match_object.start(1)
match_object.end(1)
match_object.span(1)
regex - find all occurences of a pattern
re.findall(pattern, text) -> list
(re.finditer does the same, but does it one item at a time and returns a match object)
regex - a set of characters to match
[…]
e. g. “[cbr]at”
regex - specify ranges
[…]
e. g. “[0-5]”
regex - negate patterns
e. g. “[^0-9]”
regex - predefined group for digits
\d
regex - predefined group for non-digits
\D
regex - predefined group for whitespace characters
\s
regex - predefined group for non-whitespace characters
\S
regex - predefined group for word characters
\w