Python | Filter out integers from float numpy array Last Updated : 06 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Given a numpy array, the task is to filter out integers from an array containing float and integers. Let's see few methods to solve a given task. Method #1 : Using astype(int) Python3 # Python code to demonstrate # filtering integers from numpy array # containing integers and float import numpy as np # initialising array ini_array = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0]) # printing initial array print ("initial array : ", str(ini_array)) # filtering integers result = ini_array[ini_array != ini_array.astype(int)] # printing resultant print ("final array", result) Method #2: Using np.equal() and np.mod() Python3 # Python code to demonstrate # filtering integers from numpy array # containing integers and float import numpy as np # initialising array ini_array = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0]) # printing initial array print ("initial array : ", str(ini_array)) # filtering integers result = ini_array[~np.equal(np.mod(ini_array, 1), 0)] # printing resultant print ("final array : ", str(result)) Method #3: Using np.isclose() Python3 # Python code to demonstrate # filtering integers from numpy array # containing integers and float import numpy as np # initialising array ini_array = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0]) # printing initial array print ("initial array : ", str(ini_array)) # filtering integers mask = np.isclose(ini_array, ini_array.astype(int)) result = ini_array[~mask] # printing resultant print ("final array : ", str(result)) Method #4 : Using round() Approach is to use the numpy.isreal() function and apply additional filtering to select only those elements that are not equal to their integer counterparts. Python3 import numpy as np # initializing array ini_array = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0]) # printing initial array print("initial array : ", str(ini_array)) # filtering integers mask = np.isreal(ini_array) result = ini_array[mask] result = result[result != np.round(result)] # printing resultant print("final array : ", str(result)) #This code is contributed by Edula Vinay Kumar Reddy Output: initial array : [1. 1.2 2.2 2. 3. 2. ]final array : [1.2 2.2] Time complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Python | Filter out integers from float numpy array garg_ak0109 Follow Improve Article Tags : Python Python-numpy Python numpy-program Practice Tags : python Similar Reads How to Convert NumPy Array of Floats into Integers In this article, we will see how to convert NumPy Array of Floats into Integers. We are given a NumPy array of float-type values. Our task is to convert all float-type values of Numpy array to their nearest array of integer values.Input: [1.2, 4.5, 9.1, 6.5, 8.9, 2.3, 1.2]Output: [1, 4, 9, 6, 8, 2, 4 min read Import Text Files Into Numpy Arrays - Python We have to import data from text files into Numpy arrays in Python. By using the numpy.loadtxt() and numpy.genfromtxt() functions, we can efficiently read data from text files and store it as arrays for further processing.numpy.loadtxt( ) - Used to load text file datanumpy.genfromtxt( ) - Used to lo 3 min read Boolean Array in NumPy - Python The goal here is to work with Boolean arrays in NumPy, which contain only True or False values. Boolean arrays are commonly used for conditional operations, masking and filtering elements based on specific criteria. For example, given a NumPy array [1, 0, 1, 0, 1], we can create a Boolean array wher 3 min read numpy.asarray_chkfinite() in Python numpy.asarray_chkfinite() function is used when we want to convert the input to an array, checking for NaNs (Not A Number) or Infs(Infinities). Input includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asarray_chkfinite(arr, dtype=None, ord 2 min read How to round elements of the NumPy array to the nearest integer? Prerequisites: Python NumPy In this article, let's discuss how to round elements of the NumPy array to the nearest integer. numpy.rint() function of Python that can convert the elements of an array to the nearest integer. Syntax: numpy.rint(x, /, out=None, *, where=True, casting='same_kind', order=' 1 min read Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into 4 min read Filter Python list by Predicate in Python A predicate is a function that returns either True or False for a given input. By applying this predicate to each element of a list, we can create a new list containing only the elements that satisfy the condition. Let's explore different methods to filter a list based on a predicate.Using list comp 3 min read Python | Numpy numpy.ndarray.__ne__() With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!= 1 min read Convert 2D float array to 2D int array in NumPy Converting a 2D float array to a 2D integer array in NumPy is a straightforward process using the astype() method. This conversion can be useful in various data analysis and scientific computing tasks where integer data types are required or where memory efficiency is essential. In this article, we 8 min read numpy.asfarray() in Python numpy.asfarray()function is used when we want to convert input to a float type array. Input includes scalar, lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays. Syntax : numpy.asfarray(arr, dtype=type 'numpy.float64') Parameters : arr : [array_like] Input data, in any for 2 min read Like