NumPy Cheat Sheet
NumPy Cheat Sheet
empty()
Inspecting Properties Output: Removing Elements from Numpy Array Flatten a Two Dimensional array a[start:stop:step] Vector Math corrcoef
NumPy Cheat Sheet print(np.empty([4, 3], dtype=int))
Divide
print("mean of arr:", np.mean(arr))
np.ones([4, 3], dtype = [ 5 6 7 8 9 10]] np.divide(a, b)
li = [1, 2, 3, 4] ones()
np.int32)
printing reshaped array elements of 2 Array median
print(np.array(li))
full() np.full([2, 2], 67, dtype =
Sorting 1D Array arr.sort() Array
[[ 1
after appending the values row wise
2 3 4 5 6 1]
print("\nSecond Reshaped Array:") Indexing, Slicing and Subsetting Modulo
elements of 2 Array
np.mod(a, b) print("median of arr:", np.median(arr))
int) Sorting along the first axis of print(reshaped2)
create a NumPy array from a tuple the 2D array
np.sort(a, axis = 0) [ 7 8 9 10 11 12 2]] Remainder np.remainder(a,b)
sum
tup = (5, 6, 7, 8) eye() np.eye(4) elements of 2 Array print("Sum of arr(uint8):",
print(np.array(tup))
Subsetting Numpy Array Power
elements of 2 Array
np.power(a, b) np.sum(arr, dtype = np.uint8))
create a NumPy array using fromiter() Python3 NumPy Array Manipulation Inserting Elements into the Array Resizing an Array Exponant np.exp(b)
print("Sum of arr(float32):",
np.sum(arr, dtype = np.float32))
elements of 2 Array
iterable = (a for a in range(8)) create a NumPy array using numpy.zeros()
print(np.fromiter(iterable, float))
print(np.arange(1, 10))
Python3 min and max
Appending Elements to Array One-Dimensional array Numpy arrays can be resized using the resize() Index values can be negative.
print("maximum element:", np.max(arr))
Multi-Dimensional Array create a NumPy array using numpy.ones()
function. It returns nothing but changes the print(arr)
Comparison print("minimum element:", np.min(arr))
print(np.ones([4, 3], dtype = np.int32)) Python3 var
np.array([[1, 2, 3, 4],[5, 6,
One-Dimensional array original array. print("Elements are:", arr[np.array([1, 3, -3])])
Using Python Lists 7, 8], [9, 10, 11, 12]]) arr = np.asarray([1, 2, 3, 4]) print("var of arr:", np.var(arr))
create a NumPy array using numpy.full()
Python3 Python3 print("var of arr(float32):",
Using empty() np.empty([4, 3], dtype=int) print(np.full([2, 2], 67, dtype = int)) Python Program illustrating numpy.insert() Python3 np.var(arr, dtype = np.float32))
Adding the values at the end print("1D arr:", arr) Slicing Numpy Array an_array = np.array([[1, 2], [3, 4]]) standard deviation
Python3 create a NumPy array using numpy.eye() print("Shape:", arr.shape) Making a random array
of a numpy array arr = np.array([1, 2, 3, 4, 5, 6])
another_array = np.array([[1, 2], [3, 4]]) print("std of arr:", np.std(arr))
print(np.eye(4))
create a NumPy array from a list print("Original Array:", arr) Inserting value 9 at index 1 The ":" operator means all elements till the end. comparison = an_array == another_array print ("More precision with float32",
list_1 = [1, 2, 3, 4] a = np.insert(arr, 1, 9) Required values 12, existing values 6 equal_arrays = comparison.all() np.std(arr, dtype = np.float32))
list_2 = [5, 6, 7, 8] appending to the array print("\nArray after insertion:", a) arr.resize(3, 4)
Python3
list_3 = [9, 10, 11, 12] arr = np.append(arr, [7]) print("Shape:", a.shape) print(arr) print(equal_arrays)
print(np.array([list_1, list_2, list_3])) print("Array after appending:", arr) print(arr)