{"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":"gpu","dataSources":[{"sourceId":129860,"databundleVersionId":15575551,"sourceType":"competition"}],"dockerImageVersionId":31260,"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":"2026-02-10T07:03:48.922911Z","iopub.execute_input":"2026-02-10T07:03:48.923174Z","iopub.status.idle":"2026-02-10T07:03:49.984847Z","shell.execute_reply.started":"2026-02-10T07:03:48.923150Z","shell.execute_reply":"2026-02-10T07:03:49.984031Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import f1_score, classification_report\n\n# ==========================================\n# 1. CONFIGURATION\n# ==========================================\n# IMPORTANT: Replace this with the correct integer ID for 'JugglingBalls'\n# Look at your classInd.txt file to find the number.\n# Python uses 0-based indexing (e.g., if file says 1, put 0 here).\nTARGET_CLASS_ID = 12  # <--- UPDATE THIS VALUE!\n\n# File Paths (Kaggle Standard Paths)\nTRAIN_FEAT_PATH = '/kaggle/input/dartmouth-cosc-74-w26-competition-01/train_feature.npy'\nTEST_FEAT_PATH = '/kaggle/input/dartmouth-cosc-74-w26-competition-01/test_feature.npy'\nTRAIN_LABEL_PATH = '/kaggle/input/dartmouth-cosc-74-w26-competition-01/train_label.npy'\n\n# ==========================================\n# 2. LOAD DATA & PREPROCESS\n# ==========================================\nprint(\"Loading data...\")\nX_train_raw = np.load(TRAIN_FEAT_PATH)\ny_train_raw = np.load(TRAIN_LABEL_PATH)\nX_test_raw = np.load(TEST_FEAT_PATH)\n\n# FIX 1: Flatten Labels to 1D array\ny_train_full = y_train_raw.ravel()\n\n# FIX 2: Reshape 3D Features to 2D using Mean Pooling\n# Converts (Samples, 20, 2048) -> (Samples, 2048)\nprint(\"Reshaping features (Mean Pooling)...\")\nX_train_full = np.mean(X_train_raw, axis=1)\nX_test = np.mean(X_test_raw, axis=1)\n\nprint(f\"Train Shape: {X_train_full.shape}\")\nprint(f\"Test Shape:  {X_test.shape}\")\n\n# ==========================================\n# 3. TRAIN MODEL (Multiclass -> Binary)\n# ==========================================\n# We train on all 30 classes so the model learns to distinguish them.\nprint(f\"\\nTraining Logistic Regression (Target Class: {TARGET_CLASS_ID})...\")\n\nclf = LogisticRegression(\n    solver='liblinear', \n    multi_class='ovr',       # One-vs-Rest is good for binary selection\n    class_weight='balanced', # Important for imbalanced data\n    max_iter=1000, \n    random_state=42\n)\n\n# Optional: Internal Validation to check F1 Score\nX_train, X_val, y_train, y_val = train_test_split(\n    X_train_full, y_train_full, test_size=0.2, random_state=42, stratify=y_train_full\n)\n\nclf.fit(X_train, y_train)\n\n# Validation Check\nval_preds = clf.predict(X_val)\n# Create binary vectors for validation (1 for JugglingBalls, 0 for others)\nval_binary_true = (y_val == TARGET_CLASS_ID).astype(int)\nval_binary_pred = (val_preds == TARGET_CLASS_ID).astype(int)\n\nval_f1 = f1_score(val_binary_true, val_binary_pred)\nprint(f\"Validation F1 Score for Class {TARGET_CLASS_ID}: {val_f1:.4f}\")\nif val_f1 == 0.0:\n    print(\"WARNING: F1 is 0.0. Check if TARGET_CLASS_ID is correct!\")\n\n# ==========================================\n# 4. FINAL PREDICTION & SUBMISSION\n# ==========================================\nprint(\"\\nRetraining on full dataset...\")\nclf.fit(X_train_full, y_train_full)\n\n# Predict on Test Data\ntest_preds_all = clf.predict(X_test)\n\n# Convert to Binary (1 if JugglingBalls, 0 otherwise)\ntest_preds_binary = (test_preds_all == TARGET_CLASS_ID).astype(int)\n\n# Create Submission DataFrame\n# Assumes test IDs are 0, 1, 2... matching the row order\ntest_ids = np.arange(len(test_preds_binary))\n\nsubmission_df = pd.DataFrame({\n    'id': test_ids,          # Lowercase 'id'\n    'label': test_preds_binary # Lowercase 'label'\n})\n\n# Save\noutput_filename = 'submission_juggling.csv'\nsubmission_df.to_csv(output_filename, index=False)\nprint(f\"\\nSubmission saved to {output_filename}\")\nprint(submission_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-10T07:04:47.031769Z","iopub.execute_input":"2026-02-10T07:04:47.032396Z","iopub.status.idle":"2026-02-10T07:05:45.280234Z","shell.execute_reply.started":"2026-02-10T07:04:47.032346Z","shell.execute_reply":"2026-02-10T07:05:45.278772Z"}},"outputs":[],"execution_count":null}]}