1. Write a function to find the indices of non-zero elements in a NumPy array.
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)
2. Create a NumPy array containing the first 10 even numbers.
import numpy as np
# Generate array of first 10 even numbers
even_numbers = np.arange(2, 22, 2)
print(even_numbers)
3. Given a NumPy array, write a function to sort it in descending order.
import numpy as np
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)
4. Write a function to calculate the standard deviation of a NumPy array.
import numpy as np
def calculate_std(arr):
"""
Calculates the standard deviation of a NumPy array.
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(f"Standard Deviation: {std_dev:.2f}")
5. Create a 5x5 NumPy array with random floats between 0 and 1.
import numpy as np
# Creating a 5x5 array with random floats between 0 and 1
random_array = np.random.rand(5, 5)
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)
# Avoid division by zero if all values in array are the same
if min_val == max_val:
return np.zeros_like(arr)
normalized = (arr - min_val) / (max_val - min_val)
return normalized
# Example usage:
arr = np.array([10, 20, 30, 40, 50])
normalized_arr = normalize_array(arr)
print(normalized_arr)
7. Write a function to find the unique elements in a NumPy array.
8. Given a NumPy array, write a function to calculate the cumulative sum along the rows.
9. Create a NumPy array containing 10 equally spaced values between 0 and 1.
10. Write a function to compute the element-wise square root of a NumPy array.
11. Generate a 3x3 NumPy array with random integers between -10 and 10.
12. Given a NumPy array, write a function to calculate the median of its values.
13. Create a 4x4 NumPy array with consecutive integers starting from 1 and reshape it into
a 2x8 array. c. Write a function to calculate the absolute difference between two NumPy
arrays.
14. Generate a random NumPy array of size 3x3 and replace all negative values with 0.
15. Given a NumPy array, write a function to calculate the cumulative product along the
columns.
16. Create a NumPy array containing the first 10 Fibonacci numbers.
17. Write a function to calculate the exponential of each element in a NumPy array.
18. Given two NumPy arrays, write a function to find the common elements between them.
19. Generate a 5x5 NumPy array with random integers and find the indices of the
maximum value.
20. Write a function to calculate the Pearson correlation coefficient between two NumPy
arrays.
21. You are given a 2D array representing a matrix. Write a Python function using NumPy
that takes this matrix as input and returns the sum of the diagonal elements.