Download task_027/solution/code/solve.py from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 5.31 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_027/solution/code/solve.py
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_027/solution/code/solve.py
-
curl -L -o solve.py https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_027/solution/code/solve.py
5.31 kB
| import csv, statistics | |
| from collections import Counter | |
| from html import escape | |
| rows = list(csv.DictReader(open('diamonds.csv', encoding='utf-8-sig'))) | |
| by_cut = {} | |
| for r in rows: | |
| by_cut.setdefault(r.get('cut', 'Unknown'), []).append(float(r.get('price') or 0)) | |
| with open('cut_price_stats.csv', 'w', newline='', encoding='utf-8') as f: | |
| writer = csv.DictWriter(f, fieldnames=['cut', 'mean_price', 'median_price', 'count']) | |
| writer.writeheader() | |
| for cut, prices in by_cut.items(): | |
| writer.writerow({'cut': cut, 'mean_price': round(statistics.mean(prices), 2), | |
| 'median_price': round(statistics.median(prices), 2), 'count': len(prices)}) | |
| def price_bin(price): | |
| if price < 1000: | |
| return '0-1000' | |
| if price < 5000: | |
| return '1000-5000' | |
| return '5000+' | |
| by_bin = {} | |
| for r in rows: | |
| by_bin.setdefault(price_bin(float(r.get('price') or 0)), []).append(float(r.get('price') or 0)) | |
| with open('price_bin_stats.csv', 'w', newline='', encoding='utf-8') as f: | |
| writer = csv.DictWriter(f, fieldnames=['price_bin', 'mean_price', 'count']) | |
| writer.writeheader() | |
| for name in ('0-1000','1000-5000','5000+'): | |
| prices=by_bin.get(name,[]) | |
| writer.writerow({'price_bin': name, 'mean_price': round(statistics.mean(prices), 2) if prices else 0, 'count': len(prices)}) | |
| top = sorted(rows, key=lambda r: float(r.get('price') or 0), reverse=True)[:10] | |
| fields = list(rows[0].keys()) | |
| with open('top_10_diamonds.csv', 'w', newline='', encoding='utf-8') as f: | |
| writer = csv.DictWriter(f, fieldnames=fields) | |
| writer.writeheader() | |
| writer.writerows(top) | |
| # Average carat and price grouped by clarity grade. | |
| by_clarity = {} | |
| for r in rows: | |
| grade = r.get('clarity', 'Unknown') | |
| entry = by_clarity.setdefault(grade, {'carat': [], 'price': []}) | |
| entry['carat'].append(float(r.get('carat') or 0)) | |
| entry['price'].append(float(r.get('price') or 0)) | |
| with open('clarity_stats.csv', 'w', newline='', encoding='utf-8') as f: | |
| writer = csv.DictWriter(f, fieldnames=['clarity', 'mean_carat', 'mean_price', 'count']) | |
| writer.writeheader() | |
| for grade, vals in by_clarity.items(): | |
| writer.writerow({ | |
| 'clarity': grade, | |
| 'mean_carat': round(statistics.mean(vals['carat']), 3) if vals['carat'] else 0, | |
| 'mean_price': round(statistics.mean(vals['price']), 2) if vals['price'] else 0, | |
| 'count': len(vals['carat']), | |
| }) | |
| parts = ['<!doctype html><html><meta charset="utf-8"><title>Diamonds report</title><body>', | |
| '<style>body{font:16px system-ui;margin:24px auto;max-width:1000px;padding:0 20px;color:#243247;background:#f5f7fb}h1{margin-bottom:8px}h2{font-size:20px;margin-top:28px}table{border-collapse:collapse;background:white;min-width:360px}td,th{border:1px solid #d5dfeb;padding:7px 12px;text-align:right}th{background:#e8eef7}td:first-child,th:first-child{text-align:left}svg{display:block;max-width:100%;background:white;border:1px solid #d5dfeb;margin:12px 0}svg text{font:13px system-ui;fill:#243247}</style>', | |
| '<h1>Diamond analysis</h1><p>Fixture-derived distributions, price summaries and carat-price relationship.</p>'] | |
| for field in ('cut', 'color', 'clarity'): | |
| parts.append(f'<h2>{field} distribution</h2><table id="{field}-distribution"><tr><th>{field}</th><th>count</th></tr>') | |
| for category, count in sorted(Counter(row[field] for row in rows).items()): | |
| parts.append(f'<tr><td>{escape(category)}</td><td>{count}</td></tr>') | |
| parts.append('</table>') | |
| parts.append('<h2>Cut mean and median price</h2><table id="cut-price"><tr><th>cut</th><th>mean</th><th>median</th></tr>') | |
| for cut, prices in sorted(by_cut.items()): | |
| parts.append(f'<tr><td>{escape(cut)}</td><td>{statistics.mean(prices):.2f}</td><td>{statistics.median(prices):.2f}</td></tr>') | |
| parts.append('</table><h2>Top 10 diamonds</h2><table id="top-ten"><tr><th>price</th><th>carat</th><th>z</th></tr>') | |
| for row in top: | |
| parts.append(f'<tr><td>{row["price"]}</td><td>{row["carat"]}</td><td>{row["z"]}</td></tr>') | |
| parts.append('</table><h2>Price bins</h2><table id="price-bins"><tr><th>price_bin</th><th>count</th></tr>') | |
| for category in ('0-1000', '1000-5000', '5000+'): | |
| parts.append(f'<tr><td>{category}</td><td>{len(by_bin.get(category, []))}</td></tr>') | |
| parts.append('</table><h2>Carat versus price</h2><svg id="carat-price-scatter" width="760" height="420" viewBox="0 0 760 420" xmlns="http://www.w3.org/2000/svg">') | |
| max_carat = max(float(row['carat']) for row in rows) | |
| max_price = max(float(row['price']) for row in rows) | |
| parts.append('<path d="M70 40 V350 H700" fill="none" stroke="#243247"/>') | |
| for fraction in (0,.5,1): | |
| x=70+630*fraction | |
| y=350-300*fraction | |
| parts.append(f'<text x="{x}" y="374" text-anchor="middle">{max_carat*fraction:.3f}</text>') | |
| parts.append(f'<text x="60" y="{y+4}" text-anchor="end">{max_price*fraction:.2f}</text>') | |
| parts.append('<text x="385" y="407" text-anchor="middle">carat</text><text x="70" y="22">price</text>') | |
| for row in rows: | |
| carat, price = float(row['carat']), float(row['price']) | |
| parts.append(f'<circle cx="{70+630*carat/max_carat:.2f}" cy="{350-300*price/max_price:.2f}" r="4" fill="#396ba8"/>') | |
| parts.append('</svg></body></html>') | |
| open('report.html', 'w', encoding='utf-8').write(''.join(parts)) | |