gpt4 book ai didi

python - 提取循环内的某些条件/值

转载 作者:行者123 更新时间:2023-12-01 07:03:07 25 4
gpt4 key购买 nike

我有一个数据集,是我使用循环开发的(代码如下)。

## A subset of the data
deficit = (E_daily[:,160,100] - P_daily[:,160,100]) # A view of `deficit` is given below
deficit_cum = np.zeros([365])
start = 0
stop = 0
for i in range(365):
deficit_cum[i] = deficit[i] + deficit_cum[i-1]
if deficit_cum[i] >= 0:
if np.nanmax(deficit_cum) <= deficit_cum[i]:
stop = i
else:
continue
else:
deficit_cum[i] = 0
if np.nanmax(deficit_cum) > deficit_cum[i]:
continue
else:
start = i #Start is not defined correctly by me

这是循环之前deficit的样子 enter image description here

现在我也对循环开始和结束的值的索引感兴趣。我知道这很令人困惑,这是一个示例(下面是 deficit_cum 的外观):

enter image description here

在上图中,绿线定义了代码中的start(x轴),红线定义了stop(x轴)。所以基本上我希望最大值成为我的停止点。但我希望我的 start 小于 stop,并且 startstop 之间不应该有任何负点。 因此,对于下图,我的 start 应该在 111 左右,stop 应该在 236 左右。

我想我知道如何获得stop(上面的代码),但我仍然无法定义start[附加信息:我的 start 应该是全局最大值之前最后一个零的索引,stop 应该是全局最大值的索引]

最佳答案

因此,您有一个列表,并且想要在名为 startstopmax 之前找到 min ,假设列表名为a,您可以使用argmaxargmin函数,所以我们有:

import numpy as np

a = np.array([1,2,3,6,6,6,4,5])
start = np.argmin(a)

b = np.where(a==a.max()) # find indices in which max values exist
b = np.reshape(b,-1)
new_array = a[:b[-1]+1] # make a new array, it starts from 0 to index of last max value
# new_array = = [1,2,3,6,6,6]

c = np.where(new_array==new_array.min())
c = np.reshape(c,-1)

start = c[-1]
stop = b[-1]

关于python - 提取循环内的某些条件/值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com