Data science NumPy Interview Question
1) Create a 1D array of numbers from 1 to 10.
import numpy as np
arr = np.arange(1, 11)
print(arr)
2) Create a 3x3 array with all elements set to 1.
arr = np.ones((3, 3))
print(arr)
3) Create a 4x4 matrix with numbers from 1 to 16.
arr = np.arange(1, 17).reshape(4, 4)
print(arr)
4) Extract all even numbers from an array.
arr = np.array([1, 2, 3, 4, 5, 6])
even_numbers = arr[arr % 2 == 0]
print(even_numbers)
5) Replace all odd numbers in an array with -1.
arr = np.array([1, 2, 3, 4, 5])
arr[arr % 2 != 0] = -1
print(arr)
6) Create a 5x5 matrix with random integers between 10 and 50.
arr = np.random.randint(10, 50, (5, 5))
print(arr)
7) Find the sum of all elements in a 2D array.
arr = np.array([[1, 2], [3, 4]])
print(arr.sum())
8) Find the row-wise and column-wise mean of a 3x3 array.
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Row-wise mean:", arr.mean(axis=1))
print("Column-wise mean:", arr.mean(axis=0))
9) Flatten a multi-dimensional array into a 1D array.
arr = np.array([[1, 2], [3, 4]])
flat_arr = arr.flatten()
print(flat_arr)
10) Reverse the rows and columns of a 2D array.
arr = np.array([[1, 2, 3], [4, 5, 6]])
reversed_arr = arr[::-1, ::-1]
print(reversed_arr)
11) Stack two arrays vertically and horizontally.
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print("Vertical stack:\n", np.vstack((arr1, arr2)))
print("Horizontal stack:\n", np.hstack((arr1, arr2)))
12) Compute the dot product of two 2D arrays.
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
dot_product = np.dot(arr1, arr2)
print(dot_product)
13) Calculate the determinant of a matrix.
from numpy.linalg import det
matrix = np.array([[1, 2], [3, 4]])
determinant = det(matrix)
print(determinant)
14) Find the unique elements and their counts in an array.
arr = np.array([1, 2, 2, 3, 3, 3])
unique, counts = np.unique(arr, return_counts=True)
print("Unique elements:", unique)
print("Counts:", counts)
15) Normalize a 1D array (scale to range 0-1).
arr = np.array([1, 2, 3, 4, 5])
normalized = (arr - arr.min()) / (arr.max() - arr.min())
print(normalized)
16) Generate an array of 10 linearly spaced numbers between 0 and 1.
arr = np.linspace(0, 1, 10)
print(arr)
17) Replace all NaN values in an array with 0.
arr = np.array([1, np.nan, 3, np.nan, 5])
arr = np.nan_to_num(arr)
print(arr)
18) Sort a 2D array by its second column.
arr = np.array([[3, 2], [1, 4], [2, 1]])
sorted_arr = arr[arr[:, 1].argsort()]
print(sorted_arr)
19) Find the cumulative sum of a 1D array.
arr = np.array([1, 2, 3, 4])
cumsum = arr.cumsum()
print(cumsum)
20) Broadcast a smaller array onto a larger one.
arr1 = np.array([[1, 2, 3]])
arr2 = np.array([[1], [2], [3]])
result = arr1 + arr2
print(result)
21) Perform element-wise multiplication of two 3D arrays.
arr1 = np.random.rand(2, 2, 2)
arr2 = np.random.rand(2, 2, 2)
result = arr1 * arr2
print(result)
22) Find the eigenvalues and eigenvectors of a matrix.
from numpy.linalg import eig
matrix = np.array([[4, 2], [1, 3]])
eigenvalues, eigenvectors = eig(matrix)
print("Eigenvalues:", eigenvalues)
print("Eigenvectors:\n", eigenvectors)
23) Extract the diagonal elements of a matrix.
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
diagonal = np.diag(matrix)
print(diagonal)
24) Replace the diagonal elements of a matrix with a specific value.
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.fill_diagonal(matrix, 0)
print(matrix)
25) Split a 1D array into 3 equal parts.
arr = np.arange(1, 10)
split_arr = np.array_split(arr, 3)
print(split_arr)
26) Find the inverse of a square matrix.
from numpy.linalg import inv
matrix = np.array([[1, 2], [3, 4]])
inverse = inv(matrix)
print(inverse)
27) Compute the rank of a matrix.
from numpy.linalg import matrix_rank
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rank = matrix_rank(matrix)
print(rank)
28) Perform matrix multiplication without using np.dot.
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
result = arr1 @ arr2
print(result)
29) Generate a Toeplitz matrix from a given array.
from scipy.linalg import toeplitz
arr = np.array([1, 2, 3, 4])
toeplitz_matrix = toeplitz(arr)
print(toeplitz_matrix)
30) Create a block matrix by stacking smaller matrices.
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
block_matrix = np.block([[A, B], [B, A]])
print(block_matrix)
31) Perform Singular Value Decomposition (SVD) of a matrix.
from numpy.linalg import svd
matrix = np.array([[1, 2], [3, 4], [5, 6]])
U, S, Vt = svd(matrix)
print("U:\n", U)
print("Singular values:", S)
print("Vt:\n", Vt)
32) Solve a system of linear equations.
from numpy.linalg import solve
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = solve(A, b)
print(x)
33) Generate a Hilbert matrix of size 4x4.
n=4
hilbert_matrix = 1.0 / (np.arange(1, n+1)[:, None] + np.arange(n))
print(hilbert_matrix)
34) Compute the Frobenius norm of a matrix.
from numpy.linalg import norm
matrix = np.array([[1, 2], [3, 4]])
frobenius_norm = norm(matrix, 'fro')
print(frobenius_norm)
35) Find the nearest value to a given number in an array.
arr = np.array([10, 20, 30, 40, 50])
target = 33
nearest = arr[np.abs(arr - target).argmin()]
print(nearest)
36) Remove duplicate rows from a 2D array.
arr = np.array([[1, 2], [3, 4], [1, 2], [5, 6]])
unique_rows = np.unique(arr, axis=0)
print(unique_rows)
37) Pad an array with a specific value.
arr = np.array([1, 2, 3])
padded_arr = np.pad(arr, pad_width=2, mode='constant', constant_values=0)
print(padded_arr)
38) Find the indices of non-zero elements in a NumPy array.
arr = np.array([0, 1, 2, 0, 3, 0])
indices = np.nonzero(arr)
print(indices)
39) Compute the rolling average of an array with a window size of 3.
arr = np.array([1, 2, 3, 4, 5, 6])
rolling_avg = np.convolve(arr, np.ones(3)/3, mode='valid')
print(rolling_avg)
40) Replace all values in an array greater than a threshold with a specified value.
arr = np.array([10, 20, 30, 40])
threshold = 25
arr[arr > threshold] = -1
print(arr)
41) Split a 2D array into multiple sub-arrays along rows.
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
split_arrs = np.split(arr, 2, axis=0)
print(split_arrs)
42) Broadcast a scalar value over an array conditionally.
arr = np.array([1, 2, 3, 4])
arr[arr % 2 == 0] += 5
print(arr)
43) Compute the covariance matrix of two arrays.
x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 6, 8])
covariance = np.cov(x, y)
print(covariance)
44) Identify rows in a 2D array with at least one NaN value.
arr = np.array([[1, 2, np.nan], [4, 5, 6], [np.nan, 8, 9]])
rows_with_nan = np.any(np.isnan(arr), axis=1)
print(np.where(rows_with_nan)[0])
45) Count the frequency of each unique value in a 1D array.
arr = np.array([1, 2, 2, 3, 3, 3, 4])
unique, counts = np.unique(arr, return_counts=True)
frequency = dict(zip(unique, counts))
print(frequency)
46) Efficiently Compute the Determinant of a Large Matrix Without Looping.
from scipy.linalg import lu
matrix = np.random.rand(5, 5) # Replace 5 with larger dimensions for more complexity
P, L, U = lu(matrix)
determinant = np.prod(np.diag(U)) * np.linalg.det(P)
print(determinant)
47) Optimize Memory Usage in a Sliding Window Operation.
arr = np.random.rand(10_000)
window_size = 10
sliding_sums = np.convolve(arr, np.ones(window_size, dtype=arr.dtype), mode='valid')
print(sliding_sums)
48) Find the Most Frequent Subarray of Length kkk.
arr = np.array([1, 2, 3, 1, 2, 3, 1, 2, 4])
k=3
subarrays = np.lib.stride_tricks.sliding_window_view(arr, k)
unique, counts = np.unique(subarrays, axis=0, return_counts=True)
most_frequent = unique[np.argmax(counts)]
print("Most frequent subarray:", most_frequent)
49) Efficient Sparse Matrix Representation Using NumPy.
matrix = np.array([[0, 0, 3], [4, 0, 0], [0, 5, 6]])
row_indices = np.where(matrix != 0)[0]
col_indices = np.where(matrix != 0)[1]
data = matrix[matrix != 0]
print("CSR Representation:")
print("Data:", data)
print("Row indices:", row_indices)
print("Column indices:", col_indices)
50) Solve a Quadratic Optimization Problem.
from numpy.linalg import solve
Q = np.array([[2, 0], [0, 4]]) # Symmetric positive-definite matrix
c = np.array([-1, -2])
x = solve(Q, -c)
print("Optimal solution:", x)