Numpy Flashcards
Create a numpy array with numbers from 0-9
import numpy as np
array = np.arange(10)
Create a 3x3 NumPy array filled with zeros.
import numpy as np
array = np.zeros((3, 3))
Create a 3x3 NumPy array filled with random values.
import numpy as np
array = np.random.rand(3, 3)
Create a NumPy array containing all even integers between 0 and 20.
import numpy as np
array = np.linspace(0, 1, 10)
Create a NumPy array containing 10 equally spaced values between 0 and 1.
import numpy as np
array = np.linspace(0, 1, 10)
Reshape a 1D NumPy array into a 2D array with 2 rows.
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
transposed_array = array.T
Multiply two NumPy arrays element-wise.
import numpy as np
array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])
result = array1 * array2
Concatenate two NumPy arrays horizontally.
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
concatenated_array = np.concatenate((array1, array2))
Perform element-wise comparison between two NumPy arrays.
import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([2, 2, 3])
comparison = np.equal(array1, array2)
Compute the Euclidean distance between two points represented by NumPy arrays.
import numpy as np
point1 = np.array([1, 2, 3])
point2 = np.array([4, 5, 6])
euclidean_distance = np.linalg.norm(point1 - point2)
Find the unique elements in a NumPy array.
import numpy as np
array = np.array([1, 2, 2, 3, 3, 4, 4])
unique_values = np.unique(array)
Reverse the order of elements in a NumPy array.
import numpy as np
array = np.arange(10)
reversed_array = np.flip(array)
Transpose a 2D NumPy array.
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
transposed_array = array.T
Compute the exponential of each element in a NumPy array.
import numpy as np
array = np.array([1, 2, 3])
exp_array = np.exp(array)