File size: 8,244 Bytes
e6ea0f1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | """
EDA script for Amazon Review + Meta datasets.
Streams both files without full extraction to disk.
"""
import gzip, io, json, sys
from collections import Counter, defaultdict
from pathlib import Path
REVIEW_PATH = Path("/workspace/amazon/All_Amazon_Review_5.json.gz")
META_PATH = Path("/workspace/amazon/All_Amazon_Meta.json.gz")
SAMPLE_REVIEWS = 500_000 # rows to sample for review EDA
SAMPLE_META = 200_000 # rows to sample for meta EDA
# βββ helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def open_review(path):
"""Single gzip β standard JSONL."""
return gzip.open(path, "rt", encoding="utf-8", errors="replace")
def open_meta(path):
"""Double gzip β outer gz β inner gz β JSONL, fully streaming."""
f_outer = gzip.open(path, "rb")
f_inner = gzip.open(f_outer, "rt", encoding="utf-8", errors="replace")
return f_inner
def stream_jsonl(fh, max_rows, label=""):
for i, line in enumerate(fh):
if i >= max_rows:
break
if i % 100_000 == 0 and i > 0:
print(f" [{label}] {i:,} rows...", flush=True)
line = line.strip()
if not line:
continue
try:
yield json.loads(line)
except json.JSONDecodeError:
continue
# βββ Review EDA βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def eda_reviews():
print("\n" + "="*60)
print("REVIEW FILE EDA")
print("="*60)
fields_seen = Counter()
overall_dist = Counter()
verified_dist = Counter()
user_counts = Counter()
item_counts = Counter()
year_counts = Counter()
has_text = 0
has_vote = 0
total = 0
with open_review(REVIEW_PATH) as fh:
for rec in stream_jsonl(fh, SAMPLE_REVIEWS, "review"):
total += 1
for k in rec:
fields_seen[k] += 1
overall_dist[int(rec.get("overall", 0))] += 1
verified_dist[rec.get("verified", None)] += 1
user_counts[rec.get("reviewerID", "")] += 1
item_counts[rec.get("asin", "")] += 1
ts = rec.get("unixReviewTime")
if ts:
import datetime
year_counts[datetime.datetime.fromtimestamp(ts).year] += 1
if rec.get("reviewText", "").strip():
has_text += 1
if rec.get("vote"):
has_vote += 1
print(f"\nRows sampled : {total:,}")
print(f"Unique users : {len(user_counts):,}")
print(f"Unique items : {len(item_counts):,}")
print("\nField presence (%):")
for f, cnt in sorted(fields_seen.items(), key=lambda x: -x[1]):
print(f" {f:<20} {cnt/total*100:6.1f}%")
print("\nRating distribution:")
for star in sorted(overall_dist):
bar = "#" * int(overall_dist[star] / total * 50)
print(f" {star}β
{overall_dist[star]:>7,} {bar}")
print(f"\nVerified purchases: {verified_dist.get(True,0)/total*100:.1f}%")
print(f"Has review text : {has_text/total*100:.1f}%")
print(f"Has vote field : {has_vote/total*100:.1f}%")
print("\nYear distribution (sampled):")
for yr in sorted(year_counts):
bar = "#" * int(year_counts[yr] / total * 40)
print(f" {yr} {year_counts[yr]:>7,} {bar}")
print("\nUser review count distribution:")
cnt_vals = list(user_counts.values())
cnt_vals.sort()
n = len(cnt_vals)
print(f" min={cnt_vals[0]} p25={cnt_vals[n//4]} median={cnt_vals[n//2]} "
f"p75={cnt_vals[3*n//4]} p90={cnt_vals[int(n*.9)]} "
f"p99={cnt_vals[int(n*.99)]} max={cnt_vals[-1]}")
print(f" Users with >=5 reviews: {sum(1 for v in cnt_vals if v>=5):,} "
f"({sum(1 for v in cnt_vals if v>=5)/n*100:.1f}%)")
print("\nTop-10 most reviewed items:")
for asin, cnt in item_counts.most_common(10):
print(f" {asin} {cnt:,}")
# βββ Meta EDA βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def eda_meta():
print("\n" + "="*60)
print("META FILE EDA (double-gzip β loading outer layer first...)")
print("="*60)
fields_seen = Counter()
has_price = 0
has_also_buy = 0
has_also_view = 0
has_desc = 0
has_image = 0
cat_top = Counter()
price_vals = []
also_buy_lens = []
also_view_lens = []
total = 0
with open_meta(META_PATH) as fh:
for rec in stream_jsonl(fh, SAMPLE_META, "meta"):
total += 1
for k in rec:
fields_seen[k] += 1
p = rec.get("price")
if p not in (None, "", "None"):
has_price += 1
try:
price_vals.append(float(str(p).replace("$","").replace(",","")))
except ValueError:
pass
ab = rec.get("also_buy") or []
av = rec.get("also_view") or []
desc = rec.get("description") or []
if ab:
has_also_buy += 1
also_buy_lens.append(len(ab))
if av:
has_also_view += 1
also_view_lens.append(len(av))
if desc and any(d.strip() for d in (desc if isinstance(desc, list) else [desc])):
has_desc += 1
if rec.get("imageURL") or rec.get("imageURLHighRes"):
has_image += 1
cats = rec.get("category") or []
if cats:
cat_top[cats[0]] += 1
print(f"\nRows sampled : {total:,}")
print("\nField presence (%):")
for f, cnt in sorted(fields_seen.items(), key=lambda x: -x[1]):
print(f" {f:<20} {cnt/total*100:6.1f}%")
print(f"\nHas price : {has_price/total*100:.1f}%")
print(f"Has also_buy : {has_also_buy/total*100:.1f}%")
print(f"Has also_view : {has_also_view/total*100:.1f}%")
print(f"Has description : {has_desc/total*100:.1f}%")
print(f"Has image : {has_image/total*100:.1f}%")
if price_vals:
price_vals.sort()
n = len(price_vals)
print(f"\nPrice stats (USD):")
print(f" min={price_vals[0]:.2f} p25={price_vals[n//4]:.2f} "
f"median={price_vals[n//2]:.2f} p75={price_vals[3*n//4]:.2f} "
f"p90={price_vals[int(n*.9)]:.2f} p99={price_vals[int(n*.99)]:.2f} "
f"max={price_vals[-1]:.2f}")
if also_buy_lens:
also_buy_lens.sort()
n = len(also_buy_lens)
print(f"\nalso_buy length: mean={sum(also_buy_lens)/n:.1f} "
f"median={also_buy_lens[n//2]} max={also_buy_lens[-1]}")
if also_view_lens:
also_view_lens.sort()
n = len(also_view_lens)
print(f"also_view length: mean={sum(also_view_lens)/n:.1f} "
f"median={also_view_lens[n//2]} max={also_view_lens[-1]}")
print("\nTop-15 categories (first-level):")
for cat, cnt in cat_top.most_common(15):
bar = "#" * int(cnt / total * 40)
print(f" {cat[:50]:<52} {cnt:>7,} {bar}")
print("\nSample records:")
with open_meta(META_PATH) as fh:
for i, rec in enumerate(stream_jsonl(fh, 3, "meta-sample")):
print(f"\n--- Record {i+1} ---")
for k, v in rec.items():
val_str = str(v)[:120]
print(f" {k:<20}: {val_str}")
# βββ main ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
mode = sys.argv[1] if len(sys.argv) > 1 else "all"
if mode in ("all", "review"):
eda_reviews()
if mode in ("all", "meta"):
eda_meta()
print("\nDone.")
|