Python / Data Science Essentials Interview Questions
What are the most commonly used NumPy mathematical functions in data science?
NumPy ships a comprehensive set of universal functions (ufuncs) — compiled, vectorised operations that apply element-wise across the full array without Python loops. Knowing these avoids writing slow manual loops for standard computations.
import numpy as np a = np.array([1.0, 4.0, 9.0, 16.0, 25.0]) # Element-wise math np.sqrt(a) # [1. 2. 3. 4. 5.] np.log(a) # natural log np.log2(a) # base-2 log np.log10(a) # base-10 log np.exp(a) # e^x np.abs(np.array([-3, 4, -1])) # [3 4 1] # Aggregation a.sum() # 55.0 a.mean() # 11.0 a.std() # standard deviation a.var() # variance a.min(); a.max() # extremes a.argmin(); a.argmax() # INDEX of min/max np.median(a) # 9.0 np.percentile(a, 75) # 75th percentile # Linear algebra A = np.array([[1, 2], [3, 4]]) B = np.array([[5, 6], [7, 8]]) np.dot(A, B) # matrix multiplication (also A @ B in Python 3.5+) np.linalg.inv(A) # matrix inverse np.linalg.det(A) # determinant vals, vecs = np.linalg.eig(A) # eigenvalues and eigenvectors # Sorting unsorted = np.array([3, 1, 4, 1, 5]) np.sort(unsorted) # returns sorted copy: [1 1 3 4 5] np.argsort(unsorted) # indices that would sort: [1 3 0 2 4]
More Related questions...