【问题标题】:Finding daily patterns in timestamps data with python使用 python 在时间戳数据中查找每日模式
【发布时间】:2016-08-25 17:34:36
【问题描述】:

所以我想分析智能家居的数据库数据。这就是我的数据的样子:

ID   NAME   STATUS TIME   
1    light  1      2016-06-25 08:00:00
2    light  1      2016-06-25 08:01:05
3    light  1      2016-06-25 08:00:21
4    light  1      2016-06-25 08:00:30
...

基本上,我需要计算的只是(特定时间亮灯的数量)除以特定时间不同日期的数量。

【问题讨论】:

  • 为了能够找到规律,例如在早上 8 点打开灯,它只需要每天查看,检查是否在早上 8 点发生事件,然后检查是否事件是“光”。然后为了能够确定他们是否会在明天早上 8 点打开它,请查看您的数据并做出预测。比如说,如果用户在 8 天 80% 的时间打开灯,那么他们很可能会在明天 8 点打开灯。
  • 然后基本上计算我所需要的只是除以(特定时间亮灯的数量)除以(特定时间不同日期的数量
  • 是的,这是您可以做到的一种方式。

标签: python machine-learning statistics artificial-intelligence


【解决方案1】:

此脚本从数据库中获取最短和最长时间,并计算这些时间之间的时间。

# db.txt
1    light  1      2016-06-25 08:00:00
1    light  1      2016-06-25 08:01:05
1    light  1      2016-06-25 08:00:21
1    light  1      2016-06-25 08:00:30

# python script
import datetime


def data():
    with open('db.txt', 'r') as f:
        for line in f.readlines():  
            row = line.split()
            if row[2] == '1':
                yield row[4]


data = sorted(data())
early = datetime.datetime.strptime(data[0], '%H:%M:%S')
lately = datetime.datetime.strptime(data[1], '%H:%M:%S')
sth_between = (lately - early)/2
print (early + sth_between).time()

【讨论】:

  • 这如何回答 OP 的问题?
  • @Harrison 已编辑。
  • 如果我的数据库中只有 08:00:00 左右的时间,那就太好了,但是一旦我有不同的时间(例如 16:00:00),计算就会出错
  • 好吧,我没有看到其他时间的任何数据。好的,所以我需要在您的问题中了解更多关于条件或更详细的解释以及所有用例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-31
  • 1970-01-01
  • 2016-11-02
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
相关资源
最近更新 更多