在Python中返回数组的最大值或忽略任何NaN的最大值 |
您所在的位置:网站首页 › numpy数组最大值 › 在Python中返回数组的最大值或忽略任何NaN的最大值 |
在Python中返回数组的最大值或忽略任何NaN的最大值
在这篇文章中,我们将介绍如何使用NumPy在Python中通过忽略任何NaN来返回一个数组的最大值或最大。 示例: 输入: [ -1. -2. nan 1000.] 输出: 1000.0 解释: 忽略nans的数组的最大值。 NumPy.nanmax() 方法NumPy的numpy.nanmax()方法返回数组中的最高值或最大值或沿轴的最高值,忽略任何NaN。 语法: numpy.nanmax(a, axis=None, out=None) 参数: a: 类似数组的对象。 axis:默认为无。 out:默认为无。返回:最大的数组值(如果没有轴,则为标量值)或具有沿指定轴的最大值的数组。 示例 1:在这个例子中,NumPy包被导入。使用numpy.array()方法创建了一个数组,其中包含nan和其他值,np.nanmax()返回数组的最大值,忽略了nans。数组的形状、数据类型和尺寸可以通过.shape、.dtype和.ndim属性找到。 import numpy as np # Creating an array array = np.array([-1, -2 , np.nan, 1000]) print(array) # shape of the array is print("Shape of the array is : ",array.shape) # dimension of the array print("The dimension of the array is : ",array.ndim) # Datatype of the array print("Datatype of our Array is : ",array.dtype) # computing the maximum or array ignoring Nans print(np.nanmax(array))输出: [ -1. -2. nan 1000.] Shape of the array is : (4,) The dimension of the array is : 1 Datatype of our Array is : float64 1000.0 示例 2:如果我们的数组包含np.inf或正无穷大,np.nanmax()方法会返回inf。 import numpy as np # Creating an array array = np.array([-1, -2 , np.inf,np.nan, 1000]) print(array) # shape of the array is print("Shape of the array is : ",array.shape) # dimension of the array print("The dimension of the array is : ",array.ndim) # Datatype of the array print("Datatype of our Array is : ",array.dtype) # computing the maximum or array ignoring Nans print(np.nanmax(array))输出: [ -1. -2. inf nan 1000.] Shape of the array is : (5,) The dimension of the array is : 1 Datatype of our Array is : float64 inf 示例 3:如果数组中的所有元素都是纳数,那么该方法就会引发一个运行时警告:”RuntimeWarning:遇到了全纳的片断”。 import numpy as np # Creating an array array = np.array([np.nan, np.nan]) print(array) # shape of the array is print("Shape of the array is : ",array.shape) # dimension of the array print("The dimension of the array is : ",array.ndim) # Datatype of the array print("Datatype of our Array is : ",array.dtype) # computing the maximum or array ignoring Nans print(np.nanmax(array))输出: [nan nan] Shape of the array is : (2,) The dimension of the array is : 1 Datatype of our Array is : float64 nan RuntimeWarning: All-NaN slice encountered |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |