{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":5048,"databundleVersionId":868335}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 1. 探索性数据分析 (EDA) - 驾驶员状态检测\n\n本项目旨在通过计算机视觉技术，对行车记录仪视角下的驾驶员状态进行 10 分类识别。\n在本节中，我们将首先对官方提供的元数据进行解析，排查数据分布情况与潜在的陷阱（如数据穿越）。\n\n## 1.1 环境配置与数据加载\n\n导入数据分析必备的 Pandas、NumPy 库，以及用于数据可视化的 Matplotlib 和 Seaborn。","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-21T03:21:36.511039Z","iopub.execute_input":"2026-03-21T03:21:36.511554Z","iopub.status.idle":"2026-03-21T03:21:38.124655Z","shell.execute_reply.started":"2026-03-21T03:21:36.511524Z","shell.execute_reply":"2026-03-21T03:21:38.123525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_list=pd.read_csv(\"/kaggle/input/competitions/state-farm-distracted-driver-detection/driver_imgs_list.csv\")\nprint(image_list.describe())  #解析基础元数据\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_list.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T02:38:35.476633Z","iopub.execute_input":"2026-03-21T02:38:35.477343Z","iopub.status.idle":"2026-03-21T02:38:35.489427Z","shell.execute_reply.started":"2026-03-21T02:38:35.477311Z","shell.execute_reply":"2026-03-21T02:38:35.488642Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\n# 统计各类别数量\nclass_counts = image_list['classname'].value_counts()\n\n# 设置画布大小\nplt.figure(figsize=(12, 9))\n\n# 选择一组柔和高级的调色板\ncolors = sns.color_palette('pastel')[0:10]\n\n# 绘制基础饼图\nplt.pie(\n    class_counts.values,\n    labels=class_counts.index,\n    colors=colors,\n    autopct='%.1f%%',\n    startangle=140,\n    pctdistance=0.85,\n    labeldistance=1.05, # 防止带 C0-C9 的长标签和饼图重叠\n    wedgeprops={'edgecolor': 'white', 'linewidth': 1.5}, \n    textprops={'fontsize': 11}\n)\n\n# 画一个白色的圆放在正中间，把饼图变成“环形图”\ncentre_circle = plt.Circle((0,0), 0.65, fc='white')\nfig = plt.gcf()\nfig.gca().add_artist(centre_circle)\n\n# 添加标题并展示\nplt.title('Distribution of Driving Status Category Proportions (C0 - C9)', fontsize=16, pad=20)\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T02:41:17.932532Z","iopub.execute_input":"2026-03-21T02:41:17.932890Z","iopub.status.idle":"2026-03-21T02:41:18.123437Z","shell.execute_reply.started":"2026-03-21T02:41:17.932862Z","shell.execute_reply":"2026-03-21T02:41:18.122488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_counts=image_list['subject'].value_counts()\nplt.figure(figsize=(10, 8)) \n# 核心画图函数 plt.pie()\n# labels: 每块饼的标签 (c0, c1...)\n# autopct: 自动计算并显示百分比，'%1.1f%%' 表示保留一位小数\n# startangle: 起始角度，140度通常能让排版看起来更舒服\n# colors: 使用 matplotlib 内置的 'Paired' 调色板，让相邻的颜色区分度更高\n# wedgeprops: 加上白色的边框线，让饼图的区块切割更清晰\nplt.pie(class_counts, \n        labels=class_counts.index, \n        autopct='%1.1f%%', \n        startangle=140, \n        colors=plt.cm.Paired.colors,\n        wedgeprops={'edgecolor': 'white', 'linewidth': 1.5})\n\n# --- 3. 图表美化 ---\nplt.title('Proportion of Driver States (c0-c9)', fontsize=16, fontweight='bold')\nplt.axis('equal') \n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T02:53:35.610600Z","iopub.execute_input":"2026-03-21T02:53:35.610968Z","iopub.status.idle":"2026-03-21T02:53:35.856257Z","shell.execute_reply.started":"2026-03-21T02:53:35.610941Z","shell.execute_reply":"2026-03-21T02:53:35.855482Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"driver_class_counts = pd.crosstab(image_list['subject'], image_list['classname'])\n\n# 2. 设置更大的画布尺寸\nplt.figure(figsize=(18, 14))\n\n# 3. 绘制热力图并精细调整参数\n# ⭐️ 核心修改：cmap 换成了 'YlOrRd' (Yellow-Orange-Red) 暖色高对比度色系\nax = sns.heatmap(\n    driver_class_counts,\n    cmap='YlOrRd',\n    annot=True,\n    fmt='d',\n    linewidths=0.5,\n    linecolor='lightgray', # 加上浅灰色的网格线，让方块区分更明显\n    annot_kws={'size': 11},\n    cbar_kws={'label': 'Image Count', 'shrink': 0.8}\n)\n\n# 4. 设置标题和坐标轴标签\nplt.title('Heatmap for Cross-Distribution of Drivers (Subject) and Action Categories', fontsize=22, pad=30)\nplt.xlabel('Driving State (Categories)', fontsize=16, labelpad=20)\nplt.ylabel('Driver ID (Subject)', fontsize=16, labelpad=20)\n\n# 5. 精细控制刻度标签的旋转和字体大小\nplt.xticks(rotation=45, ha='right', fontsize=12)\nplt.yticks(rotation=0, fontsize=12)\n\n# 6. 自动调整布局\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-21T03:03:20.521274Z","iopub.execute_input":"2026-03-21T03:03:20.521645Z","iopub.status.idle":"2026-03-21T03:03:21.522520Z","shell.execute_reply.started":"2026-03-21T03:03:20.521615Z","shell.execute_reply":"2026-03-21T03:03:21.521547Z"}},"outputs":[],"execution_count":null}]}