Python | Filter out integers from float numpy array Last Updated : 06 Jan, 2023 Summarize Comments Improve Suggest changes Share 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 G 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 Like