{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":87793,"databundleVersionId":12276181,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-08-18T12:30:44.455457Z","iopub.execute_input":"2025-08-18T12:30:44.455764Z","iopub.status.idle":"2025-08-18T12:31:04.551602Z","shell.execute_reply.started":"2025-08-18T12:30:44.455743Z","shell.execute_reply":"2025-08-18T12:31:04.550823Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.ensemble import RandomForestClassifier  # Placeholder model\n\ndef load_data(paths):\n    \"\"\"\n    Load and merge training and test data.\n    If files are missing, create dummy datasets for testing.\n    \"\"\"\n    try:\n        # 嘗試讀取檔案\n        train_seq = pd.read_csv(paths[\"train_sequences\"])\n        train_lbl = pd.read_csv(paths[\"train_labels\"])\n        test_df = pd.read_csv(paths[\"test_sequences\"])\n    except FileNotFoundError:\n        print(\"⚠️ 找不到指定路徑，建立模擬資料進行測試...\")\n        # 建立模擬 train_sequences\n        train_seq = pd.DataFrame({\n            \"id\": range(1, 11),\n            \"feature1\": np.random.randn(10),\n            \"feature2\": np.random.randn(10)\n        })\n        # 建立模擬 train_labels\n        train_lbl = pd.DataFrame({\n            \"ID\": range(1, 11),\n            \"label\": np.random.choice([0, 1], size=10)\n        })\n        # 建立模擬 test_sequences\n        test_df = pd.DataFrame({\n            \"id\": range(11, 16),\n            \"feature1\": np.random.randn(5),\n            \"feature2\": np.random.randn(5)\n        })\n\n    # Merge train_seq and train_lbl on 'id' (train_seq) and 'ID' (train_lbl)\n    train_df = train_seq.merge(train_lbl, left_on='id', right_on='ID', how='inner')\n    if 'ID' in train_df.columns:\n        train_df = train_df.drop('ID', axis=1)\n\n    # Features and labels\n    X_train = train_df.drop(['id', 'label'], axis=1, errors='ignore')\n    y_train = train_df['label']\n\n    # Test data\n    test_ids = test_df['id']\n    X_test = test_df.drop('id', axis=1, errors='ignore')\n\n    return X_train, y_train, X_test, test_ids\n\ndef main():\n    # Example paths (replace with your actual paths)\n    paths = {\n        \"train_sequences\": \"path/to/train_sequences.csv\",\n        \"train_labels\": \"path/to/train_labels.csv\",\n        \"test_sequences\": \"path/to/test_sequences.csv\"\n    }\n    \n    # Load data\n    X_train, y_train, X_test, test_ids = load_data(paths)\n    \n    # Train/validation split\n    X_train_split, X_val, y_train_split, y_val = train_test_split(\n        X_train, y_train, test_size=0.2, random_state=42\n    )\n    \n    # Train model\n    model = RandomForestClassifier(random_state=42)\n    model.fit(X_train_split, y_train_split)\n    \n    # Evaluate\n    val_score = model.score(X_val, y_val)\n    print(f\"Validation accuracy: {val_score:.4f}\")\n    \n    # Predict on test set\n    predictions = model.predict(X_test)\n    \n    # Save predictions\n    submission = pd.DataFrame({'id': test_ids, 'prediction': predictions})\n    submission.to_csv('submission.csv', index=False)\n    print(\"✅ Predictions saved to submission.csv\")\n    print(submission)\n\nif __name__ == \"__main__\":\n    main()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-18T12:31:04.552662Z","iopub.execute_input":"2025-08-18T12:31:04.552911Z","iopub.status.idle":"2025-08-18T12:31:04.713805Z","shell.execute_reply.started":"2025-08-18T12:31:04.552885Z","shell.execute_reply":"2025-08-18T12:31:04.713002Z"}},"outputs":[],"execution_count":null}]}