""" ساخت دیتاست مصنوعی «پیش‌بینی مشتری در برابر خرید واقعی» Synthetic dataset: customer-provided forecasts vs. actual purchases (weekly). Run: python make_dataset.py -> writes demand_forecast.csv next to this file. """ from pathlib import Path import numpy as np import pandas as pd SEED = 42 N_CUSTOMERS = 6 N_PARTS = 30 START, END = "2023-01-02", "2025-12-29" # Mondays def build(seed: int = SEED) -> pd.DataFrame: rng = np.random.default_rng(seed) weeks = pd.date_range(START, END, freq="W-MON") # --- مشتری‌ها: هر کدام یک «عادت» در پیش‌بینی دارند --- # bias > 1 => بیش از واقع پیش‌بینی می‌کند (over-forecast) customers = pd.DataFrame({ "customer_id": [f"C{i:02d}" for i in range(1, N_CUSTOMERS + 1)], "region": rng.choice(["EU", "ASIA", "AMER"], N_CUSTOMERS), "bias": [1.35, 0.85, 1.00, 1.15, 0.95, 1.50], "noise": [0.10, 0.08, 0.25, 0.12, 0.05, 0.30], }) # --- قطعه‌ها --- families = ["Sensor", "Display", "ECU", "Connector", "Audio"] parts = pd.DataFrame({ "part_id": [f"P{i:03d}" for i in range(1, N_PARTS + 1)], "product_family": rng.choice(families, N_PARTS), "unit_price": rng.uniform(2, 120, N_PARTS).round(2), "base_demand": rng.lognormal(mean=5.0, sigma=0.7, size=N_PARTS), "trend": rng.normal(0.002, 0.003, N_PARTS), # رشد هفتگی }) # هر مشتری فقط بخشی از قطعه‌ها را می‌خرد pairs = [] for c in customers.customer_id: chosen = rng.choice(parts.part_id, size=rng.integers(8, 15), replace=False) pairs += [(c, p) for p in chosen] pairs = pd.DataFrame(pairs, columns=["customer_id", "part_id"]) grid = pairs.merge(pd.DataFrame({"week_start": weeks}), how="cross") df = grid.merge(customers, on="customer_id").merge(parts, on="part_id") t = (df.week_start - df.week_start.min()).dt.days.values / 7 woy = df.week_start.dt.isocalendar().week.astype(int).values season = 1 + 0.20 * np.sin(2 * np.pi * woy / 52) + 0.08 * np.cos(4 * np.pi * woy / 52) holiday = np.isin(woy, [1, 52, 31, 32, 33]).astype(int) # تعطیلات کریسمس و تابستان promo = (rng.random(len(df)) < 0.06).astype(int) true_demand = (df.base_demand.values * (1 + df.trend.values) ** t * season * (1 - 0.35 * holiday) * (1 + 0.40 * promo)) actual = rng.poisson(np.clip(true_demand * rng.lognormal(0, 0.10, len(df)), 0, None)) # پیش‌بینی مشتری: افق‌های ۸، ۴ و ۱ هفته قبل. هرچه نزدیک‌تر، دقیق‌تر. # مشتری از promo و holiday خبر ندارد یا کم‌توجه است. base_fc = df.base_demand.values * (1 + df.trend.values) ** t * season def fc(noise_scale): n = rng.normal(0, df.noise.values * noise_scale) return np.round(np.clip(base_fc * df.bias.values * (1 + n), 0, None)) df["forecast_8w"] = fc(1.6) df["forecast_4w"] = fc(1.0) df["forecast_1w"] = np.round(0.6 * fc(0.5) + 0.4 * actual * df.bias.values ** 0.3) df["is_holiday_week"] = holiday df["promo_flag"] = promo df["actual_qty"] = actual.astype(float) out = df[["week_start", "customer_id", "region", "part_id", "product_family", "unit_price", "is_holiday_week", "promo_flag", "forecast_8w", "forecast_4w", "forecast_1w", "actual_qty"]].copy() # --- «کثیف‌کاری» عمدی برای تمرین Data Cleaning --- n = len(out) for col, frac in [("forecast_8w", 0.06), ("forecast_4w", 0.03), ("unit_price", 0.01)]: idx = rng.choice(n, int(n * frac), replace=False) out.loc[idx, col] = np.nan idx = rng.choice(n, 25, replace=False) # خطای تایپی: یک صفر اضافه out.loc[idx, "forecast_4w"] = out.loc[idx, "forecast_4w"] * 10 idx = rng.choice(n, 8, replace=False) # مقدار منفی نامعتبر out.loc[idx, "actual_qty"] = -out.loc[idx, "actual_qty"] idx = rng.choice(out.index[out.region == "EU"], 15, replace=False) out.loc[idx, "region"] = "eu " # ناهماهنگی متنی dup = out.sample(20, random_state=seed) # ردیف تکراری out = pd.concat([out, dup]).sort_values(["week_start", "customer_id", "part_id"]) return out.reset_index(drop=True) if __name__ == "__main__": df = build() path = Path(__file__).with_name("demand_forecast.csv") df.to_csv(path, index=False) print(f"saved {len(df):,} rows -> {path}")