{"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":"none","dataSources":[{"sourceId":16017,"databundleVersionId":708189,"sourceType":"competition"}],"dockerImageVersionId":31012,"isInternetEnabled":false,"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)\nimport lightgbm as lgb\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.metrics import accuracy_score\nfrom lightgbm import early_stopping, log_evaluation\nimport warnings\nwarnings.filterwarnings('ignore')\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-05-04T18:49:32.776514Z","iopub.execute_input":"2025-05-04T18:49:32.777019Z","iopub.status.idle":"2025-05-04T18:49:42.976384Z","shell.execute_reply.started":"2025-05-04T18:49:32.776910Z","shell.execute_reply":"2025-05-04T18:49:42.974886Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv',index_col=False)\ntest_df = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv',index_col=\"id\")\ndig_df = pd.read_csv('/kaggle/input/Kannada-MNIST/Dig-MNIST.csv',index_col=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T18:49:45.377558Z","iopub.execute_input":"2025-05-04T18:49:45.377858Z","iopub.status.idle":"2025-05-04T18:49:51.496067Z","shell.execute_reply.started":"2025-05-04T18:49:45.377833Z","shell.execute_reply":"2025-05-04T18:49:51.495015Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pixel_columns = [col for col in train_df.columns if col.startswith(\"pixel\")]\nX = train_df[pixel_columns].values / 255.0\ny = train_df[\"label\"].values\nX_test = test_df[pixel_columns].values / 255.0\nX_dig = dig_df[pixel_columns].values / 255.0\ny_dig = dig_df[\"label\"].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T18:49:54.320699Z","iopub.execute_input":"2025-05-04T18:49:54.321019Z","iopub.status.idle":"2025-05-04T18:49:54.792904Z","shell.execute_reply.started":"2025-05-04T18:49:54.320997Z","shell.execute_reply":"2025-05-04T18:49:54.791977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"param_grid = [\n    {\n        \"n_estimators\": 1000,\n        \"learning_rate\": 0.05,\n        \"num_leaves\": 64,\n        \"max_depth\": 10,\n        \"subsample\": 1.0,\n        \"colsample_bytree\": 1.0,\n        \"model_name\": \"LGBM_base\"\n    },\n    {\n        \"n_estimators\": 1000,\n        \"learning_rate\": 0.03,\n        \"num_leaves\": 127,\n        \"max_depth\": 15,\n        \"subsample\": 0.9,\n        \"colsample_bytree\": 0.9,\n        \"model_name\": \"LGBM_deep_wide\"\n    },\n    {\n        \"n_estimators\": 500,\n        \"learning_rate\": 0.1,\n        \"num_leaves\": 31,\n        \"max_depth\": 7,\n        \"subsample\": 0.8,\n        \"colsample_bytree\": 0.8,\n        \"model_name\": \"LGBM_fast\"\n    },\n    {\n        \"n_estimators\": 1200,\n        \"learning_rate\": 0.02,\n        \"num_leaves\": 255,\n        \"max_depth\": 20,\n        \"subsample\": 0.85,\n        \"colsample_bytree\": 0.85,\n        \"model_name\": \"LGBM_very_deep\"\n    },\n    {\n        \"n_estimators\": 800,\n        \"learning_rate\": 0.07,\n        \"num_leaves\": 40,\n        \"max_depth\": 6,\n        \"subsample\": 0.95,\n        \"colsample_bytree\": 0.95,\n        \"model_name\": \"LGBM_compact\"\n    },\n]\n\nresults = []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T18:50:00.842123Z","iopub.execute_input":"2025-05-04T18:50:00.842440Z","iopub.status.idle":"2025-05-04T18:50:00.847692Z","shell.execute_reply.started":"2025-05-04T18:50:00.842414Z","shell.execute_reply":"2025-05-04T18:50:00.846395Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for config in param_grid:\n    print(f\"\\n🔧 Training model: {config['model_name']}\")\n    skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n    test_preds = np.zeros((X_test.shape[0], 10))\n    dig_preds = np.zeros((X_dig.shape[0], 10))\n    val_scores = []\n\n    for fold, (train_idx, val_idx) in enumerate(skf.split(X, y)):\n        X_train, X_val = X[train_idx], X[val_idx]\n        y_train, y_val = y[train_idx], y[val_idx]\n\n        model = lgb.LGBMClassifier(\n            objective=\"multiclass\",\n            num_class=10,\n            random_state=42,\n            **{k: v for k, v in config.items() if k != \"model_name\"}\n        )\n\n        model.fit(\n            X_train, y_train,\n            eval_set=[(X_val, y_val)],\n            eval_metric=\"multi_logloss\",\n            callbacks=[early_stopping(20), log_evaluation(100)]\n        )\n\n        val_preds = model.predict(X_val)\n        acc = accuracy_score(y_val, val_preds)\n        val_scores.append(acc)\n        test_preds += model.predict_proba(X_test) / skf.n_splits\n        dig_preds += model.predict_proba(X_dig) / skf.n_splits\n\n    final_preds = np.argmax(test_preds, axis=1)\n    dig_final_preds = np.argmax(dig_preds, axis=1)\n    dig_acc = accuracy_score(y_dig, dig_final_preds)\n\n    results.append({\n        \"model_name\": config[\"model_name\"],\n        \"val_acc\": np.mean(val_scores),\n        \"dig_acc\": dig_acc,\n        \"submission\": final_preds\n    })","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T18:50:03.699327Z","iopub.execute_input":"2025-05-04T18:50:03.699651Z","iopub.status.idle":"2025-05-04T18:52:38.029138Z","shell.execute_reply.started":"2025-05-04T18:50:03.699623Z","shell.execute_reply":"2025-05-04T18:52:38.027956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"best_model = max(results, key=lambda x: x[\"val_acc\"])\nsubmission = pd.DataFrame({\"id\": np.arange(0, len(best_model[\"submission\"])), \"label\": best_model[\"submission\"]}).set_index('id')\nsubmission.to_csv(\"submission.csv\")\nprint(len(best_model[\"submission\"]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-04T18:52:38.050488Z","iopub.execute_input":"2025-05-04T18:52:38.050848Z","iopub.status.idle":"2025-05-04T18:52:38.070717Z","shell.execute_reply.started":"2025-05-04T18:52:38.050812Z","shell.execute_reply":"2025-05-04T18:52:38.069458Z"}},"outputs":[],"execution_count":null}]}