You can initialize a NumPy array using the following functions:
np.zeros(): Creates an array of zeros.
np.ones(): Creates an array of ones.
np.arange(): Creates an array with evenly spaced values.
np.linspace(): Creates an array with a specified number of evenly spaced values.
You can compute statistical values like mean, median, and standard deviation using NumPy functions:
The shape of a NumPy array is a tuple that represents the number of elements in each dimension. For example, a 2D array with 3 rows and
4 columns will have a shape of (3, 4).
You can reshape a NumPy array using the reshape() function. This allows you to change the dimensions of the array without changing its
data.
Broadcasting is a set of rules that NumPy follows to perform element-wise operations on arrays of different shapes. It automatically
expands the smaller array to match the shape of the larger array during the operation.
NumPy arrays offer better performance for numerical operations, as they are implemented in C and optimized for fast operations. They
are more memory-efficient, support vectorized operations, and allow for advanced indexing and slicing.
NumPy supports element-wise operations, which means you can perform mathematical operations (e.g., addition, subtraction,
multiplication, etc.) directly on arrays.
Q 77. What is broadcasting in NumPy?
Q 76. How do you reshape a NumPy array?
Q 75. What is the shape of a NumPy array?
Q 78. What are some common ways to initialize a NumPy array?
Q 79. How do you perform element-wise operations on NumPy arrays?
Q 80. How do you compute the mean, median, and standard deviation of a NumPy array?
arr = np.array([1, 2, 3, 4, 5])
mean = np.mean(arr) # Mean
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # Output: (2, 3)
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6])
result = arr1 + arr2 # Element-wise addition
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3) # Reshapes to 2 rows, 3 columns
arr1 = np.array([1, 2, 3])
arr2 = np.array([1])
result = arr1 + arr2 # arr2 is broadcasted to match the shape of arr1
np.zeros((2, 3))
np.ones((2, 3))
np.arange(0, 10, 2)
# 2x3 array of zeros
# 2x3 array of ones
# Array with values from 0 to 10 with a step of 2
np.linspace(0, 10, 5) # Array with 5 evenly spaced values between 0 and 10