Finding daily patterns in timestamps data with python
<p>So i want to analyze database data of smart house. This is how data i have look like: </p>

<pre><code>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
...
</code></pre>

<p>Basically to calculate all i need is divide (number of light turns on at certain hour) by number of different dates at certain hour.</p>

<p>This script takes lowest and maximum time from database and calculate time between these times.</p>

<pre><code># 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()
</code></pre>

python machine-learning statistics artificial-intelligence