Answer:
You can create a structured array using the np.array function with a list of tuples and a structured data type.
import numpy as np
data = [(1, 'Alice', 25), (2, 'Bob', 30), (3, 'Charlie', 35)]
dtype = [('id', 'i4'), ('name', 'U10'), ('age', 'i4')]
structured_array = np.array(data, dtype=dtype)
print(structured_array)
# Output: [(1, 'Alice', 25) (2, 'Bob', 30) (3, 'Charlie', 35)]Answer:
You can create a boolean mask by applying a condition directly to the array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mask = arr > 3
print(mask)
# Output: [False False False True True]Answer:
You can use the boolean mask to filter the array by indexing it with the mask.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
mask = arr > 3
filtered_arr = arr[mask]
print(filtered_arr)
# Output: [4 5]Answer:
You can compute the dot product using the np.dot function.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
dot_product = np.dot(a, b)
print(dot_product)
# Output: 32Answer:
You can compute the cross product using the np.cross function.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
cross_product = np.cross(a, b)
print(cross_product)
# Output: [-3 6 -3]Answer:
You can find the indices of the maximum value using the np.argmax function.
import numpy as np
arr = np.array([1, 3, 7, 1, 2])
max_index = np.argmax(arr)
print(max_index)
# Output: 2Answer:
You can find the indices of the minimum value using the np.argmin function.
import numpy as np
arr = np.array([1, 3, 7, 1, 2])
min_index = np.argmin(arr)
print(min_index)
# Output: 0Answer:
You can calculate the cumulative sum using the np.cumsum function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
cumsum = np.cumsum(arr)
print(cumsum)
# Output: [ 1 3 6 10 15]Answer:
You can calculate the cumulative product using the np.cumprod function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
cumprod = np.cumprod(arr)
print(cumprod)
# Output: [ 1 2 6 24 120]Answer:
You can compute the eigenvalues and eigenvectors using the np.linalg.eig function.
import numpy as np
matrix = np.array([[4, -2], [1, 1]])
eigenvalues, eigenvectors = np.linalg.eig(matrix)
print(eigenvalues)
# Output: [3. 2.]
print(eigenvectors)
# Output: [[ 0.89442719 0.70710678]
# [ 0.4472136 -0.70710678]]Answer:
You can compute the inverse using the np.linalg.inv function.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
inverse_matrix = np.linalg.inv(matrix)
print(inverse_matrix)
# Output: [[-2. 1. ]
# [ 1.5 -0.5]]Answer:
You can compute the determinant using the np.linalg.det function.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
determinant = np.linalg.det(matrix)
print(determinant)
# Output: -2.0000000000000004Answer:
You can solve a system of linear equations using the np.linalg.solve function.
import numpy as np
A = np.array([[3, 1], [1, 2]])
B = np.array([9, 8])
solution = np.linalg.solve(A, B)
print(solution)
# Output: [2. 3.]Answer:
You can perform element-wise multiplication using the * operator.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
result = a * b
print(result)
# Output: [ 4 10 18]Answer:
You can perform matrix multiplication using the np.matmul function.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
result = np.matmul(a, b)
print(result)
# Output: [[19 22]
# [43 50]]Answer:
You can compute the QR decomposition using the np.linalg.qr function.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
Q, R = np.linalg.qr(matrix)
print(Q)
# Output: [[-0.31622777 -0.9486833 ]
# [-0.9486833 0.31622777]]
print(R)
# Output: [[-3.16227766 -4.42718872]
# [ 0. 0.63245553]]Answer:
You can compute the SVD using the np.linalg.svd function.
import numpy as np
matrix = np.array([[1, 2], [3, 4], [5, 6]])
U, S, V = np.linalg.svd(matrix)
print(U)
# Output: [[-0.2298477 0.88346102 0.40824829]
# [-0.52474482 0.24078249 -0.81649658]
# [-0.81964194 -0.40189603 0.40824829]]
print(S)
# Output: [9.52551809 0.51430058]
print(V)
# Output: [[-0.61962948 -0.78489445]
# [-0.78489445 0.61962948]]Answer:
You can generate a random matrix using the np.random.randn function.
import numpy as np
random_matrix = np.random.randn(3, 3)
print(random_matrix)
# Output: Random 3x3 matrix with values from normal distributionAnswer:
You can shuffle the elements using the np.random.shuffle function.
import numpy as np
arr = np.array([1, 2, 3, 4,
5])
np.random.shuffle(arr)
print(arr)
# Output: Shuffled array, e.g., [3 1 4 5 2]Answer:
You can create a random permutation using the np.random.permutation function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
permuted_arr = np.random.permutation(arr)
print(permuted_arr)
# Output: Permuted array, e.g., [3 1 4 5 2]Answer:
You can generate random integers using the np.random.randint function.
import numpy as np
random_integers = np.random.randint(0, 10, size=5)
print(random_integers)
# Output: Random integers within range [0, 10)Answer:
You can create such an array using the np.zeros function.
import numpy as np
zero_array = np.zeros((3, 4))
print(zero_array)
# Output: 3x4 array of zerosAnswer:
You can create such an array using the np.ones function.
import numpy as np
one_array = np.ones((2, 3))
print(one_array)
# Output: 2x3 array of onesAnswer:
You can create an identity matrix using the np.eye function.
import numpy as np
identity_matrix = np.eye(3)
print(identity_matrix)
# Output: 3x3 identity matrixAnswer:
You can create an array with a range of values using the np.arange function.
import numpy as np
range_array = np.arange(0, 10, 2)
print(range_array)
# Output: [0 2 4 6 8]Answer:
You can create such an array using the np.linspace function.
import numpy as np
linspace_array = np.linspace(0, 1, 5)
print(linspace_array)
# Output: [0. 0.25 0.5 0.75 1. ]Answer:
You can concatenate two arrays using the np.concatenate function.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
concatenated_array = np.concatenate((a, b), axis=0)
print(concatenated_array)
# Output: [[1 2]
# [3 4]
# [5 6]]Answer:
You can stack arrays vertically using the np.vstack function.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked_array = np.vstack((a, b))
print(stacked_array)
# Output: [[1 2 3]
# [4 5 6]]Answer:
You can stack arrays horizontally using the np.hstack function.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
stacked_array = np.hstack((a, b))
print(stacked_array)
# Output: [1 2 3 4 5 6]Answer:
You can split an array using the np.split function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
split_array = np.split(arr, 3)
print(split_array)
# Output: [array([1, 2]), array([3, 4]), array([5, 6])]Answer:
You can compute the outer product using the np.outer function.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
outer_product = np.outer(a, b)
print(outer_product)
# Output: [[ 4 5 6]
# [ 8 10 12]
# [12 15 18]]Answer:
You can compute the inner product using the np.inner function.
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
inner_product = np.inner(a, b)
print(inner_product)
# Output: 32Answer:
You can compute the Kronecker product using the np.kron function.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[0, 5], [6, 7]])
kronecker_product = np.kron(a, b)
print(kronecker_product)
# Output: [[ 0 5 0 10]
# [ 6 7 12 14]
# [ 0 15 0 20]
# [18 21 24 28]]Answer:
You can compute the matrix rank using the np.linalg.matrix_rank function.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
rank = np.linalg.matrix_rank(matrix)
print(rank)
# Output: 2Answer:
You can compute the Frobenius norm using the np.linalg.norm function with the ord='fro' argument.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
frobenius_norm = np.linalg.norm(matrix, ord='fro')
print(frobenius_norm)
# Output: 5.477225575051661Answer:
You can compute the condition number using the np.linalg.cond function.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
condition_number = np.linalg.cond(matrix)
print(condition_number)
# Output: 14.933034373659268Answer:
You can compute the pseudoinverse using the np.linalg.pinv function.
import numpy as np
matrix = np.array([[1, 2], [3, 4]])
pseudoinverse = np.linalg.pinv(matrix)
print(pseudoinverse)
# Output: [[-2. 1. ]
# [ 1.5 -0.5]]Answer:
You can compute the covariance matrix using the np.cov function.
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
cov_matrix = np.cov(data, rowvar=False)
print(cov_matrix)
# Output: [[9. 9. 9. ]
# [9. 9. 9. ]
# [9. 9. 9. ]]Answer:
You can compute the correlation matrix using the np.corrcoef function.
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
corr_matrix = np.corrcoef(data, rowvar=False)
print(corr_matrix)
# Output: [[1
. 1. 1.]
# [1. 1. 1.]
# [1. 1. 1.]]Answer:
You can compute the standard deviation using the np.std function.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
std_dev = np.std(arr, axis=0)
print(std_dev)
# Output: [2.44948974 2.44948974 2.44948974]Answer:
You can compute the variance using the np.var function.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
variance = np.var(arr, axis=0)
print(variance)
# Output: [6. 6. 6.]Answer:
You can compute the median using the np.median function.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
median = np.median(arr, axis=0)
print(median)
# Output: [4. 5. 6.]Answer:
You can compute the mode using the stats.mode function from the scipy library.
import numpy as np
from scipy import stats
arr = np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
mode = stats.mode(arr)
print(mode)
# Output: ModeResult(mode=array([4]), count=array([4]))Answer:
You can generate a 2D grid of coordinates using the np.meshgrid function.
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5])
X, Y = np.meshgrid(x, y)
print(X)
# Output: [[1 2 3]
# [1 2 3]]
print(Y)
# Output: [[4 4 4]
# [5 5 5]]Answer:
You can interpolate missing values using the np.interp function.
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, np.nan, 4, np.nan, 16, 25])
nans, x_ = np.isnan(y), lambda z: z.nonzero()[0]
y[nans] = np.interp(x(nans), x_(~nans), y[~nans])
print(y)
# Output: [ 0. 2. 4. 10. 16. 25.]Answer:
You can perform linear regression using the np.polyfit function.
import numpy as np
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 2, 4, 6, 8, 10])
coefficients = np.polyfit(x, y, 1)
print(coefficients)
# Output: [2.00000000e+00 4.02163285e-15]Answer:
You can create a sliding window view using the np.lib.stride_tricks.sliding_window_view function.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
window_size = 3
sliding_windows = np.lib.stride_tricks.sliding_window_view(arr, window_size)
print(sliding_windows)
# Output: [[1 2 3]
# [2 3 4]
# [3 4 5]]Answer:
You can find the unique rows using the np.unique function with the axis=0 argument.
import numpy as np
arr = np.array([[1, 2], [3, 4], [1, 2], [5, 6]])
unique_rows = np.unique(arr, axis=0)
print(unique_rows)
# Output: [[1 2]
# [3 4]
# [5 6]]Answer:
You can sort a 2D array by a specific column using the np.argsort function.
import numpy as np
arr = np.array([[3, 2], [1, 4], [2, 1]])
sorted_arr = arr[arr[:, 0].argsort()]
print(sorted_arr)
# Output: [[1 4]
# [2 1]
# [3 2]]Answer:
You can create a boolean array by applying a condition directly to the array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
bool_arr = arr > 3
print(bool_arr)
# Output: [False False False True True]If you found this repository helpful, please give it a star!
Follow me on:
Stay updated with my latest content and projects!