click below
click below
Normal Size Small Size show me how
Python numpy
Term | Definition |
---|---|
np.array() | Creates an ndarray |
some_array.ndim | Returns the number of dimensions in an ndarray |
ndmin=5 | Uses the **kwarg “ndmin” to set the number of dimensions in a ndarray to 5 |
Numpy Data Types: i b u f c m M O S U V | i - integer b - boolean u - unsigned integer f - float c - complex float m - timedelta M - datetime O - object S - string U - unicode string V - some fixed chunk of memory (void) |
some_array.dtype | Returns the datatype and size of an ndarray |
dtype=‘S’ | Uses the **kwarg “dtype” to define the ndarray as a string type |
new_array = some_array.astype(‘i’) | Creates a copy of some_array that is converted to integer type |
x = some_array.copy() | Makes a copy of some_array under the variable name “x” Changes to some_array generally will not affect x |
x = some_array.view() | Makes a view of some_array under the variable name “x” Changes to some_array will affect x |
some_array.base | • If the array owns its own data then it returns None • If it is a view or doesn’t own its data for some other reason then it returns the original array |
Output of this code: ———————————————— arr = np.array([[1, 2, 3], [5, 6, 7]]) print(arr.shape) | (2, 3) |
Output of this code: ———————————————— arr = np.array([[1, 2, 3, 4], [5, 6]]) print(arr.shape) | (2, ) |
Output of this code: ———————————————— arr = np.array([1, 2, 3], ndmin=5) print(arr.shape) | (1, 1, 1, 1, 3) |
newarr = arr.reshape(4, 2, -1) | Creates a 3d array that has 4 groups divided into 2 subgroups with an unknown number of elements. (-1 tells numpy that you don’t know the number of groups an array can be divided into so it just finds the smallest number into which the elem |
newarr = arr.reshape(-1) | Flattens the array to 1 dimesion |
for x in np.nditer(arr[:,::2], flags=[‘buffered’], op_types=[‘S’]): | For loop that uses nditer() to iterate over a multidimensional array |
For idx, x in np.ndenumerate(arr): | Just like np.nditer(arr) but returns the index as well |
arr = np.concatenate((arr1, arr2), axis=1) | Joins arr1 and arr2 in their second dimension |
arr = np.stack((arr1, arr2), axis=1) | Adds another dimension of pairs of the combined elements of arr1 and arr2 this is basically stacking along columns. The resulting number of columns equals the number of elements in each array. Very similar to concatenate with axis=1 |
arr = np.hstack((arr1, arr2)) | Stacks along rows. Very similar to concatenate with axis=0 |
arr = np.vstack((arr1, arr2)) | Stacks along columns. |
newarr = np.array_split(arr, 3) | Splits arr into 3 |
np.hsplit() | Opposite of np.hstack() |
np.vsplit() | Oppotite of np.vstack() |
np.dsplit() | Opposite of np.dstack() |
np.dstack() | Stacks along depth. Makes an subarray for each pair in the two joined arrays |
np.where() | Takes a conditional as a parameter and returns the index of the matching values |
np.searchsorted() | Performs a binary search on a sorted array and returns the index of a specified value |
np.sort(arr) | Sorts the array |
x = random.randint(100, size=3, 5) | Creates a 2D integer array with numbers from 0-100 that has 3 rows containing 5 integers. |
x = random.rand(3, 5) | Creates an array of random floats that has 3 groups of 5 numbers. |
random.choice([1, 2, 3, 4], size=(3, 5)) | Creates a 2D array containing 3 rows of 5 numbers randomly selected from the array. |
random.shuffle(arr) | Shuffles arr |
random.permutation(arr) | Returns a random permutation of arr |
np.trunc([]) | Returns the float rounded towards 0 |
np.fix([]) | Returns the float rounded towards 0 |
np.around(3.1666, 2) | 3.17 |
np.floor() | Rounds down to the nearest lower integer |
np.ceil() | Rounds up to nearest higher integer |
np.arrange(1, 10) | Returns an array with elements from 1 and up to but not including 10 |
np.log2() | Returns the base 2 logarithm |
np.log10() | Returns the base 10 logarithm |
no.log() | Returns the ln() |
np.sum() | Returns sum of all elements in an array(s) |
np.cumsum() | Returns an array containing the partial sum for each index |
np.prod() | Returns the product of all of the elements in an array |
np.cumprod() | Returns the partial product for each index of an array |
np.diff() | Returns the discrete difference for every successive pair of elements in an array |
np.lmc.reduce() | Returns the lowest common multiple of every element in the array |
np.gmc.reduce() | Returns the greatest common denominator ot the elements in the array. |
np.unique() | Returns all unique values from an array |
np.union1d(arr1, arr2) | Finds all unique values in two arrays |
np.intersect1d(arr1, arr2) | Finds only values present in both arrays |
np.setdiff1d(arr1, arr2) | Returns values that are in the first but ant not in the second set |
np.setxor1d(arr1, arr2) | Finds values that are in one array OR the other but NOT in both |