NumPy Python Tips

NumPy Python Tips

DataScienceQ

Python tip:
Create an uninitialized array (contents are arbitrary) for performance.

import numpy as np
empty_array = np.empty((2, 3))

Python tip:
Create an array with the same shape and dtype as another array, but filled with zeros.
import numpy as np
a = np.array([[1, 2], [3, 4]])
zeros_like_a = np.zeros_like(a)

Python tip:
Create an array with logarithmically spaced numbers.
import numpy as np
log_array = np.logspace(0, 2, 5) # 5 numbers from 10^0 to 10^2

Python tip:
Create a 2D array from a function that takes indices as input.
import numpy as np
def my_func(i, j):
return i + j
from_func_array = np.fromfunction(my_func, (3, 3), dtype=int)

Python tip:
Use np.newaxis or None to add a new dimension for broadcasting.
import numpy as np
a = np.arange(3) # shape (3,)
a_col = a[:, np.newaxis] # shape (3, 1)

Python tip:
Keep dimensions when aggregating for easier broadcasting later.
import numpy as np
a = np.arange(12).reshape(3, 4)
row_means = a.mean(axis=1, keepdims=True) # shape (3, 1)

Python tip:
Use the where argument in aggregations to ignore certain values.
import numpy as np
a = np.array([1, 2, 3, -1, 5])
positive_sum = np.sum(a, where=a>0)

Python tip:
Perform bitwise AND operation on integer arrays element-wise.
import numpy as np
a = np.array([1, 2, 3]) # 01, 10, 11
b = np.array([1, 1, 1]) # 01, 01, 01
bitwise_and = np.bitwise_and(a, b) # [1, 0, 1]

Python tip:
Combine multiple boolean conditions for indexing using & (and) and | (or).
import numpy as np
a = np.arange(10)
filtered = a[(a > 2) & (a < 8)]

Python tip:
Use np.take to select elements along an axis using indices.
import numpy as np
a = np.array([4, 3, 5, 7, 6, 8])
indices = [0, 1, 4]
taken = np.take(a, indices)

Python tip:
Use np.put to replace elements at specific indices with new values.
import numpy as np
a = np.arange(5)
np.put(a, [0, 2], [-44, -55])

Python tip:
Create a structured array with named fields (like a database row).
import numpy as np
person_dtype = np.dtype([('name', 'S10'), ('age', 'i4'), ('weight', 'f4')])
people = np.array([('Alice', 25, 55.0), ('Bob', 30, 80.5)], dtype=person_dtype)

Python tip:
Access a column in a structured array by its field name.
import numpy as np
person_dtype = np.dtype([('name', 'S10'), ('age', 'i4')])
people = np.array([('Alice', 25), ('Bob', 30)], dtype=person_dtype)
ages = people['age']

Python tip:
Calculate the median of an array.
import numpy as np
a = np.array([1, 2, 5, 8, 10])
median = np.median(a)

Python tip:
Calculate percentiles of data.
import numpy as np
data = np.arange(1, 101)
p75 = np.percentile(data, 75)

Python tip:
Calculate a weighted average.
import numpy as np
values = np.array([10, 20, 30])
weights = np.array([1, 2, 3])
weighted_avg = np.average(values, weights=weights)

Python tip:
Calculate the correlation coefficient matrix.
import numpy as np
x = np.array([0, 1, 2])
y = np.array([2, 1, 0])
corr_matrix = np.corrcoef(x, y)

Python tip:
Count the occurrences of each value in an array of non-negative integers.
import numpy as np
a = np.array([0, 1, 1, 3, 2, 1, 7])
counts = np.bincount(a)

Python tip:
Find the indices into a sorted array where elements should be inserted to maintain order.
import numpy as np
sorted_array = np.array([10, 20, 30])
indices = np.searchsorted(sorted_array, [-5, 15, 35])

Python tip:
Bin data into discrete intervals.
import numpy as np
data = np.array([1, 5, 12, 25, 33, 48])
bins = np.array([0, 10, 20, 30, 40, 50])
binned_data = np.digitize(data, bins)

Python tip:
Create a masked array to handle missing or invalid values.
import numpy as np
import numpy.ma as ma
a = np.array([1, 2, -1, 4, 5])
masked_a = ma.masked_where(a < 0, a)

Python tip:
Perform calculations on masked arrays, which automatically ignore masked values.
import numpy as np
import numpy.ma as ma
a = np.array([1, 2, -1, 4])
masked_a = ma.masked_equal(a, -1)
mean_val = masked_a.mean() # Calculates mean of [1, 2, 4]

Python tip:
Use np.datetime64 for high-performance date and time operations.
import numpy as np
date = np.datetime64('2024-01-15')

Python tip:
Perform arithmetic with dates using np.timedelta64.
import numpy as np
start_date = np.datetime64('2024-01-01')
one_week = np.timedelta64(7, 'D') # 'D' for days
end_date = start_date + one_week

Python tip:
Extract date components from a datetime array.
import numpy as np
dates = np.arange('2024-01', '2024-02', dtype='datetime64[D]')
# To get years, cast to 'datetime64[Y]'
years = dates.astype('datetime64[Y]')

Python tip:
Calculate the trace of a matrix (sum of diagonal elements).
import numpy as np
a = np.eye(4)
trace = np.trace(a)

Python tip:
Compute the Singular Value Decomposition (SVD) of a matrix.
import numpy as np
A = np.array([[1, 2], [3, 4], [5, 6]])
U, s, Vt = np.linalg.svd(A)

Python tip:
Compute the norm of a vector or matrix.
import numpy as np
v = np.arange(3)
norm_v = np.linalg.norm(v)

Python tip:
Roll array elements along a given axis.
import numpy as np
a = np.arange(10)
rolled_a = np.roll(a, 2)

Python tip:
Pad an array with a constant value.
import numpy as np
a = np.ones((2, 2))
padded_a = np.pad(a, pad_width=1, mode='constant', constant_values=0)

Python tip:
Check if an array is C-contiguous or Fortran-contiguous in memory.
import numpy as np
a = np.zeros((3, 3), order='C')
is_c_contig = a.flags.c_contiguous

Python tip:
Create a view of an array that is guaranteed to be contiguous.
import numpy as np
a = np.arange(9).reshape(3, 3).T # Transpose makes it non-C-contiguous
b = np.ascontiguousarray(a)

Python tip:
Check if an array is a view of another array using the .base attribute.
import numpy as np
a = np.arange(10)
b = a[2:5]
is_view = b.base is a # Returns True

Python tip:
Explicitly broadcast an array to a new shape.
import numpy as np
a = np.array([1, 2, 3])
b = np.broadcast_to(a, (3, 3))

Python tip:
Broadcast multiple arrays against each other.
import numpy as np
a = np.arange(3)[:, np.newaxis] # shape (3, 1)
b = np.arange(4)[np.newaxis, :] # shape (1, 4)
x, y = np.broadcast_arrays(a, b)

Python tip:
Calculate trigonometric functions like sine element-wise.
import numpy as np
angles = np.array([0, np.pi/2, np.pi])
sines = np.sin(angles)

Python tip:
Calculate the floor of each element (largest integer less than or equal to).
import numpy as np
a = np.array([1.1, 2.9, -3.5])
floor_a = np.floor(a)

Python tip:
Find the remainder of division element-wise.
import numpy as np
a = np.array([5, 6, 7])
b = np.array([2, 3, 4])
remainder = np.mod(a, b)

Python tip:
Find the greatest common divisor of two integers.
import numpy as np
gcd = np.gcd(12, 20)

Python tip:
Check for infinity in an array.
import numpy as np
a = np.array([1, np.inf, 3])
is_inf = np.isinf(a)

Python tip:
Generate random numbers from a uniform distribution over a specific range.
import numpy as np
uniform_rand = np.random.uniform(low=10, high=20, size=5)

Python tip:
Generate random numbers from a normal (Gaussian) distribution with a given mean and std dev.
import numpy as np
normal_rand = np.random.normal(loc=100, scale=15, size=(2, 2))

Python tip:
Create a random permutation of a sequence of integers.
import numpy as np
perm = np.random.permutation(10)

Python tip:
Use the modern random number Generator for better statistical properties and reproducibility.
import numpy as np
rng = np.random.default_rng(seed=42)
random_integers = rng.integers(low=0, high=10, size=5)

Python tip:
Compute the one-dimensional discrete Fourier Transform.
import numpy as np
signal = np.array([0, 1, 0, 0])
fft_result = np.fft.fft(signal)

Python tip:
Compute the inverse discrete Fourier Transform.
import numpy as np
freqs = np.array([1.+0.j, 0.-1.j, -1.+0.j, 0.+1.j])
original_signal = np.fft.ifft(freqs)

Python tip:
Use np.ix_ to construct an open mesh from multiple sequences for indexing.
import numpy as np
a = np.arange(16).reshape(4, 4)
indexed = a[np.ix_([0, 3], [1, 2])]

Python tip:
Save multiple arrays into a single compressed .npz file.
import numpy as np
a = np.arange(5)
b = np.arange(5, 10)
np.savez_compressed('my_arrays.npz', array_a=a, array_b=b)

Python tip:
Load arrays from an .npz file.
import numpy as np
data = np.load('my_arrays.npz')
a = data['array_a']

Python tip:
Use np.testing for writing unit tests for numerical code.
import numpy as np
from numpy.testing import assert_almost_equal
# In a test function:
# assert_almost_equal(0.12345, 0.12346, decimal=4)

Python tip:
Find the indices of non-zero elements.
import numpy as np
a = np.array([1, 0, 3, 0, 5])
indices = np.nonzero(a)

Python tip:
Extract a sub-window from a large array without copying data.
from numpy.lib.stride_tricks import as_strided
a = np.arange(20)
# Caution: Advanced and can be unsafe if used incorrectly
window_shape = (10, 5)
window_view = as_strided(a, shape=window_shape, strides=(a.strides[0], a.strides[0]))

Python tip:
Set printing options for arrays to control display output.
import numpy as np
np.set_printoptions(precision=3, suppress=True)
a = np.array([1/3, 2/3, 1.23456])
print(a)

Python tip:
Get the real and imaginary parts of a complex array.
import numpy as np
a = np.array([1+2j, 3+4j])
real_part = a.real
imag_part = a.imag

Python tip:
Create a block matrix from smaller matrices.
import numpy as np
A = np.ones((2, 2))
B = np.zeros((2, 2))
block_matrix = np.block([[A, B], [B, A]])

Python tip:
Convert angles from degrees to radians.
import numpy as np
degrees = np.array([0, 90, 180, 270])
radians = np.deg2rad(degrees)

Python tip:
Perform matrix exponentiation.
import numpy as np
from scipy.linalg import expm # Often better than np.linalg.matrix_power for general case
A = np.array([[1, 2], [3, 4]])
# matrix_power is for integer powers, expm for matrix exponential
exp_A = expm(A)

Python tip:
Calculate the cross product of two vectors.
import numpy as np
a = np.array([1, 0, 0])
b = np.array([0, 1, 0])
cross_prod = np.cross(a, b) # Result is [0, 0, 1]

Python tip:
Use np.squeeze to remove single-dimensional entries from the shape of an array.
import numpy as np
a = np.zeros((1, 3, 1, 4))
squeezed_a = np.squeeze(a) # shape is (3, 4)

Python tip:
Calculate element-wise maximum of two arrays.
import numpy as np
a = np.array([1, 5, 3])
b = np.array([4, 2, 6])
max_vals = np.maximum(a, b)

Python tip:
Compute the sign of each element (-1 for negative, 0 for zero, 1 for positive).
import numpy as np
a = np.array([-5, 0, 2, -100])
signs = np.sign(a)

Python tip:
Check for equality between two arrays, allowing for a tolerance (for floats).
import numpy as np
a = np.array([1.000001])
b = np.array([1.0])
is_close = np.isclose(a, b)

Python tip:
Find the indices that would sort an array along a specified axis.
import numpy as np
a = np.array([[0, 3], [2, 1]])
sorted_indices = np.argsort(a, axis=1)

Python tip:
Create a full matrix from a triangular representation.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 0], [6, 0, 0]])
full_matrix = a + a.T - np.diag(np.diag(a))

Python tip:
Get the upper triangle of an array.
import numpy as np
a = np.arange(9).reshape(3,3)
upper_triangle = np.triu(a)

Python tip:
Get the lower triangle of an array.
import numpy as np
a = np.arange(9).reshape(3,3)
lower_triangle = np.tril(a)

Python tip:
Calculate the exponential function e^x for each element.
import numpy as np
a = np.array([0, 1, 2])
exp_a = np.exp(a)

Python tip:
Calculate the natural logarithm ln(x) for each element.
import numpy as np
a = np.array([1, np.e, 10])
log_a = np.log(a)

Python tip:
Create a Vandermonde matrix.
import numpy as np
x = np.array([1, 2, 3])
vandermonde_matrix = np.vander(x, N=3)

Python tip:
Check if any elements of an array fall within a range.
import numpy as np
a = np.arange(10)
is_in_range = ((a > 2) & (a < 5)).any()

Python tip:
Perform a logical NOT operation on a boolean array.
import numpy as np
a = np.array([True, False, True])
not_a = np.logical_not(a)

Python tip:
Concatenate arrays along a new axis using np.stack.
import numpy as np
a = np.array([1, 2])
b = np.array([3, 4])
stacked = np.stack((a, b), axis=0) # default axis=0

Python tip:
Split an array horizontally.
import numpy as np
a = np.arange(16).reshape(4, 4)
h_split = np.hsplit(a, 2)

Python tip:
Split an array vertically.
import numpy as np
a = np.arange(16).reshape(4, 4)
v_split = np.vsplit(a, 2)

Python tip:
Find the version of NumPy you are using.
import numpy as np
print(np.__version__)

Python tip:
Convert a NumPy array to a standard Python list.
import numpy as np
a = np.array([1, 2, 3])
my_list = a.tolist()

Python tip:
Get information about a NumPy function using np.info.
import numpy as np
np.info(np.linspace)

Python tip:
Calculate the difference between the maximum and minimum values.
import numpy as np
a = np.array([10, 2, 8, 5])
ptp = np.ptp(a) # "peak to peak"

Python tip:
Change the data type of an array, creating a copy.
import numpy as np
a = np.array([1, 2, 3])
float_a = a.astype(float)

Python tip:
Disassemble a N-dimensional array into a tuple of 1-D arrays.
import numpy as np
a = np.arange(4).reshape(2, 2)
array_tuple = np.dsplit(a, 2)

Python tip:
Generate a sequence of numbers by specifying the start, end, and number of items.
import numpy as np
geom_seq = np.geomspace(1, 1000, 4) # [1., 10., 100., 1000.]

Python tip:
Use np.add.at to perform unbuffered in-place addition at specified indices.
import numpy as np
a = np.zeros(5)
indices = np.array([0, 1, 0, 2, 1])
np.add.at(a, indices, 1) # a is now [2., 2., 1., 0., 0.]

Python tip:
Find common elements between two arrays, returning the indices for each.
import numpy as np
a = np.array([1, 2, 3, 2, 4, 1])
b = np.array([3, 4, 5, 6])
common, a_idx, b_idx = np.intersect1d(a, b, return_indices=True)

Python tip:
Calculate the Cartesian product of input arrays.
import numpy as np
x = [1, 2]
y = [3, 4]
grid = np.array(np.meshgrid(x, y)).T.reshape(-1, 2)

Python tip:
Use Ellipsis (...) to slice higher-dimensional arrays concisely.
import numpy as np
a = np.zeros((3, 4, 5, 6))
# Select all elements from first two dims, then index 2, then all
slice_a = a[..., 2, :]

Python tip:
Create a read-only view of an array to prevent accidental modification.
import numpy as np
a = np.arange(5)
a.flags.writeable = False
# a[0] = 99 # This will raise a ValueError

Python tip:
Use the polynomial class for a more object-oriented approach to polynomials.
from numpy.polynomial import Polynomial
p = Polynomial([1, 2, 3]) # Represents 1 + 2x + 3x^2
val = p(2)

Python tip:
Find the roots of a polynomial.
import numpy as np
# Roots of x^2 - 1
coeffs = [1, 0, -1]
roots = np.roots(coeffs)

Python tip:
Calculate the inverse cosine (arccosine) element-wise.
import numpy as np
x = np.array([-1, 0, 1])
arccos_x = np.arccos(x)

Python tip:
Get memory usage of an array in bytes.
import numpy as np
a = np.zeros((100, 100), dtype=np.float64)
memory = a.nbytes

#NumPy #Python #DataScience #MachineLearning #ScientificComputing #Vectorization #PythonTips #Programming #Coding #ArrayProgramming

Report Page