Product of an array
Write a NumPy program to calculate the product of an array.
[3, 5, 1]
15
Approach
np.prod.
Solution
import numpy as npprint(np.prod([3, 5, 1]))15
Learn the concept
New to this topic? Read the lesson, then come back.
Online Learning Platform
Python Numpy Practice Problems
Write a NumPy program to calculate the product of an array.
[3, 5, 1]
15
np.prod.import numpy as npprint(np.prod([3, 5, 1]))15
New to this topic? Read the lesson, then come back.
Write a NumPy program to check whether an array is empty.
[4, 8, 0], []
[4 8 0] Array is not empty [] Array is empty
a.size == 0.import numpy as npfor a in [np.array([4, 8, 0]), np.array([])]: print(a, "Array is empty" if a.size == 0 else "Array is not empty")[4 8 0] Array is not empty [] Array is empty
New to this topic? Read the lesson, then come back.
Write a NumPy program to count how often each unique value appears.
[8, 6, 7, 0, 7, 8, 6, 6]
[[0 6 7 8] [1 3 2 2]]
np.unique(..., return_counts=True).import numpy as npvalues, counts = np.unique([8, 6, 7, 0, 7, 8, 6, 6], return_counts=True)print(np.vstack((values, counts)))[[0 6 7 8] [1 3 2 2]]
New to this topic? Read the lesson, then come back.
Write a NumPy program to get the magnitude (length) of a vector.
[3, 5, 1]
5.92
np.linalg.norm.import numpy as npprint(round(float(np.linalg.norm([3, 5, 1])), 2))5.92
New to this topic? Read the lesson, then come back.
Write a NumPy program to remove rows that contain NaN.
[[4, nan, 8], [2, 4, 6]]
[[2. 4. 6.]]
import numpy as npa = np.array([[4, np.nan, 8], [2, 4, 6]])print(a[~np.isnan(a).any(axis=1)])[[2. 4. 6.]]
isnan(a).any(axis=1) is True for rows with a NaN; ~ inverts it.
New to this topic? Read the lesson, then come back.
Write a NumPy program to replace negative values with 1.
[[4, 8, -5], [2, -9, 6]]
[[4 8 1] [2 1 6]]
import numpy as npa = np.array([[4, 8, -5], [2, -9, 6]])a[a < 0] = 1print(a)[[4 8 1] [2 1 6]]
New to this topic? Read the lesson, then come back.
Write a NumPy program to convert an array of floats to integers.
[[6.7, 5.2], [9.4, 2.7]]
[[6 5] [9 2]] [[7 5] [9 3]]
astype(int) truncates; round first if needed.import numpy as npa = np.array([[6.7, 5.2], [9.4, 2.7]])print(a.astype(int))print(np.rint(a).astype(int))[[6 5] [9 2]] [[7 5] [9 3]]
Truncation drops the decimals.
rint rounds to the nearest integer first.
New to this topic? Read the lesson, then come back.
Write a NumPy program to print each column of an array.
[[4, 8, 0], [2, 0, 6]]
Column 1: [4 2] Column 2: [8 0] Column 3: [0 6]
a[:, j] selects column j.import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for j in range(a.shape[1]): print(f"Column {j + 1}:", a[:, j])Column 1: [4 2] Column 2: [8 0] Column 3: [0 6]
New to this topic? Read the lesson, then come back.
Write a NumPy program to print the squares of all elements.
[2, 6, 3, 1]
[ 4 36 9 1]
a ** 2 or np.square.import numpy as npprint(np.square(np.array([2, 6, 3, 1])))[ 4 36 9 1]
New to this topic? Read the lesson, then come back.
Write a NumPy program to print every element of a 2-D array.
[[4, 8, 0], [2, 0, 6]]
4 8 0 2 0 6
np.nditer or ravel().import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for x in np.nditer(a): print(x, end=" ")print()4 8 0 2 0 6
New to this topic? Read the lesson, then come back.