PROGRAM
PROGRAM
import numpy as np
def find_nonzero_indices(arr):
"""
Returns the indices of non-zero elements in the given NumPy array.
Parameters:
arr (numpy.ndarray): Input array.
Returns:
tuple: Indices of non-zero elements.
"""
return np.nonzero(arr)
# Example usage:
arr = np.array([0, 3, 0, 4, 7, 0])
indices = find_nonzero_indices(arr)
print(indices)
print(even_numbers)
def sort_descending(arr):
"""
Sorts a NumPy array in descending order.
Parameters:
arr (numpy.ndarray): Input array.
Returns:
numpy.ndarray: Array sorted in descending order.
"""
return np.sort(arr)[::-1]
# Example usage:
arr = np.array([5, 2, 9, 1, 7])
sorted_arr = sort_descending(arr)
print(sorted_arr)
Parameters:
arr (numpy.ndarray): Input array.
Returns:
float: Standard deviation of the array.
"""
return np.std(arr)
# Example usage:
arr = np.array([4, 8, 6, 5, 3, 7])
std_dev = calculate_std(arr)
print(random_array)
6. Given a NumPy array, write a function to normalize the values to be between 0 and 1.
import numpy as np
def normalize_array(arr):
"""
Normalizes a NumPy array to values between 0 and 1.
Parameters:
arr (numpy.ndarray): Input array.
Returns:
numpy.ndarray: Normalized array.
"""
min_val = np.min(arr)
max_val = np.max(arr)
# Example usage:
arr = np.array([10, 20, 30, 40, 50])
normalized_arr = normalize_array(arr)
print(normalized_arr)