NumPy Coding Application Questions Flashcards
(15 cards)
Create a 1D NumPy array with numbers from 0 to 9 using np.arange().
import numpy as np
arr = np.arange(0, 10)
print(arr)
Create a 2x3 array filled with zeros using np.zeros().
import numpy as np
arr = np.zeros((2, 3))
print(arr)
Generate a 2x2 array with random integers between 1 and 10 using np.random.randint().
import numpy as np
arr = np.random.randint(1, 11, size=(2, 2))
print(arr)
Given an array arr = np.array([10, 20, 30, 40]), print its shape.
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr.shape)
Slice the array arr = np.array([1, 2, 3, 4, 5]) to get elements from index 1 to 3 (exclusive).
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr[1:3])
Calculate the mean of the array arr = np.array([5, 10, 15, 20]).
import numpy as np
arr = np.array([5, 10, 15, 20])
print(np.mean(arr))
Find the maximum value in the array arr = np.array([3, 9, 2, 9, 5]).
import numpy as np
arr = np.array([3, 9, 2, 9, 5])
print(np.max(arr))
Conditionally select elements greater than 10 from arr = np.array([5, 15, 20, 8, 12]).
import numpy as np
arr = np.array([5, 15, 20, 8, 12])
print(arr[arr > 10])
Concatenate two arrays arr1 = np.array([1, 2]) and arr2 = np.array([3, 4]).
import numpy as np
arr1 = np.array([1, 2])
arr2 = np.array([3, 4])
concatenated = np.concatenate((arr1, arr2))
print(concatenated)
Reshape the array arr = np.array([1, 2, 3, 4, 5, 6]) into a 2x3 array.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped = arr.reshape(2, 3)
print(reshaped)
Sort the array arr = np.array([10, 5, 3, 8, 2]) in ascending order.
import numpy as np
arr = np.array([10, 5, 3, 8, 2])
sorted_arr = np.sort(arr)
print(sorted_arr)
Create an array with 5 evenly spaced numbers from 0 to 1 using np.linspace().
import numpy as np
arr = np.linspace(0, 1, 5)
print(arr)
Access the element at row 1, column 2 in the 2D array arr = np.array([[1, 2, 3], [4, 5, 6]]).
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr[1, 2])
Calculate the standard deviation of the array arr = np.array([1, 2, 3, 4, 5]).
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(np.std(arr))
Stack two arrays arr1 = np.array([1, 2, 3]) and arr2 = np.array([4, 5, 6]) vertically using np.vstack().
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
stacked = np.vstack((arr1, arr2))
print(stacked)