{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":59156,"databundleVersionId":13719397,"sourceType":"competition"}],"dockerImageVersionId":31090,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"**EDA file csv**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Đọc dữ liệu\nCSV_PATH = \"/kaggle/input/MABe-mouse-behavior-detection/train.csv\"  # đổi đường dẫn nếu cần\ndf = pd.read_csv(CSV_PATH)\n\n# ===== 1. Tổng quan =====\nprint(\"Shape:\", df.shape)\nprint(\"\\n== Dtypes ==\")\nprint(df.dtypes)\nprint(\"\\n== Missing values (Top 30) ==\")\nprint(df.isnull().sum().sort_values(ascending=False).head(30))\nprint(\"\\n== Numeric summary ==\")\nprint(df.describe(include='number'))\nprint(\"\\n== Categorical summary ==\")\nprint(df.describe(include='object'))\n\n# ===== 2. Biểu đồ Missing values =====\nmissing_counts = df.isnull().sum().sort_values(ascending=False)\ntop_missing = missing_counts.head(30)\nplt.figure(figsize=(10,8))\nplt.barh(top_missing.index, top_missing.values)\nplt.title(\"Top 30 cột có giá trị thiếu nhiều nhất\")\nplt.xlabel(\"Số lượng thiếu\")\nplt.ylabel(\"Cột\")\nplt.show()\n\n# ===== 3. Histogram cho các cột số =====\nnumeric_cols = [\n    \"frames_per_second\", \"video_duration_sec\", \"pix_per_cm_approx\",\n    \"video_width_pix\", \"video_height_pix\", \"arena_width_cm\", \"arena_height_cm\"\n]\nfor c in [c for c in numeric_cols if c in df.columns]:\n    s = df[c].dropna()\n    if s.empty:\n        continue\n    plt.figure(figsize=(8,4))\n    if s.max() / max(s.min(), 1e-9) > 1e3:\n        plt.hist(s, bins=50, log=True)\n        plt.title(f\"Histogram (log-y) của {c}\")\n        plt.ylabel(\"Tần suất (log)\")\n    else:\n        plt.hist(s, bins=50)\n        plt.title(f\"Histogram của {c}\")\n        plt.ylabel(\"Tần suất\")\n    plt.xlabel(c)\n    plt.show()\n\n# ===== 4. Bar chart cho một số cột phân loại =====\ncat_cols = [\"arena_shape\", \"arena_type\", \"tracking_method\", \"mouse1_strain\", \"mouse1_sex\", \"mouse1_age\"]\nfor c in [c for c in cat_cols if c in df.columns]:\n    vc = df[c].astype(str).fillna(\"NA\").value_counts().head(20)\n    plt.figure(figsize=(10,6))\n    plt.barh(vc.index, vc.values)\n    plt.title(f\"Top 20 giá trị phổ biến của {c}\")\n    plt.xlabel(\"Số lượng\")\n    plt.ylabel(c)\n    plt.show()\n\n# ===== 5. Tỉ lệ có behaviors_labeled =====\nif \"behaviors_labeled\" in df.columns:\n    has_labels = df[\"behaviors_labeled\"].notna().astype(int)\n    counts = has_labels.value_counts().sort_index()\n    plt.figure(figsize=(6,4))\n    plt.bar([\"Không có nhãn\",\"Có nhãn\"], [counts.get(0,0), counts.get(1,0)])\n    plt.title(\"Tỉ lệ video có/không có behaviors_labeled\")\n    plt.ylabel(\"Số video\")\n    plt.show()\n\n    # Tách sơ hành vi ra top action (nếu muốn)\n    actions = []\n    for raw in df[\"behaviors_labeled\"].dropna().astype(str):\n        s = raw.strip().strip(\"[]\").replace('\"', \"\").replace(\"'\", \"\")\n        parts = [p.strip() for p in s.split(\", \")]\n        for i in range(0, len(parts), 3):\n            triple = parts[i:i+3]\n            if len(triple) == 3:\n                actions.append(triple[2])\n    if actions:\n        import pandas as pd\n        vc_act = pd.Series(actions).value_counts().head(20)\n        plt.figure(figsize=(10,6))\n        plt.barh(vc_act.index, vc_act.values)\n        plt.title(\"Top 20 hành vi trong behaviors_labeled (ước lượng)\")\n        plt.xlabel(\"Số lần xuất hiện\")\n        plt.ylabel(\"action\")\n        plt.show()\n\n# ===== 6. Scatter video dims =====\nif all(c in df.columns for c in [\"video_width_pix\",\"video_height_pix\"]):\n    plt.figure(figsize=(6,6))\n    sample = df[[\"video_width_pix\",\"video_height_pix\"]].dropna().sample(min(3000,len(df)), random_state=0)\n    plt.scatter(sample[\"video_width_pix\"], sample[\"video_height_pix\"], s=6)\n    plt.title(\"Scatter: video_width_pix vs video_height_pix (sample)\")\n    plt.xlabel(\"video_width_pix\")\n    plt.ylabel(\"video_height_pix\")\n    plt.show()\n\n# ===== 7. Scatter arena dims =====\nif all(c in df.columns for c in [\"arena_width_cm\",\"arena_height_cm\"]):\n    plt.figure(figsize=(6,6))\n    sample = df[[\"arena_width_cm\",\"arena_height_cm\"]].dropna().sample(min(3000,len(df)), random_state=0)\n    plt.scatter(sample[\"arena_width_cm\"], sample[\"arena_height_cm\"], s=6)\n    plt.title(\"Scatter: arena_width_cm vs arena_height_cm (sample)\")\n    plt.xlabel(\"arena_width_cm\")\n    plt.ylabel(\"arena_height_cm\")\n    plt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-21T09:05:18.111582Z","iopub.execute_input":"2025-09-21T09:05:18.112347Z","iopub.status.idle":"2025-09-21T09:05:21.775610Z","shell.execute_reply.started":"2025-09-21T09:05:18.112304Z","shell.execute_reply":"2025-09-21T09:05:21.774687Z"}},"outputs":[],"execution_count":null}]}