計算速度

先照之前的,讀取資料


In [1]:
import tqdm
import tarfile
import pandas
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import PIL
import gzip
from urllib.request import urlopen
%matplotlib inline
matplotlib.style.use('ggplot')
#matplotlib.style.use('bmh')

# progress bar
tqdm.tqdm.pandas()

# 檔案名稱格式
filename_format="M06A_{year:04d}{month:02d}{day:02d}.tar.gz".format
xz_filename_format="xz/M06A_{year:04d}{month:02d}{day:02d}.tar.xz".format
csv_format = "M06A/{year:04d}{month:02d}{day:02d}/{hour:02d}/TDCS_M06A_{year:04d}{month:02d}{day:02d}_{hour:02d}0000.csv".format

In [2]:
# 打開剛才下載的檔案試試
data_config ={"year":2016, "month":12, "day":18}
tar = tarfile.open(filename_format(**data_config), 'r')

In [3]:
# 如果沒有下載,可以試試看 xz 檔案
#data_dconfig ={"year":2016, "month":11, "day":18}
#tar = tarfile.open(xz_filename_format(**data_config), 'r')

In [4]:
# 設定欄位名稱
M06A_fields = ['VehicleType',
               'DetectionTime_O','GantryID_O',
               'DetectionTime_D','GantryID_D ',
               'TripLength', 'TripEnd', 'TripInformation']
# 打開裡面 10 點鐘的資料
csv = tar.extractfile(csv_format(hour=10, **data_config))

# 讀進資料
data = pandas.read_csv(csv, names=M06A_fields)

# 檢查異常的資料
print("異常資料數:", data[data.TripEnd == 'N'].shape[0])

# 去除異常資料
data = data[data.TripEnd == 'Y']

# 焦點放在 TripInformation 和 VehicleType
data = data[['VehicleType', "TripInformation"]]

# 看前五筆
data.head(5)


異常資料數: 0
Out[4]:
VehicleType TripInformation
0 31 2016-12-18 10:00:50+01F1045N; 2016-12-18 10:04...
1 31 2016-12-18 10:34:24+01F3525S; 2016-12-18 10:37...
2 31 2016-12-18 10:57:33+03F3854N; 2016-12-18 11:12...
3 31 2016-12-18 10:05:11+03F0525S; 2016-12-18 10:07...
4 31 2016-12-18 10:35:46+01F3185S; 2016-12-18 10:38...

In [5]:
import datetime
# 用來解析時間格式
def strptime(x):
    return datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S")

In [6]:
def parse_tripinfo(tripinfo):
    split1 = tripinfo.split("; ")
    split2 = (x.split('+') for x in split1)
    return [(strptime(t), node) for t,node in split2]
# 試試看
data.head(10).TripInformation.apply(parse_tripinfo)


Out[6]:
0    [(2016-12-18 10:00:50, 01F1045N), (2016-12-18 ...
1    [(2016-12-18 10:34:24, 01F3525S), (2016-12-18 ...
2    [(2016-12-18 10:57:33, 03F3854N), (2016-12-18 ...
3    [(2016-12-18 10:05:11, 03F0525S), (2016-12-18 ...
4    [(2016-12-18 10:35:46, 01F3185S), (2016-12-18 ...
5    [(2016-12-18 10:00:35, 03F3496N), (2016-12-18 ...
6    [(2016-12-18 10:00:21, 05F0287N), (2016-12-18 ...
7    [(2016-12-18 10:50:12, 01H0271N), (2016-12-18 ...
8    [(2016-12-18 10:09:36, 01F0339S), (2016-12-18 ...
9    [(2016-12-18 10:36:58, 01F3696N), (2016-12-18 ...
Name: TripInformation, dtype: object

In [7]:
# 新增一欄
data['Trip'] = data.TripInformation.progress_apply(parse_tripinfo)


100%|██████████| 211247/211247 [00:18<00:00, 11235.23it/s]

計算平均速度

計算兩個偵測站之間的速度,只計算同一條路線上的


In [8]:
trip = data['Trip'][0]
trip


Out[8]:
[(datetime.datetime(2016, 12, 18, 10, 0, 50), '01F1045N'),
 (datetime.datetime(2016, 12, 18, 10, 4, 37), '01F0979N'),
 (datetime.datetime(2016, 12, 18, 10, 5, 58), '01F0956N'),
 (datetime.datetime(2016, 12, 18, 10, 7, 43), '01F0928N'),
 (datetime.datetime(2016, 12, 18, 10, 10, 43), '01F0880N'),
 (datetime.datetime(2016, 12, 18, 10, 18, 38), '01F0750N'),
 (datetime.datetime(2016, 12, 18, 10, 27, 26), '01H0608N'),
 (datetime.datetime(2016, 12, 18, 10, 29, 1), '01F0584N')]

In [9]:
for (t1,n1), (t2,n2) in zip(trip[:-1], trip[1:]):
    # 去除換路線的
    if n1[:3] != n2[:3] or n1[-1]!=n2[-1]:
        continue
    # 去除額外路線的
    if n1[3]=='R' or n2[3]=='R': 
        continue
    # 從站名取得公里數
    km1 = int(n1[3:-1])/10
    km2 = int(n2[3:-1])/10
    hr_delta = (t2-t1).total_seconds()/60/60
    speed = abs(km2-km1)/hr_delta
    print(speed)


104.66960352422898
102.22222222222271
95.9999999999999
95.99999999999994
98.52631578947368

包成函數


In [10]:
def compute_speed(trip):
    rtn = []
    for (t1,n1), (t2,n2) in zip(trip[:-1], trip[1:]):
        # 去除換路線的
        if n1[:3] != n2[:3] or n1[-1]!=n2[-1]:
            continue
        # 去除額外路線的
        if n1[3]=='R' or n2[3]=='R': 
            continue
        # 從站名取得公里數
        km1 = int(n1[3:-1])/10
        km2 = int(n2[3:-1])/10
        hr_delta = (t2-t1).total_seconds()/60/60
        speed = abs(km2-km1)/hr_delta
        rtn.append(speed)
    return np.array(rtn)

Q

compute_speed 改成 numpy 方式的寫法?


In [11]:
data.head(10).Trip.apply(compute_speed)


Out[11]:
0    [104.669603524, 102.222222222, 96.0, 96.0, 98....
1    [76.6863905325, 81.5625, 84.5070422535, 85.263...
2                       [95.2941176471, 94.7368421053]
3                                      [82.7027027027]
4    [103.561643836, 105.882352941, 108.318584071, ...
5                                      [103.728813559]
6    [9.45652173913, 82.7234042553, 100.0, 94.94505...
7                                [87.9069767442, 90.0]
8                                                   []
9                       [83.7209302326, 41.6720257235]
Name: Trip, dtype: object

In [12]:
data['Speed'] = data.Trip.progress_apply(compute_speed)


100%|██████████| 211247/211247 [00:02<00:00, 71177.30it/s]

In [13]:
# 只留下有速度的旅程
valid_idx = data.Speed.apply(len).astype('bool')
valid_data = data[valid_idx].copy()
del valid_data['TripInformation']
valid_data.head()


Out[13]:
VehicleType Trip Speed
0 31 [(2016-12-18 10:00:50, 01F1045N), (2016-12-18 ... [104.669603524, 102.222222222, 96.0, 96.0, 98....
1 31 [(2016-12-18 10:34:24, 01F3525S), (2016-12-18 ... [76.6863905325, 81.5625, 84.5070422535, 85.263...
2 31 [(2016-12-18 10:57:33, 03F3854N), (2016-12-18 ... [95.2941176471, 94.7368421053]
3 31 [(2016-12-18 10:05:11, 03F0525S), (2016-12-18 ... [82.7027027027]
4 31 [(2016-12-18 10:35:46, 01F3185S), (2016-12-18 ... [103.561643836, 105.882352941, 108.318584071, ...

這樣就能計算旅程中的最高速度


In [14]:
valid_data['MaxSpeed']=valid_data.Speed.apply(max)

In [15]:
valid_data.sort_values('MaxSpeed', ascending=False)


Out[15]:
VehicleType Trip Speed MaxSpeed
98 31 [(2016-12-18 10:55:40, 03F2261S), (2016-12-18 ... [114.893617021, 101.886792453, 111.968503937, ... 180.000000
66641 32 [(2016-12-18 10:06:25, 03F4142N), (2016-12-18 ... [122.016806723, 138.461538462, 138.633540373, ... 172.800000
130441 31 [(2016-12-18 10:50:56, 03F2985S), (2016-12-18 ... [138.737541528, 172.0, 148.965517241, 160.0, 1... 172.000000
44047 31 [(2016-12-18 10:07:24, 01F3590S), (2016-12-18 ... [118.421052632, 130.909090909, 171.428571429] 171.428571
51576 31 [(2016-12-18 10:20:05, 03F1944S), (2016-12-18 ... [116.689655172, 116.379310345, 106.363636364, ... 169.411765
114582 31 [(2016-12-18 10:18:50, 03F0525N), (2016-12-18 ... [164.745762712, 150.491803279, 153.870967742, ... 164.745763
175436 31 [(2016-12-18 10:21:08, 01F3460N), (2016-12-18 ... [98.3259911894, 98.4615384615, 14.9843912591, ... 163.125000
109114 31 [(2016-12-18 10:46:57, 03F2747S), (2016-12-18 ... [116.129032258, 114.545454545, 123.488372093, ... 163.018868
29966 31 [(2016-12-18 10:48:35, 03F3496N), (2016-12-18 ... [132.086330935, 128.053691275, 121.912350598, ... 162.514286
62146 31 [(2016-12-18 10:04:08, 03F4259N), (2016-12-18 ... [124.615384615, 143.105590062, 161.379310345, ... 161.379310
13266 31 [(2016-12-18 10:42:38, 03F4259N), (2016-12-18 ... [120.0, 138.795180723, 161.379310345] 161.379310
159963 31 [(2016-12-18 10:23:47, 01F2603N), (2016-12-18 ... [108.979591837, 125.393258427, 124.285714286, ... 160.851064
70902 31 [(2016-12-18 10:05:27, 03F2153N), (2016-12-18 ... [148.235294118, 160.714285714] 160.714286
6824 31 [(2016-12-18 10:13:45, 03F4021N), (2016-12-18 ... [115.596330275, 115.051546392, 124.099378882, ... 160.000000
141798 31 [(2016-12-18 10:52:39, 01F3640N), (2016-12-18 ... [134.081632653, 141.244019139, 142.75862069, 1... 160.000000
48300 31 [(2016-12-18 10:31:46, 03F2985S), (2016-12-18 ... [146.526315789, 140.727272727, 160.0, 138.24, ... 160.000000
204316 31 [(2016-12-18 10:24:42, 03F2306N), (2016-12-18 ... [159.230769231, 124.285714286, 113.846153846, ... 159.230769
131266 31 [(2016-12-18 10:18:02, 05F0309N), (2016-12-18 ... [50.1265822785, 56.9325153374, 83.0769230769, ... 158.400000
200065 31 [(2016-12-18 10:02:54, 03F2194N), (2016-12-18 ... [149.090909091, 157.5, 142.857142857, 129.8360... 157.500000
211119 31 [(2016-12-18 10:50:53, 01F2425N), (2016-12-18 ... [126.818181818, 125.825242718, 121.666666667, ... 157.500000
154637 31 [(2016-12-18 10:27:52, 03F0447S), (2016-12-18 ... [88.3018867925, 97.92, 80.9090909091, 86.53846... 157.365269
83285 31 [(2016-12-18 10:02:23, 01F3640S), (2016-12-18 ... [124.615384615, 156.52173913] 156.521739
20760 31 [(2016-12-18 10:11:39, 01F3640S), (2016-12-18 ... [129.6, 156.52173913] 156.521739
31446 31 [(2016-12-18 10:29:08, 01F0664S), (2016-12-18 ... [98.7096774194, 108.0, 109.601873536, 98.74285... 155.820896
194798 31 [(2016-12-18 10:13:38, 03F2194S), (2016-12-18 ... [98.4, 112.771084337, 108.0, 102.857142857, 11... 155.675676
160843 31 [(2016-12-18 10:13:41, 03F2066S), (2016-12-18 ... [91.7647058824, 93.3333333333, 31.8292682927, ... 155.329949
154964 31 [(2016-12-18 10:59:08, 03F1710S), (2016-12-18 ... [124.285714286, 115.2, 116.64, 155.076923077, ... 155.076923
21269 31 [(2016-12-18 10:00:00, 03F0498N), (2016-12-18 ... [125.753424658, 130.684931507, 123.680981595, ... 154.883721
41953 31 [(2016-12-18 10:26:35, 03F2336N), (2016-12-18 ... [138.461538462, 141.538461538, 117.303370787, ... 154.883721
479 31 [(2016-12-18 10:47:47, 01F3640N), (2016-12-18 ... [115.384615385, 94.5762711864, 94.8837209302, ... 154.666667
... ... ... ... ...
200742 31 [(2016-12-18 10:41:37, 03F0301N), (2016-12-18 ... [5.2509116166] 5.250912
172814 31 [(2016-12-18 10:59:24, 03F0338N), (2016-12-18 ... [5.19933158272] 5.199332
152096 31 [(2016-12-18 10:56:16, 03F0158S), (2016-12-18 ... [5.15759312321] 5.157593
119915 31 [(2016-12-18 10:38:31, 03F0648N), (2016-12-18 ... [5.08329366968] 5.083294
73849 5 [(2016-12-18 10:55:03, 01F1664S), (2016-12-18 ... [5.06329113924] 5.063291
207187 31 [(2016-12-18 10:56:29, 01F3640S), (2016-12-18 ... [5.05032021958] 5.050320
86607 31 [(2016-12-18 10:42:51, 03F0648N), (2016-12-18 ... [5.02115655853] 5.021157
11309 31 [(2016-12-18 10:15:22, 01F2089N), (2016-12-18 ... [5.01966392563] 5.019664
58710 31 [(2016-12-18 10:18:00, 03F0447S), (2016-12-18 ... [4.9734325186] 4.973433
88975 31 [(2016-12-18 10:48:02, 03F3496N), (2016-12-18 ... [4.9550026469] 4.955003
176941 31 [(2016-12-18 10:53:52, 01F0256N), (2016-12-18 ... [4.94033412888] 4.940334
21087 31 [(2016-12-18 10:15:55, 01F3696N), (2016-12-18 ... [4.93150684932] 4.931507
34395 31 [(2016-12-18 10:52:36, 03A0015S), (2016-12-18 ... [4.90052356021] 4.900524
57164 32 [(2016-12-18 10:18:36, 01F3696N), (2016-12-18 ... [4.76033057851] 4.760331
119259 42 [(2016-12-18 10:55:22, 01F2394S), (2016-12-18 ... [4.75906183369] 4.759062
63138 31 [(2016-12-18 10:12:18, 03F0447S), (2016-12-18 ... [4.7344461305] 4.734446
77795 31 [(2016-12-18 10:58:59, 01F0256N), (2016-12-18 ... [4.5948945616] 4.594895
91733 31 [(2016-12-18 10:41:05, 03F0525N), (2016-12-18 ... [4.59198691742] 4.591987
176758 31 [(2016-12-18 10:34:27, 03F0846N), (2016-12-18 ... [4.57534799274] 4.575348
63360 31 [(2016-12-18 10:19:12, 03F1332S), (2016-12-18 ... [4.56246228123] 4.562462
38966 32 [(2016-12-18 10:01:25, 03F3187S), (2016-12-18 ... [3.59850062474] 3.598501
107598 32 [(2016-12-18 10:22:15, 03F1710S), (2016-12-18 ... [3.44782034346] 3.447820
23682 31 [(2016-12-18 10:33:49, 01F0532S), (2016-12-18 ... [3.37964701465] 3.379647
137430 31 [(2016-12-18 10:33:59, 03F1739N), (2016-12-18 ... [3.04018637158] 3.040186
49206 31 [(2016-12-18 10:26:40, 03F1739N), (2016-12-18 ... [2.70466321244] 2.704663
116347 31 [(2016-12-18 10:42:49, 03F1739N), (2016-12-18 ... [2.50901225667] 2.509012
23119 31 [(2016-12-18 10:04:45, 03F3211N), (2016-12-18 ... [2.4338028169] 2.433803
145484 5 [(2016-12-18 10:21:10, 01F0061N), (2016-12-18 ... [1.87525702536] 1.875257
109393 31 [(2016-12-18 10:42:08, 03F3187S), (2016-12-18 ... [1.64071401443] 1.640714
111141 32 [(2016-12-18 10:42:07, 03F3187S), (2016-12-18 ... [1.63760424564] 1.637604

148719 rows × 4 columns

Q

為什麼有些旅程的速限那麼低? 發生了什麼事情? 用

valid_data.loc[號碼].Trip

可以看看


In [ ]:

查詢超過最高速限 110 十公里以上的旅程


In [16]:
valid_data.query("MaxSpeed > 120").shape


Out[16]:
(5894, 4)

In [17]:
# 看看統計圖表
valid_data.MaxSpeed.hist(bins=np.arange(0,200,10))


Out[17]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f48d8115668>

In [18]:
# 不同車種的速度
valid_data.boxplot(by='VehicleType', showmeans=True, showfliers=False);


Q

參考 http://matplotlib.org/api/pyplot_api.html 用不同方法畫畫看 如混何應用看看

# 畫很多 histogram 小圖
valid_data.hist(by='VehicleType', column='MaxSpeed', bins=np.arange(0,200,10));

# 另一種很多 histogram 小圖
valid_data.groupby('VehicleType').hist()

# histogram 疊合在一起
for vt in [31,32,41,42,5]:
    valid_data[valid_data.VehicleType==vt].MaxSpeed.hist(bins=np.arange(0,200,10), alpha=0.5)

# 空心的畫法,圖示說明,透明度
for vt in [41,42,5]:
    valid_data[valid_data.VehicleType==vt].MaxSpeed.hist(bins=np.arange(0,200,10), 
                label=str(vt), alpha=0.5, histtype='step', linewidth=2)
plt.legend();

# 不同車種的速度
gp = valid_data.groupby('VehicleType')
plt.gca().get_xaxis().set_visible(False)
gp.mean().plot.bar(ax=plt.gca(), yerr=gp.std(), table=True);

In [19]:
vt_types = [31,32,41,42, 5]
vt_labels = ["小客車", "小貨車", "大客車","大貨車", "聯結車"]
plt.hist([valid_data.MaxSpeed[valid_data.VehicleType==vt] for vt in vt_types], 
         label=vt_labels, bins=np.arange(50,150,10), normed=1)
for t in plt.legend().texts:
    t.set_fontname('Droid Sans Fallback')
#用 matplotlib.font_manager.fontManager.ttflist 看你有什麼字型


最後看看我們抓到多少超速了


In [20]:
num_total = data.shape[0]
num_valid = valid_data.shape[0]
num_overspeed = valid_data.query("MaxSpeed > 120").shape[0]
num_total, num_valid, num_overspeed


Out[20]:
(211247, 148719, 5894)

In [21]:
num_overspeed / num_total


Out[21]:
0.027900987943024043

In [22]:
num_overspeed / num_valid


Out[22]:
0.03963178880976876

參考一下國道違規統計的數據


In [23]:
stat_url = "http://www.hpb.gov.tw/files/11-1000-138.php"
國道違規統計 = pandas.read_html(stat_url, attrs={"bordercolor":"#cccccc"}, header=1)[0]
國道違規統計


Out[23]:
違規 項目 105年 104年 103年 102年 101年 100年 99年 98年 97年 96年
0 行駛路肩 29144 27203 16750 14023 12189 12781 16111 15770 24625 35383
1 超速 383655 392444 415784 417088 460826 477775 493017 473948 565548 695921
2 大客車站立乘客 8 11 6 9 21 23 4 16 25 36
3 超載 10013 11380 4413 9938 8618 10079 9789 10497 10498 8866
4 路肩上下客 0 70 59 51 38 42 27 7 0 7
5 無照駕駛 5375 5207 4684 7339 7575 6047 7667 9158 10368 12502
6 酒後駕車 4034 4024 5039 4921 6129 5816 7174 9560 8598 9486
7 不依規定車道行駛 11694 9541 10467 11384 11381 9707 8493 8540 15553 17385
8 任意停車或未擺警告標誌 1113 1565 1282 921 1228 1330 1368 1612 4059 3313
9 任意變換車道 29163 25114 23800 26127 23516 23463 22326 26874 35080 40554
10 載貨未覆蓋沿途滲漏飛散 950 928 1004 867 1724 2457 102 363 494 470
11 裝載不穩妥 5302 4223 3940 3514 2620 4079 3760 3306 3989 3410
12 裝載危險物品未依規定 1118 1245 1062 1005 805 998 965 950 1376 919
13 號牌粘貼反光紙塗抹磨損不清或未依規定縣掛號牌 179 140 98 113 124 175 270 393 534 2572
14 未保持行車安全距離 19764 17435 14787 15429 14868 13395 16578 21991 31219 46220
15 其他違規 129288 117361 92344 98841 107702 110835 116108 114931 131203 97009
16 合計 630870 617891 595519 611570 659364 679002 703759 697916 843169 974053

In [24]:
國道違規統計[國道違規統計["違規 項目"]=="超速"]


Out[24]:
違規 項目 105年 104年 103年 102年 101年 100年 99年 98年 97年 96年
1 超速 383655 392444 415784 417088 460826 477775 493017 473948 565548 695921

In [25]:
# 平均每天
國道違規統計[國道違規統計["違規 項目"]=="超速"].applymap(lambda x:int(x)/365 if isinstance(x,int) else x)


Out[25]:
違規 項目 105年 104年 103年 102年 101年 100年 99年 98年 97年 96年
1 超速 1051.109589 1075.189041 1139.134247 1142.706849 1262.536986 1308.972603 1350.731507 1298.487671 1549.446575 1906.632877

統計一整天

剛才我們只有統計一個小時,現在來統計整天


In [26]:
csvs = (tar.extractfile(csv_format(hour=hr, **data_config)) for hr in tqdm.trange(24))

data = pandas.concat([pandas.read_csv(csv, names=M06A_fields) for csv in csvs])
print("資料大小", data.shape)

# 檢查異常的資料
print("異常資料數:", data[data.TripEnd == 'N'].shape[0])

# 去除異常資料
data = data[data.TripEnd == 'Y']

# 把焦點放在 TripInformation 和 VehicleType
data = data[['VehicleType', "TripInformation"]]


100%|██████████| 24/24 [00:39<00:00,  1.71s/it]
資料大小 (3350606, 8)
異常資料數: 0

In [27]:
# 很慢? 可以參考 04-Speed-Limit.ipynb 中 parse_tripinfo 的加速
data['Trip'] = data.TripInformation.progress_apply(parse_tripinfo)
data['Speed'] = data.Trip.progress_apply(compute_speed)


100%|██████████| 3350606/3350606 [09:28<00:00, 5891.02it/s]
100%|██████████| 3350606/3350606 [01:44<00:00, 31954.76it/s]

In [28]:
valid_idx = data.Speed.apply(len).astype('bool')
valid_data = data[valid_idx].copy()
del valid_data['TripInformation']
valid_data['MaxSpeed']=valid_data.Speed.apply(max)

In [29]:
valid_data.MaxSpeed.hist(bins=np.arange(0,200,10));



In [30]:
valid_data.sort_values("MaxSpeed", ascending=False)


Out[30]:
VehicleType Trip Speed MaxSpeed
515 31 [(2016-12-18 02:29:08, 01F1774N), (2016-12-18 ... [70.2788844622, 91.1203319502, 254.634146341] 254.634146
1920 31 [(2016-12-18 05:25:34, 03F0301N), (2016-12-18 ... [162.580645161, 250.434782609, 151.73553719, 1... 250.434783
13585 31 [(2016-12-18 00:22:21, 03F2261S), (2016-12-18 ... [221.917808219, 211.764705882, 243.076923077, ... 249.448819
13317 31 [(2016-12-18 02:44:05, 03F0116N), (2016-12-18 ... [166.451612903, 180.0, 175.263157895, 172.9870... 247.868852
38236 31 [(2016-12-18 00:19:33, 03F2152S), (2016-12-18 ... [232.615384615, 238.064516129, 217.674418605, ... 247.304348
39553 31 [(2016-12-18 00:23:36, 03F2306S), (2016-12-18 ... [234.97005988, 244.06779661, 210.909090909, 17... 244.067797
6333 31 [(2016-12-18 01:54:23, 03F3588S), (2016-12-18 ... [236.16, 243.333333333, 199.384615385, 225.0] 243.333333
7859 31 [(2016-12-18 02:58:16, 01F3640N), (2016-12-18 ... [219.512195122, 232.5, 172.394366197, 195.0, 1... 232.500000
25188 31 [(2016-12-18 00:19:31, 03F2152S), (2016-12-18 ... [229.846153846, 222.857142857, 225.0, 232.1893... 232.189349
5641 31 [(2016-12-18 03:41:40, 03F2129S), (2016-12-18 ... [231.683168317] 231.683168
12011 31 [(2016-12-18 03:13:32, 03F1651N), (2016-12-18 ... [231.428571429, 137.319587629, 114.084507042, ... 231.428571
29820 31 [(2016-12-18 01:11:58, 03F2129S), (2016-12-18 ... [230.0, 191.392405063, 205.0, 173.333333333, 1... 230.000000
4908 31 [(2016-12-18 00:19:00, 03F2129S), (2016-12-18 ... [230.0, 222.352941176, 223.636363636, 208.0, 2... 230.000000
6802 31 [(2016-12-18 00:18:57, 03F2129S), (2016-12-18 ... [229.411764706] 229.411765
6803 31 [(2016-12-18 00:29:37, 03F2535S), (2016-12-18 ... [222.918149466, 171.0, 173.164556962, 169.3069... 222.918149
35003 31 [(2016-12-18 00:19:00, 03F2129S), (2016-12-18 ... [217.894736842, 212.957746479, 217.058823529, ... 222.857143
58836 31 [(2016-12-18 23:51:45, 03F2066S), (2016-12-18 ... [161.379310345, 222.352941176, 152.195121951, ... 222.352941
15829 32 [(2016-12-18 01:05:09, 03F2614N), (2016-12-18 ... [184.675324675, 207.058823529, 221.538461538, ... 221.538462
37693 31 [(2016-12-18 06:06:30, 03F2415N), (2016-12-18 ... [203.142857143, 189.473684211, 201.951219512, ... 221.204819
9073 31 [(2016-12-18 03:17:20, 03F1161N), (2016-12-18 ... [220.0, 195.211267606, 183.157894737, 176.6037... 220.000000
8653 31 [(2016-12-18 03:16:14, 01F0557N), (2016-12-18 ... [219.512195122] 219.512195
24781 31 [(2016-12-18 01:54:20, 03F2153N), (2016-12-18 ... [219.130434783, 173.076923077, 152.307692308, ... 219.130435
8406 31 [(2016-12-18 01:54:19, 03F2153N), (2016-12-18 ... [219.130434783, 173.076923077, 149.433962264, ... 219.130435
48685 31 [(2016-12-18 00:11:40, 01F3185N), (2016-12-18 ... [155.03649635, 180.0, 161.118881119, 200.93023... 216.000000
12928 31 [(2016-12-18 03:03:12, 03F1651N), (2016-12-18 ... [216.0, 215.708502024, 209.032258065, 208.0733... 216.000000
6933 31 [(2016-12-18 22:41:40, 03F0394S), (2016-12-18 ... [104.262295082, 99.9288256228, 109.285714286, ... 215.121951
14084 31 [(2016-12-18 02:25:05, 01F0248S), (2016-12-18 ... [110.769230769, 107.628865979, 150.545454545, ... 215.022624
480 31 [(2016-12-18 01:42:36, 01F1839S), (2016-12-18 ... [133.259668508, 144.0, 136.0, 147.015706806, 1... 214.615385
46339 31 [(2016-12-18 00:11:44, 01F3185N), (2016-12-18 ... [178.487394958, 173.93258427, 175.877862595, 2... 214.382022
17441 31 [(2016-12-18 03:42:12, 03F2100N), (2016-12-18 ... [214.054054054, 154.285714286, 158.571428571, ... 214.054054
... ... ... ... ...
137638 31 [(2016-12-18 16:27:29, 01F2827S), (2016-12-18 ... [2.2374501992] 2.237450
147339 31 [(2016-12-18 16:45:35, 03F1710S), (2016-12-18 ... [2.16732406062] 2.167324
94918 31 [(2016-12-18 16:21:54, 03F1710S), (2016-12-18 ... [2.16104326226] 2.161043
76231 31 [(2016-12-18 18:42:08, 03F1739N), (2016-12-18 ... [2.09176517732] 2.091765
91644 31 [(2016-12-18 14:47:09, 03F1739N), (2016-12-18 ... [2.05876552948] 2.058766
37784 31 [(2016-12-18 17:49:02, 03F1739N), (2016-12-18 ... [2.0406567631] 2.040657
166209 32 [(2016-12-18 14:01:01, 03F0961S), (2016-12-18 ... [2.02442159383] 2.024422
11878 31 [(2016-12-18 04:41:07, 01F0532S), (2016-12-18 ... [2.00445434298] 2.004454
111566 31 [(2016-12-18 12:30:18, 03F1710S), (2016-12-18 ... [1.99274670739] 1.992747
58849 32 [(2016-12-18 15:58:45, 03F1739N), (2016-12-18 ... [1.9897084048] 1.989708
189847 31 [(2016-12-18 14:39:23, 03F2336N), (2016-12-18 ... [1.9827427942] 1.982743
23439 31 [(2016-12-18 21:36:58, 01F0532S), (2016-12-18 ... [1.98150594452] 1.981506
37600 31 [(2016-12-18 18:26:29, 03F2747S), (2016-12-18 ... [1.97548929943] 1.975489
111762 31 [(2016-12-18 09:39:24, 01F3286S), (2016-12-18 ... [1.96222342148] 1.962223
168386 31 [(2016-12-18 19:24:12, 03F1739N), (2016-12-18 ... [1.91559633028] 1.915596
163276 31 [(2016-12-18 12:01:27, 03F2336N), (2016-12-18 ... [1.91082802548] 1.910828
80635 32 [(2016-12-18 20:40:06, 03F1710S), (2016-12-18 ... [1.89852700491] 1.898527
145484 5 [(2016-12-18 10:21:10, 01F0061N), (2016-12-18 ... [1.87525702536] 1.875257
10662 31 [(2016-12-18 23:18:54, 03F2777N), (2016-12-19 ... [1.86110632432] 1.861106
76004 41 [(2016-12-18 13:07:38, 03F1739N), (2016-12-18 ... [1.85831256675] 1.858313
31632 31 [(2016-12-18 21:12:33, 03F1739N), (2016-12-18 ... [1.84615384615] 1.846154
84158 31 [(2016-12-18 21:12:31, 03F1739N), (2016-12-18 ... [1.84582743989] 1.845827
126792 31 [(2016-12-18 12:31:02, 03F1710S), (2016-12-18 ... [1.84387142353] 1.843871
40713 31 [(2016-12-18 13:38:00, 03F2336N), (2016-12-18 ... [1.81056160939] 1.810562
2499 31 [(2016-12-18 21:19:55, 01F0557N), (2016-12-18 ... [1.74384809146] 1.743848
15085 41 [(2016-12-18 12:00:38, 01F3185N), (2016-12-18 ... [1.68653462591] 1.686535
109393 31 [(2016-12-18 10:42:08, 03F3187S), (2016-12-18 ... [1.64071401443] 1.640714
44921 42 [(2016-12-18 16:24:47, 01F0061N), (2016-12-18 ... [1.63773494553] 1.637735
111141 32 [(2016-12-18 10:42:07, 03F3187S), (2016-12-18 ... [1.63760424564] 1.637604
36837 31 [(2016-12-18 12:20:06, 01F3286S), (2016-12-18 ... [1.40641318222] 1.406413

2347875 rows × 4 columns


In [31]:
num_total = data.shape[0]
num_valid = valid_data.shape[0]
num_overspeed = valid_data.query("MaxSpeed > 120").shape[0]
num_total, num_valid, num_overspeed


Out[31]:
(3350606, 2347875, 108282)

In [32]:
國道違規統計[國道違規統計["違規 項目"]=="超速"]


Out[32]:
違規 項目 105年 104年 103年 102年 101年 100年 99年 98年 97年 96年
1 超速 383655 392444 415784 417088 460826 477775 493017 473948 565548 695921

Q

依照不同的車型來計算,然後繪圖。