Find null value’s index in NumPy array – Python

Numerical Python, is a Python library used for efficient numerical and scientific computing. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to perform operations on these arrays. NumPy is widely used for data analysis, machine learning, and scientific research due to its performance and ease of use.

Locating NaN Indices in a NumPy Array Using Python

With Python, we can efficiently locate null values in a NumPy array using built-in functions like argwhere. However, in this example, we use a more general approach to identify the indices of null values.

Importing the numpy library:

You import the numpy library, which allows you to handle arrays and perform numerical operations.

Defining the function user_input():

You define a function named user_input that prompts the user to enter the dimensions and elements of an array.

Getting the array dimensions from the user:

You prompt the user to enter the dimensions of the array. You split the input string into a list of strings and convert them to integers representing the number of rows and columns.

Getting the array elements from the user:

You prompt the user to enter the elements of the array as a space-separated string. You split this string into a list of strings.  We must iterate over this list, convert each string to a float, and append it to an elements list. If a string equals ‘nan’ (case-insensitive), you append np.nan instead.

Creating and reshaping the array:

You create a numpy array from the elements list and reshape it to the specified dimensions (rows and columns). You return the array.

Calling the function and storing the result:

You call the user_input function and store the returned array in a variable.

Finding indices of NaN values:

You initialize an empty list for NaN indices. You iterate over each element in the array using nested loops. If an element is NaN, you append its indices (row and column) as a list to the NaN indices list.

Printing the array and indices of NaN values:

You print the array and the list of indices where NaN values are found.By following these steps, you allow the user to input array dimensions and elements, create a numpy array from the input, and identify and print the indices of NaN values in the array.

import numpy as np

def user_input():
    dims = input("Enter the dimensions of the array").split()
    rows = int(dims[0])
    colu = int(dims[1])
    
    elements = input("Enter the elements of the array").split()
    elements_list = []
    for element in elements:
        if element.lower() == 'nan':
            elements_list.append(np.nan)
        else:
            elements_list.append(float(element))
    
    array = np.array(elements_list).reshape(rows, colu)
    return array

array = user_input()

nan_indices = []
for i in range(array.shape[0]):
    for j in range(array.shape[1]):
        if np.isnan(array[i][j]):
            nan_indices.append([i, j])

print("Array:")
print(array)
print("Indices of NaN values:", nan_indices)

 

INPUT:
Enter the dimensions of the array 2 3
Enter the elements of the array 1 5 3 nan 8 nan 
OUTPUT:
Array:
[[ 1. 5. 3.]
[nan 8. nan]]
Indices of NaN values: [[1, 0], [1, 2]]

Video:


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top