Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从numpy向量返回小于1的最大值_Python_Numpy - Fatal编程技术网

Python 从numpy向量返回小于1的最大值

Python 从numpy向量返回小于1的最大值,python,numpy,Python,Numpy,我在python中有一个numpy向量,我想找到向量的最大值的索引,条件是它小于1。我举了以下例子: temp_res = [0.9, 0.8, 0.7, 0.99, 1.2, 1.5, 0.1, 0.5, 0.1, 0.01, 0.12, 0.56, 0.89, 0.23, 0.56, 0.78] temp_res = np.asarray(temp_res) indices = np.where((temp_res == temp_res.max()) & (temp_res <

我在python中有一个numpy向量,我想找到向量的最大值的索引,条件是它小于1。我举了以下例子:

temp_res = [0.9, 0.8, 0.7, 0.99, 1.2, 1.5, 0.1, 0.5, 0.1, 0.01, 0.12, 0.56, 0.89, 0.23, 0.56, 0.78]
temp_res = np.asarray(temp_res)
indices = np.where((temp_res == temp_res.max()) & (temp_res < 1))
temp_res=[0.9,0.8,0.7,0.99,1.2,1.5,0.1,0.5,0.1,0.01,0.12,0.56,0.89,0.23,0.56,0.78]
温度分辨率=np.asarray(温度分辨率)
指数=np,其中((温度分辨率==温度分辨率最大值())和(温度分辨率<1))
然而,我尝试的总是返回一个空矩阵,因为这两个条件不能满足。HU希望返回索引=3作为最终结果,该索引对应于小于1的最大值0.99。我如何才能做到这一点?

您可以使用:

np.where(temp_res == temp_res[temp_res < 1].max())[0]
np.where(temp\u res==temp\u res[temp\u res<1].max())[0]
例如:

In [49]: temp_res
Out[49]: 
array([0.9 , 0.8 , 0.7 , 0.99, 1.2 , 1.5 , 0.1 , 0.5 , 0.1 , 0.01, 0.12,
       0.56, 0.89, 0.23, 0.56, 0.78])

In [50]: np.where(temp_res == temp_res[temp_res < 1].max())[0]
    ...: 
Out[50]: array([3])
...: temp_res = [0.9, 0.8, 0.7, 1, 0.99, 0.99, 1.2, 1.5, 0.1, 0.5, 0.1, 0.01, 0.12, 0.56, 0.89, 0.23, 0.56, 0.78]
...: temp_res = np.asarray(temp_res)
...: mask = temp_res < 1
...: indices = np.where(mask)
...: maximum = temp_res[mask].max()
...: max_indices = np.where(temp_res == maximum)
...: 

In [72]: max_indices
Out[72]: (array([4, 5]),)
[49]中的
:温度
出[49]:
阵列([0.9,0.8,0.7,0.99,1.2,1.5,0.1,0.5,0.1,0.01,0.12,
0.56, 0.89, 0.23, 0.56, 0.78])
在[50]中:np.where(temp\u res==temp\u res[temp\u res<1].max())[0]
...: 
Out[50]:数组([3])

过滤阵列后,需要执行
max()
功能:

temp_res = np.asarray(temp_res)
temp_res[temp_res < 1].max()

Out[60]: 0.99
temp\u res=np.asarray(temp\u res)
温度分辨率[温度分辨率<1].max()
Out[60]:0.99
如果您想查找所有索引,这里有一种更通用的方法:

mask = temp_res < 1
indices = np.where(mask)
maximum = temp_res[mask].max()
max_indices = np.where(temp_res == maximum)
mask=temp\u res<1
索引=np.其中(掩码)
最大值=温度分辨率[mask].max()
最大指数=np,其中(温度=最大值)
例如:

In [49]: temp_res
Out[49]: 
array([0.9 , 0.8 , 0.7 , 0.99, 1.2 , 1.5 , 0.1 , 0.5 , 0.1 , 0.01, 0.12,
       0.56, 0.89, 0.23, 0.56, 0.78])

In [50]: np.where(temp_res == temp_res[temp_res < 1].max())[0]
    ...: 
Out[50]: array([3])
...: temp_res = [0.9, 0.8, 0.7, 1, 0.99, 0.99, 1.2, 1.5, 0.1, 0.5, 0.1, 0.01, 0.12, 0.56, 0.89, 0.23, 0.56, 0.78]
...: temp_res = np.asarray(temp_res)
...: mask = temp_res < 1
...: indices = np.where(mask)
...: maximum = temp_res[mask].max()
...: max_indices = np.where(temp_res == maximum)
...: 

In [72]: max_indices
Out[72]: (array([4, 5]),)
…:temp_res=[0.9,0.8,0.7,1,0.99,0.99,1.2,1.5,0.1,0.5,0.1,0.01,0.12,0.56,0.89,0.23,0.56,0.78]
…:temp_res=np.asarray(temp_res)
…:掩码=温度分辨率<1
…:索引=np.其中(掩码)
…:max=temp_res[mask].max()
…:max_index=np.其中(temp_res==max)
...: 
In[72]:最大指数
Out[72]:(数组([4,5]),)

ValueError:包含多个元素的数组的真值不明确。对数组使用a.any()或a.all()@eugenhu是的,但这仍然是令人困惑的,不推荐使用。
argmax()
将返回筛选数组上的索引,并且只返回第一次出现的索引…@eugenhu否。如果在第一个0.99之前有一个大于1的条目,则结果是错误的。@lilicent是的,我理解你的意思,只是更新了。谢谢你的评论。