{"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":"none","dataSources":[{"sourceId":59156,"databundleVersionId":13874099,"sourceType":"competition"}],"dockerImageVersionId":31153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:21.384662Z","iopub.execute_input":"2025-10-17T03:53:21.385057Z","iopub.status.idle":"2025-10-17T03:53:53.452144Z","shell.execute_reply.started":"2025-10-17T03:53:21.385030Z","shell.execute_reply":"2025-10-17T03:53:53.451110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n# 读取 parquet 文件\nfile_path = \"/kaggle/input/MABe-mouse-behavior-detection/test_tracking/AdaptableSnail/438887472.parquet\"\ndf = pd.read_parquet(file_path)\n#看数据集，看透澈，特征，鼠数\n#运行高分方案，逐函数查看，输入输出，变量","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.453680Z","iopub.execute_input":"2025-10-17T03:53:53.454422Z","iopub.status.idle":"2025-10-17T03:53:53.534254Z","shell.execute_reply.started":"2025-10-17T03:53:53.454393Z","shell.execute_reply":"2025-10-17T03:53:53.533243Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_frames = df[\"video_frame\"].nunique()\nprint(\"不同帧的数量：\", num_unique_frames)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.535236Z","iopub.execute_input":"2025-10-17T03:53:53.535490Z","iopub.status.idle":"2025-10-17T03:53:53.549707Z","shell.execute_reply.started":"2025-10-17T03:53:53.535472Z","shell.execute_reply":"2025-10-17T03:53:53.548543Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_frames = df[\"mouse_id\"].nunique()\nprint(\"不同鼠的数量：\", num_unique_frames)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.552095Z","iopub.execute_input":"2025-10-17T03:53:53.552466Z","iopub.status.idle":"2025-10-17T03:53:53.569163Z","shell.execute_reply.started":"2025-10-17T03:53:53.552436Z","shell.execute_reply":"2025-10-17T03:53:53.567770Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head(1000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.570321Z","iopub.execute_input":"2025-10-17T03:53:53.570686Z","iopub.status.idle":"2025-10-17T03:53:53.585189Z","shell.execute_reply.started":"2025-10-17T03:53:53.570661Z","shell.execute_reply":"2025-10-17T03:53:53.583840Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.tail(2000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.586414Z","iopub.execute_input":"2025-10-17T03:53:53.586768Z","iopub.status.idle":"2025-10-17T03:53:53.611904Z","shell.execute_reply.started":"2025-10-17T03:53:53.586736Z","shell.execute_reply":"2025-10-17T03:53:53.610930Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.iloc[1000:5000]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.612833Z","iopub.execute_input":"2025-10-17T03:53:53.613085Z","iopub.status.idle":"2025-10-17T03:53:53.639549Z","shell.execute_reply.started":"2025-10-17T03:53:53.613065Z","shell.execute_reply":"2025-10-17T03:53:53.638345Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# df.shape          # 查看 (行数, 列数)","metadata":{"execution":{"iopub.status.busy":"2025-10-13T10:24:14.919281Z","iopub.execute_input":"2025-10-13T10:24:14.919559Z","iopub.status.idle":"2025-10-13T10:24:14.925068Z","shell.execute_reply.started":"2025-10-13T10:24:14.919539Z","shell.execute_reply":"2025-10-13T10:24:14.924359Z"}}},{"cell_type":"code","source":"df.info()         # 查看列名、数据类型、非空数\ndf.columns.tolist()  # 查看所有列名","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.641430Z","iopub.execute_input":"2025-10-17T03:53:53.641736Z","iopub.status.idle":"2025-10-17T03:53:53.759500Z","shell.execute_reply.started":"2025-10-17T03:53:53.641713Z","shell.execute_reply":"2025-10-17T03:53:53.758414Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = \"/kaggle/input/MABe-mouse-behavior-detection/train_annotation/AdaptableSnail/1212811043.parquet\"\ndf1 = pd.read_parquet(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.760603Z","iopub.execute_input":"2025-10-17T03:53:53.760961Z","iopub.status.idle":"2025-10-17T03:53:53.772086Z","shell.execute_reply.started":"2025-10-17T03:53:53.760936Z","shell.execute_reply":"2025-10-17T03:53:53.771071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_mouses = df1[\"agent_id\"].nunique()\nprint(\"不同鼠的数量：\", num_unique_mouses)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.775859Z","iopub.execute_input":"2025-10-17T03:53:53.776155Z","iopub.status.idle":"2025-10-17T03:53:53.782451Z","shell.execute_reply.started":"2025-10-17T03:53:53.776134Z","shell.execute_reply":"2025-10-17T03:53:53.781033Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = \"/kaggle/input/MABe-mouse-behavior-detection/train_annotation/AdaptableSnail/1260392287.parquet\"\ndf11 = pd.read_parquet(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.783497Z","iopub.execute_input":"2025-10-17T03:53:53.783856Z","iopub.status.idle":"2025-10-17T03:53:53.815479Z","shell.execute_reply.started":"2025-10-17T03:53:53.783827Z","shell.execute_reply":"2025-10-17T03:53:53.814234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_mouses1 = df11[\"agent_id\"].nunique()\nprint(\"不同鼠的数量：\", num_unique_mouses1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.816937Z","iopub.execute_input":"2025-10-17T03:53:53.817294Z","iopub.status.idle":"2025-10-17T03:53:53.824840Z","shell.execute_reply.started":"2025-10-17T03:53:53.817268Z","shell.execute_reply":"2025-10-17T03:53:53.823697Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df1.head(1000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.825832Z","iopub.execute_input":"2025-10-17T03:53:53.826105Z","iopub.status.idle":"2025-10-17T03:53:53.857074Z","shell.execute_reply.started":"2025-10-17T03:53:53.826085Z","shell.execute_reply":"2025-10-17T03:53:53.855728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df1.tail(2000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.858410Z","iopub.execute_input":"2025-10-17T03:53:53.858667Z","iopub.status.idle":"2025-10-17T03:53:53.881537Z","shell.execute_reply.started":"2025-10-17T03:53:53.858646Z","shell.execute_reply":"2025-10-17T03:53:53.880071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df1.iloc[1000:5000]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.882419Z","iopub.execute_input":"2025-10-17T03:53:53.882693Z","iopub.status.idle":"2025-10-17T03:53:53.902307Z","shell.execute_reply.started":"2025-10-17T03:53:53.882665Z","shell.execute_reply":"2025-10-17T03:53:53.901456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df1.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.903489Z","iopub.execute_input":"2025-10-17T03:53:53.903970Z","iopub.status.idle":"2025-10-17T03:53:53.924424Z","shell.execute_reply.started":"2025-10-17T03:53:53.903929Z","shell.execute_reply":"2025-10-17T03:53:53.923403Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df1.info()         # 查看列名、数据类型、非空数\ndf1.columns.tolist()  # 查看所有列名","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.925491Z","iopub.execute_input":"2025-10-17T03:53:53.925986Z","iopub.status.idle":"2025-10-17T03:53:53.950779Z","shell.execute_reply.started":"2025-10-17T03:53:53.925958Z","shell.execute_reply":"2025-10-17T03:53:53.950009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = \"/kaggle/input/MABe-mouse-behavior-detection/train_annotation/BoisterousParrot/1059582964.parquet\"\ndf2 = pd.read_parquet(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.951710Z","iopub.execute_input":"2025-10-17T03:53:53.952758Z","iopub.status.idle":"2025-10-17T03:53:53.976530Z","shell.execute_reply.started":"2025-10-17T03:53:53.952712Z","shell.execute_reply":"2025-10-17T03:53:53.975373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_mouses2 = df2[\"agent_id\"].nunique()\nprint(\"不同鼠的数量：\", num_unique_mouses2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.977568Z","iopub.execute_input":"2025-10-17T03:53:53.977889Z","iopub.status.idle":"2025-10-17T03:53:53.985283Z","shell.execute_reply.started":"2025-10-17T03:53:53.977859Z","shell.execute_reply":"2025-10-17T03:53:53.984338Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df2.head(1000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:53.986407Z","iopub.execute_input":"2025-10-17T03:53:53.986890Z","iopub.status.idle":"2025-10-17T03:53:54.015701Z","shell.execute_reply.started":"2025-10-17T03:53:53.986859Z","shell.execute_reply":"2025-10-17T03:53:54.014099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df2.tail(2000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.016719Z","iopub.execute_input":"2025-10-17T03:53:54.017092Z","iopub.status.idle":"2025-10-17T03:53:54.039304Z","shell.execute_reply.started":"2025-10-17T03:53:54.017063Z","shell.execute_reply":"2025-10-17T03:53:54.037867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df2.iloc[1000:5000]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.040426Z","iopub.execute_input":"2025-10-17T03:53:54.040706Z","iopub.status.idle":"2025-10-17T03:53:54.062026Z","shell.execute_reply.started":"2025-10-17T03:53:54.040684Z","shell.execute_reply":"2025-10-17T03:53:54.060655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df2.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.063204Z","iopub.execute_input":"2025-10-17T03:53:54.063550Z","iopub.status.idle":"2025-10-17T03:53:54.084241Z","shell.execute_reply.started":"2025-10-17T03:53:54.063521Z","shell.execute_reply":"2025-10-17T03:53:54.083199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df2.columns.tolist()  # 查看所有列名","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.085378Z","iopub.execute_input":"2025-10-17T03:53:54.085710Z","iopub.status.idle":"2025-10-17T03:53:54.106291Z","shell.execute_reply.started":"2025-10-17T03:53:54.085680Z","shell.execute_reply":"2025-10-17T03:53:54.105247Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = \"/kaggle/input/MABe-mouse-behavior-detection/train_annotation/CRIM13/1009459450.parquet\"\ndf3 = pd.read_parquet(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.107593Z","iopub.execute_input":"2025-10-17T03:53:54.108266Z","iopub.status.idle":"2025-10-17T03:53:54.126318Z","shell.execute_reply.started":"2025-10-17T03:53:54.108230Z","shell.execute_reply":"2025-10-17T03:53:54.125216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_mouses3= df3[\"agent_id\"].nunique()\nprint(\"不同鼠的数量：\", num_unique_mouses3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.127322Z","iopub.execute_input":"2025-10-17T03:53:54.127631Z","iopub.status.idle":"2025-10-17T03:53:54.135710Z","shell.execute_reply.started":"2025-10-17T03:53:54.127603Z","shell.execute_reply":"2025-10-17T03:53:54.134845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df3.head(1000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.136672Z","iopub.execute_input":"2025-10-17T03:53:54.137030Z","iopub.status.idle":"2025-10-17T03:53:54.162264Z","shell.execute_reply.started":"2025-10-17T03:53:54.137010Z","shell.execute_reply":"2025-10-17T03:53:54.161064Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df3.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.167348Z","iopub.execute_input":"2025-10-17T03:53:54.167802Z","iopub.status.idle":"2025-10-17T03:53:54.181892Z","shell.execute_reply.started":"2025-10-17T03:53:54.167668Z","shell.execute_reply":"2025-10-17T03:53:54.180814Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df3.columns.tolist()  ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.182990Z","iopub.execute_input":"2025-10-17T03:53:54.183873Z","iopub.status.idle":"2025-10-17T03:53:54.202366Z","shell.execute_reply.started":"2025-10-17T03:53:54.183697Z","shell.execute_reply":"2025-10-17T03:53:54.201157Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = \"/kaggle/input/MABe-mouse-behavior-detection/train_tracking/BoisterousParrot/1059582964.parquet\"\ndf4 = pd.read_parquet(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.203557Z","iopub.execute_input":"2025-10-17T03:53:54.204102Z","iopub.status.idle":"2025-10-17T03:53:54.620763Z","shell.execute_reply.started":"2025-10-17T03:53:54.204064Z","shell.execute_reply":"2025-10-17T03:53:54.619836Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_frames1 = df4[\"video_frame\"].nunique()\nprint(\"不同帧的数量：\", num_unique_frames1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.621758Z","iopub.execute_input":"2025-10-17T03:53:54.622070Z","iopub.status.idle":"2025-10-17T03:53:54.663983Z","shell.execute_reply.started":"2025-10-17T03:53:54.622039Z","shell.execute_reply":"2025-10-17T03:53:54.663045Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_mouse = df4[\"mouse_id\"].nunique()\nprint(\"不同鼠的数量：\", num_unique_mouse)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.665648Z","iopub.execute_input":"2025-10-17T03:53:54.665946Z","iopub.status.idle":"2025-10-17T03:53:54.700168Z","shell.execute_reply.started":"2025-10-17T03:53:54.665924Z","shell.execute_reply":"2025-10-17T03:53:54.699182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df4.head(1000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.701273Z","iopub.execute_input":"2025-10-17T03:53:54.701565Z","iopub.status.idle":"2025-10-17T03:53:54.713605Z","shell.execute_reply.started":"2025-10-17T03:53:54.701541Z","shell.execute_reply":"2025-10-17T03:53:54.712479Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df4.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.714546Z","iopub.execute_input":"2025-10-17T03:53:54.714831Z","iopub.status.idle":"2025-10-17T03:53:54.733761Z","shell.execute_reply.started":"2025-10-17T03:53:54.714808Z","shell.execute_reply":"2025-10-17T03:53:54.732485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df4.columns.tolist()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.735134Z","iopub.execute_input":"2025-10-17T03:53:54.735591Z","iopub.status.idle":"2025-10-17T03:53:54.753563Z","shell.execute_reply.started":"2025-10-17T03:53:54.735563Z","shell.execute_reply":"2025-10-17T03:53:54.752311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = \"/kaggle/input/MABe-mouse-behavior-detection/train.csv\"\ndf5 = pd.read_csv(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.754655Z","iopub.execute_input":"2025-10-17T03:53:54.755090Z","iopub.status.idle":"2025-10-17T03:53:54.872029Z","shell.execute_reply.started":"2025-10-17T03:53:54.755042Z","shell.execute_reply":"2025-10-17T03:53:54.871057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df5.head(1000)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.873423Z","iopub.execute_input":"2025-10-17T03:53:54.873682Z","iopub.status.idle":"2025-10-17T03:53:54.901688Z","shell.execute_reply.started":"2025-10-17T03:53:54.873657Z","shell.execute_reply":"2025-10-17T03:53:54.900672Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_unique_labs = df5[\"lab_id\"].nunique()\nprint(\"不同实验室的数量：\", num_unique_labs)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.902889Z","iopub.execute_input":"2025-10-17T03:53:54.903200Z","iopub.status.idle":"2025-10-17T03:53:54.909639Z","shell.execute_reply.started":"2025-10-17T03:53:54.903177Z","shell.execute_reply":"2025-10-17T03:53:54.908741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df5.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.910876Z","iopub.execute_input":"2025-10-17T03:53:54.911262Z","iopub.status.idle":"2025-10-17T03:53:54.930015Z","shell.execute_reply.started":"2025-10-17T03:53:54.911240Z","shell.execute_reply":"2025-10-17T03:53:54.929001Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# =====================================================\n# 🐭 MABe Challenge - Social Action Recognition in Mice\n# Exploratory Data Analysis (EDA) Notebook\n# =====================================================\n\nimport os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom tqdm import tqdm\n\nplt.style.use('ggplot')\n\n# ============ 1️⃣ 基本路径设置 ============\n\nBASE_DIR = \"/kaggle/input/MABe-mouse-behavior-detection\"\n\n# 可修改查看训练或测试\ntracking_dir = os.path.join(BASE_DIR, \"train_tracking\")\nannotation_dir = os.path.join(BASE_DIR, \"train_annotation\")\n\n# 元数据文件\ntrain_meta_path = os.path.join(BASE_DIR, \"train.csv\")\n\nprint(\"📂 数据集目录结构：\")\nfor folder in os.listdir(BASE_DIR):\n    print(\"  -\", folder)\n\n\n# ============ 2️⃣ 查看元数据文件结构 ============\n\ntrain_meta = pd.read_csv(train_meta_path)\nprint(\"\\n📋 train.csv 基本信息：\")\nprint(train_meta.head())\n\nprint(\"\\n🔢 元数据列：\", train_meta.columns.tolist())\n\nprint(\"\\n📊 实验室分布：\")\nprint(train_meta['lab_id'].value_counts())\n\n\n# ============ 3️⃣ 选择一个视频样本进行分析 ============\n\nsample_video = train_meta.iloc[0]['video_id']\nsample_lab = train_meta.iloc[0]['lab_id']\nprint(f\"\\n🎞️ 示例视频: {sample_video} | 实验室: {sample_lab}\")\n\nsample_tracking_path = os.path.join(tracking_dir, f\"{sample_lab}/{sample_video}.parquet\")\nsample_annotation_path = os.path.join(annotation_dir, f\"{sample_lab}/{sample_video}.parquet\")\n\ndf_track = pd.read_parquet(sample_tracking_path)\ndf_anno = pd.read_parquet(sample_annotation_path)\n\nprint(\"\\n✅ Tracking 数据：\")\nprint(df_track.head())\nprint(\"\\n📏 Tracking 数据维度:\", df_track.shape)\nprint(\"📌 Tracking 列名:\", df_track.columns.tolist())\nprint(\"--------------------------------------------------\")\nprint(\"\\n✅ Annotation 数据：\")\nprint(df_anno.head())\nprint(\"\\n📏 Annotation 数据维度:\", df_anno.shape)\nprint(\"📌 Annotation 列名:\", df_anno.columns.tolist())\n\n\n# ============ 4️⃣ Tracking 数据分析 ============\n\n# 查看 bodypart 种类\nbodypart_counts = df_track['bodypart'].value_counts()\nprint(\"\\n🐁 Tracking中bodypart种类分布:\")\nprint(bodypart_counts)\n\nprint(\"# 可视化身体部位分布\")\nplt.figure(figsize=(10,5))\nsns.barplot(x=bodypart_counts.index, y=bodypart_counts.values)\nplt.title(\"Bodyparts Distribution\")\nplt.xticks(rotation=45)\nplt.show()\n\n# 鼠ID分布\nplt.figure(figsize=(8,4))\nsns.countplot(x='mouse_id', data=df_track)\nplt.title(\"Mouse ID Distribution\")\nplt.show()\n\n\n# ============ 5️⃣ 坐标范围分析 ============\n\nplt.figure(figsize=(6,6))\nsns.scatterplot(x='x', y='y', data=df_track.sample(5000))\nplt.title(\"Bodypart Positions Sample (5000 points)\")\nplt.show()\n\nprint(\"\\n📏 坐标范围：\")\nprint(\"x:\", (df_track['x'].min(), df_track['x'].max()))\nprint(\"y:\", (df_track['y'].min(), df_track['y'].max()))\n\n\n# ============ 6️⃣ Annotation 数据分析 ============\n\nprint(\"\\n📊 行为类别统计:\")\naction_counts = df_anno['action'].value_counts()\nprint(action_counts)\n\nplt.figure(figsize=(10,5))\nsns.barplot(x=action_counts.index, y=action_counts.values)\nplt.title(\"Action Label Distribution\")\nplt.xticks(rotation=45)\nplt.show()\n\n# 行为持续时间统计\ndf_anno['duration'] = df_anno['stop_frame'] - df_anno['start_frame']\nplt.figure(figsize=(8,4))\nsns.histplot(df_anno['duration'], bins=50)\nplt.title(\"Behavior Duration Distribution (frames)\")\nplt.show()\n\nprint(\"\\n⏱️ 平均行为持续帧数:\", df_anno['duration'].mean())\n\n\n# ============ 7️⃣ Agent-Target 关系 ============\n\nprint(\"\\n🤝 Agent-Target 分析:\")\nprint(df_anno[['agent_id', 'target_id']].value_counts().head())\n\nplt.figure(figsize=(6,4))\nsns.countplot(x=df_anno['agent_id']==df_anno['target_id'])\nplt.title(\"Is Single-Mouse Behavior?\")\nplt.xlabel(\"agent == target\")\nplt.show()\n\n\n# ============ 8️⃣ 鼠间距离可视化（示例） ============\n\n# 选取两只鼠（假设存在 mouse_id 1 和 2）\nif df_track['mouse_id'].nunique() >= 2:\n    m1 = df_track[df_track['mouse_id'] == 1]\n    m2 = df_track[df_track['mouse_id'] == 2]\n    \n    # nose 坐标\n    nose1 = m1[m1['bodypart']=='nose'].sort_values('video_frame')\n    nose2 = m2[m2['bodypart']=='nose'].sort_values('video_frame')\n\n    # 对齐帧数\n    n = min(len(nose1), len(nose2))\n    dist = np.sqrt((nose1['x'].iloc[:n] - nose2['x'].iloc[:n])**2 + \n                   (nose1['y'].iloc[:n] - nose2['y'].iloc[:n])**2)\n\n    plt.figure(figsize=(10,4))\n    plt.plot(dist[:2000])\n    plt.title(\"Nose-Nose Distance (first 2000 frames)\")\n    plt.xlabel(\"Frame\")\n    plt.ylabel(\"Distance (pixels)\")\n    plt.show()\nelse:\n    print(\"⚠️ 数据中只有一只鼠，跳过距离可视化。\")\n\n\n# ============ 9️⃣ 全局行为统计（可扩展） ============\n\nprint(\"\\n📈 行为类型总览：\")\nglobal_counts = []\n\nfor lab in tqdm(os.listdir(annotation_dir)):\n    lab_path = os.path.join(annotation_dir, lab)\n    for file in os.listdir(lab_path):\n        fpath = os.path.join(lab_path, file)\n        ann = pd.read_parquet(fpath)\n        for act, cnt in ann['action'].value_counts().items():\n            global_counts.append({'lab': lab, 'action': act, 'count': cnt})\n\nglobal_df = pd.DataFrame(global_counts)\nplt.figure(figsize=(10,5))\nsns.barplot(data=global_df, x='action', y='count', estimator=sum)\nplt.title(\"Overall Action Distribution Across Labs\")\nplt.xticks(rotation=45)\nplt.show()\n\nprint(\"✅ 数据探索分析完成！\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-17T03:53:54.930921Z","iopub.execute_input":"2025-10-17T03:53:54.931195Z","iopub.status.idle":"2025-10-17T03:54:06.198922Z","shell.execute_reply.started":"2025-10-17T03:53:54.931173Z","shell.execute_reply":"2025-10-17T03:54:06.197869Z"}},"outputs":[],"execution_count":null}]}