簡體   English   中英

在Python中按時間范圍計算事件

[英]Count events per range of time in Python

任務是計算從1970年到今天(2018-02-05)每天發生該事件(在這種情況下為地震)的頻率。 我有以下時間清單:

['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
'1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
'1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
'1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
'1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
'1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
'1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
'1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
'2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
'2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
'2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
'2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
'2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
'2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
'2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
'2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
'2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
'2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
'2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']

從我沒有完成的教程中,可以執行此步驟

In [114]: for i in range(1,len(time)):
   dt = UTCDateTime(time[i])-UTCDateTime(time[i-1])
   dt = dt / (3600*24)
   inter_event_time.append(dt)
. . . . . :

In [115]: plt.hist(inter_event_time, bins=range(0,1000,100))
Out [115]:
(array([10, 7, 3, 5, 2, 0, 1, 2, 2]),
 array([0, 100, 200, 300, 400, 500, 600, 700, 800, 900]),
 <a list of 9 Patch objects>)

最終結果就是這個 (x軸是天,y軸是它發生了多少次)。 我的問題是我不知道inter_event_times應該是什么。 請幫我!

如果您每天需要頻率,這是一種方法。

import pandas as pd
from datetime import datetime
from collections import Counter

lst = ['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
       '1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z', etc.]

# convert to datetime & normalize, then count frequencies
counter_dict = Counter(pd.to_datetime(x).normalize().to_pydatetime() for x in lst)

# Counter({datetime.datetime(2012, 4, 11, 0, 0): 2,
#          datetime.datetime(1970, 7, 31, 0, 0): 1, etc.})

為了生成直方圖,您可以在如下范圍內求和:

sum(counter_dict[k] for k, v in counter_dict.items() \
    if (datetime(1970, 1, 1, 0, 0) < k < datetime(1979, 12, 31, 0, 0)))  # 6

我沒有看圖片,但是可以用:

import datetime

date_string = ['1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
'1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
'1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
'1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
'1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
'1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
'1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
'1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
'2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
'2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
'2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
'2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
'2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
'2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
'2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
'2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
'2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
'2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
'2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']

date_list = [datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S.%fZ') for date in date_string]

date_start = datetime.datetime(1990, 1, 1)
date_end = datetime.datetime(2000, 1, 1)
count = 0
for date in date_list:
    if date <= date_end and date >= date_start:
        count += 1

print(count)

要重新創建給定的直方圖,您需要添加inter_event_time = [] ,如下所示:

import matplotlib.pyplot as plt    
from obspy import UTCDateTime

time = [
    '1970-07-31T17:08:05.000Z', '1971-07-14T06:11:30.000Z',
    '1971-07-26T01:23:22.000Z', '1972-12-02T00:19:52.000Z',
    '1976-01-14T16:47:33.500Z', '1977-08-19T06:08:55.000Z',
    '1985-03-03T22:47:07.280Z', '1985-09-19T13:17:47.350Z',
    '1986-05-07T22:47:10.870Z', '1989-05-23T10:54:46.320Z',
    '1994-06-09T00:33:16.230Z', '1994-10-04T13:22:55.840Z',
    '1995-07-30T05:11:23.630Z', '1995-10-09T15:35:53.910Z',
    '1996-02-17T05:59:30.550Z', '1998-03-25T03:12:25.070Z',
    '2000-11-16T04:54:56.740Z', '2001-06-23T20:33:14.130Z',
    '2003-09-25T19:50:06.360Z', '2004-12-23T14:59:04.410Z',
    '2004-12-26T00:58:53.450Z', '2005-03-28T16:09:36.530Z',
    '2006-05-03T15:26:40.290Z', '2006-11-15T11:14:13.570Z',
    '2007-01-13T04:23:21.160Z', '2007-04-01T20:39:58.710Z',
    '2007-08-15T23:40:57.890Z', '2007-09-12T11:10:26.830Z',
    '2009-09-29T17:48:10.990Z', '2010-02-27T06:34:11.530Z',
    '2011-03-11T05:46:24.120Z', '2012-04-11T08:38:36.720Z',
    '2012-04-11T10:43:10.850Z', '2013-02-06T01:12:25.830Z',
    '2013-05-24T05:44:48.980Z', '2014-04-01T23:46:47.260Z',
    '2015-09-16T22:54:32.860Z', '2017-09-08T04:49:19.180Z']

inter_event_time = []

for i in range(1, len(time)):
   dt = UTCDateTime(time[i])-UTCDateTime(time[i-1])
   dt = dt / (3600*24)
   inter_event_time.append(dt)

plt.hist(inter_event_time, bins=range(0,1000,100))   
plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM