File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ import pandas as pd
2
+ import numpy as np
3
+
4
+
5
+ # 有三個方法帶給大家,可以大幅減少程式的執行時間
6
+
7
+ # 讀取資料型態選最快速的 | 之後如果遇到讀取資料慢時,不妨使用 pkl 檔讀取看看。 (Don't use csv)
8
+ # 多使用內建函數 | groupby+agg+ 內建函數是最快的, 如果非得需要用到自己的函式,那盡量使用 agg 會比 transform 來的快速
9
+ # 向量化的資料處理 | 採用 isin() 篩選出對應資料室最快的,速度快是因為它採用了向量化的資料處理方式(這裡的 isin() 是其中一種方式,還有其他方式
10
+
11
+
12
+ # 資料型態非常多種,在大數據的情況第一關往往都是資料讀取,以下四種資料型態進行實測,可以發現讀取速度以 pkl 檔為最快,是平常讀 csv 的 6 倍速,
13
+ # 當然不同環境與不同資料會有所差距,不過資料越多改善會越明顯。
14
+
15
+ # 題目 :
16
+
17
+ # 在速度較慢的時候,可以先從哪邊開始檢查?
18
+ print ('在速度較慢的時候,可以先從是否使用內建函數開始檢查' )
19
+ # 資料過大時應採取什麼方式讓記憶體占用量下降?
20
+ print ('資料過大時應採取: \n 1.將欄位的型態降級,不需要存太多元素在一個數字中' )
21
+ print ('2. 將整數型態 int 改成 uint 減少記憶體正用空間 int_data.apply(pd.to_numeric,downcast=unsigned' )
22
+ print ('3. 將浮點數型態 float64 改成 float32 減少記憶體正用空間 float_data.apply(pd.to_numeric,downcast=float' )
You can’t perform that action at this time.
0 commit comments