numpy cheet sheet 6 page
numpy cheet sheet 6 page
Basic Operations
addition = arr + 2 # Add 2 to each element Array Functions
subtraction = arr - 2 # Subtract 2 from each mean = np.mean(arr) # Mean (average) of array
element median = np.median(arr) # Median of array
multiplication = arr * 2 # Multiply each element by2 sum_arr = np.sum(arr) # Sum of array
division = arr / 2 # Divide each element by 2 min_val = np.min(arr) # Minimum value in array
power = np.power(arr, 2) # Raise each element to max_val = np.max(arr) # Maximum value in array
the power of 2
Stacking Arrays
Element-wise Arithmetic Operations vstack = np.vstack((arr1, arr2)) # Stack arrays
add_arrays = np.add(arr, arr) vertically
hstack = np.hstack((arr1, arr2)) # Stack arrays
horizontally
Finding Index
index = np.where(arr == 3) # Find the index of the value 3 in the array
Element-wise Absolute Values Rounding
abs_arr = np.abs(arr) # Element-wise absolute rounded_arr = np.round(arr, decimals=2) # Round
values elements to a specified number of decimals
Interpolation
Repeat and Tile interp_val = np.interp(3.5, [3, 4], [30, 40]) # Linear
repeated = np.repeat(arr, 3) # Repeat elements interpolation
tiled = np.tile(arr, 3) # Tile the array
Element-wise Absolute Value for Complex Numbers Creating an Identity Matrix with a Custom
abs_complex = np.abs(complex_arr) Diagonal
# Element-wise absolute value for complex custom_identity = np.eye(3, k=1) # Create a 3x3
numbers identity matrix with a shifted diagonal
Binning Data
Dot Product for Multi-dimensional Arrays bin_counts, bin_edges = np.histogram(arr, bins=[0,
dot_product_multi_dim = 2, 4, 6, 8])
np.tensordot(multi_dim_arr1, multi_dim_arr2,
axes=([1, 2], [0, 1])) Element-wise Logistic Sigmoid Function
logistic_sigmoid = 1 / (1 + np.exp(-arr)
Prepared by:
Chushritha Bandaru