回答編集履歴

3

d

2019/05/17 08:47

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
File without changes

2

d

2019/05/17 08:47

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -63,3 +63,49 @@
63
63
 
64
64
 
65
65
  こちらの環境では、データ読み込み ~ 集計までの処理をあわせて 10.7 秒かかりました。
66
+
67
+
68
+
69
+ ## 別解
70
+
71
+
72
+
73
+ pandas を絡めると遅くなるので、データフレームにこだわらないのであれば、標準ライブラリを使って以下のように集計することもできます。
74
+
75
+ こちらのほうが10倍高速です。
76
+
77
+
78
+
79
+ ```python
80
+
81
+ import json
82
+
83
+ from collections import Counter
84
+
85
+
86
+
87
+ with open('train.json') as f:
88
+
89
+ train = json.load(f)
90
+
91
+
92
+
93
+ # 1次元配列にする。
94
+
95
+ ingredients = [item for sample in train for item in sample['ingredients']]
96
+
97
+
98
+
99
+ # 集計する。
100
+
101
+ cnt = Counter(ingredients).items()
102
+
103
+ # ---- ここまでの処理で 952 ms 秒
104
+
105
+
106
+
107
+ for key, value in cnt:
108
+
109
+ print(key, value)
110
+
111
+ ```

1

d

2019/05/17 08:47

投稿

tiitoi
tiitoi

スコア21956

test CHANGED
@@ -59,3 +59,7 @@
59
59
  Length: 6714, dtype: int64
60
60
 
61
61
  ```
62
+
63
+
64
+
65
+ こちらの環境では、データ読み込み ~ 集計までの処理をあわせて 10.7 秒かかりました。