numpy(.ma) 数组:自上次值更改以来的值数量?
我在 numpy.ma 数组中有一个状态信号(测量的热泵)以及时间戳。我想要的是它打开的时间长度和关闭的时间长度。 (不是每日运行时间或其他什么,这很容易..)
我拥有什么(示例,事实上我有超过 16 个月的分钟值..):
Time Value
12:00 0
12:01 1
12:02 1
...
12:29 1
12:30 1
12:31 0
...
12:41 0
12:42 1
...
13:01 1
13:02 0
...and so on
我想要什么作为输出:
running_time_was:
time value (=minutes)
12:31 30
13:10 20
off_time_was:
time value (=minutes)
12:42 11
(the exact timestamps doesn't matter so much, but there should be some)
我已经问过我的人知道谁了解Python(他们也不知道),并尝试在互联网上搜索,但我只是不知道要搜索什么。所以也许有人至少可以给我一个提示,我可以在谷歌中输入哪些单词? :)
ps: 哇,stackoverflow 太棒了!作为被动用户,我已经对它的可用性感到惊讶了,但询问界面甚至更好:)
i have a status-signal (measured, of a heatpump) in an numpy.ma-array, together with timestamps. What i want is the lengths of the periods it was on and the length of the periods it was off. (NOT the daily running time or something, that would be easy..)
What i have (exemplary, in fact i have minute-values over 16months..):
Time Value
12:00 0
12:01 1
12:02 1
...
12:29 1
12:30 1
12:31 0
...
12:41 0
12:42 1
...
13:01 1
13:02 0
...and so on
And what i want to have as output:
running_time_was:
time value (=minutes)
12:31 30
13:10 20
off_time_was:
time value (=minutes)
12:42 11
(the exact timestamps doesn't matter so much, but there should be some)
I already asked the people i know who know python (they had no idea, neither), and tried to search on the internet, but i just don't know what to search for. So maybe someone could at least give me a hint which words i could type into google? :)
ps: Wow, stackoverflow is fantastic! I was already amazed of the usability as passive user, but the asking interface is even better :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,终于我得到了我自己。但非常感谢 Joe Kington 的回答,它极大地启发了我并教会了我所有的东西:)
脚本
产量:
值得一提的事情:
抱歉,不精确,但该函数给出的输出是什么
我终于需要了。
蒙面,这很容易。
Well, finally i got i for myself. But millions of thanks to the answer of Joe Kington, which highly inspired me and teached me the whole stuff :)
Script
Yields in:
Things to mention:
Sorry for beeing unprecise, but the output the function gives is what
i finally need.
masked, which will be easy.
基本上,您有一个布尔数组,并且想要找到连续区域的起点和终点。
最好避免循环遍历 numpy 数组的每个项目。
有几种不同的方法可以做到这一点,但我通常会做类似的事情(我最初可能是从 这里):
作为一个简单的例子:
这会产生:
Basically, you have a boolean array and you want to find the start and stop of contiguous regions.
It's far better to avoid looping over each item of a numpy array.
There are a few different ways of doing it, but I usually do something similar to this (which I probably originally got from here):
As a quick example:
This yields:
我会按照以下方式完成此操作:
极其未经测试,但您可以了解逻辑。
I would have done this the following way:
Extremely untested but you can get the logic.
(可能)答案
您可以尝试以下操作:
您循环遍历空数组 (
Data
),该数组的第一列中包含时间戳,第二列中包含1.0
或 < code>0.0 取决于加热器是否打开或关闭。您可以通过查看实际的开/关值和前一个值来检测状态的变化。根据这两个值,您可以看到加热器是
关闭
还是开启
。然后您需要做的就是将当前索引的值保存在Since
中,然后您就可以获得加热器打开的时间。脚本
使用以下脚本,您可以设置一个数据数组并运行上面的代码,看看它是如何工作的:
输出是
(Possible) Answer
You could try this:
You loop through the hole array (
Data
) which has in its first column the time stamps and in its second column a1.0
or0.0
depending if the heater was on or off.You detect the change of state looking at the actual on/off value and the previous one. Depending on those two values you see if the heater was
Switched off
orSwitched on
. All you need to do then is to save the value of the current index inSince
and you get the time the heater was switched on.Script
With the following script you can set up a data array and run the code above and see how it works:
The output is