{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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","execution":{"iopub.status.busy":"2023-11-01T00:40:46.680576Z","iopub.execute_input":"2023-11-01T00:40:46.681053Z","iopub.status.idle":"2023-11-01T00:40:46.691142Z","shell.execute_reply.started":"2023-11-01T00:40:46.681020Z","shell.execute_reply":"2023-11-01T00:40:46.689934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import StratifiedKFold\nimport lightgbm as lgb\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:46.693018Z","iopub.execute_input":"2023-11-01T00:40:46.693737Z","iopub.status.idle":"2023-11-01T00:40:46.699526Z","shell.execute_reply.started":"2023-11-01T00:40:46.693707Z","shell.execute_reply":"2023-11-01T00:40:46.698589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/input/digit-recognizer'\nsample_submission = pd.read_csv(f'{path}/sample_submission.csv')\ntrain = pd.read_csv(f'{path}/train.csv')\ntest = pd.read_csv(f'{path}/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:46.700898Z","iopub.execute_input":"2023-11-01T00:40:46.701513Z","iopub.status.idle":"2023-11-01T00:40:52.245041Z","shell.execute_reply.started":"2023-11-01T00:40:46.701484Z","shell.execute_reply":"2023-11-01T00:40:52.243835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:52.248153Z","iopub.execute_input":"2023-11-01T00:40:52.248624Z","iopub.status.idle":"2023-11-01T00:40:52.259278Z","shell.execute_reply.started":"2023-11-01T00:40:52.248581Z","shell.execute_reply":"2023-11-01T00:40:52.258101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:52.261069Z","iopub.execute_input":"2023-11-01T00:40:52.261611Z","iopub.status.idle":"2023-11-01T00:40:52.289224Z","shell.execute_reply.started":"2023-11-01T00:40:52.261573Z","shell.execute_reply":"2023-11-01T00:40:52.287969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:52.291216Z","iopub.execute_input":"2023-11-01T00:40:52.291642Z","iopub.status.idle":"2023-11-01T00:40:52.314962Z","shell.execute_reply.started":"2023-11-01T00:40:52.291606Z","shell.execute_reply":"2023-11-01T00:40:52.313980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_fold = 5","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:52.316190Z","iopub.execute_input":"2023-11-01T00:40:52.316736Z","iopub.status.idle":"2023-11-01T00:40:52.324080Z","shell.execute_reply.started":"2023-11-01T00:40:52.316706Z","shell.execute_reply":"2023-11-01T00:40:52.322978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"skf = StratifiedKFold(n_splits=num_fold)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:52.326698Z","iopub.execute_input":"2023-11-01T00:40:52.327080Z","iopub.status.idle":"2023-11-01T00:40:52.334906Z","shell.execute_reply.started":"2023-11-01T00:40:52.327050Z","shell.execute_reply":"2023-11-01T00:40:52.333838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.drop(['label'], axis=1).values\nlabel = train['label'].values\nnum_label = np.unique(label).shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:52.336459Z","iopub.execute_input":"2023-11-01T00:40:52.336853Z","iopub.status.idle":"2023-11-01T00:40:52.465923Z","shell.execute_reply.started":"2023-11-01T00:40:52.336811Z","shell.execute_reply":"2023-11-01T00:40:52.464838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_label","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:40:52.467400Z","iopub.execute_input":"2023-11-01T00:40:52.467731Z","iopub.status.idle":"2023-11-01T00:40:52.478152Z","shell.execute_reply.started":"2023-11-01T00:40:52.467703Z","shell.execute_reply":"2023-11-01T00:40:52.477177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"models = []\nfor fold, (train_idx, test_idx) in enumerate(skf.split(X, label)):\n    X_train, y_train = X[train_idx], label[train_idx]\n    X_test, y_test = X[test_idx], label[test_idx]\n    #print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)\n    \n    train_data = lgb.Dataset(X_train, label=y_train)\n    eval_data = lgb.Dataset(X_test, label=y_test)\n    \n    params = {\n        'task': 'train',\n        'boosting_type': 'gbdt',\n        'objective': 'multiclass',\n        'num_class': num_label,\n        'verbose': -1,\n    }\n    model = lgb.train(\n        params,\n        train_data,\n        valid_sets=eval_data,\n        num_boost_round=100,\n        verbose_eval=5,\n    )\n    models.append(model)\n    y_pred = model.predict(X_test, num_iteration=model.best_iteration)\n    y_pred = np.argmax(y_pred, axis=1)\n    accuracy = accuracy_score(y_pred, y_test)\n    #conf_matrix = confusion_matrix(y_test, y_pred)\n    print(f'fold: {fold}, accuracy score: {accuracy:0.4f}')\n    #print(f'confusion_matrix: {conf_matrix:0.4f}')\n","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:50:14.945038Z","iopub.execute_input":"2023-11-01T00:50:14.945473Z","iopub.status.idle":"2023-11-01T00:56:25.788125Z","shell.execute_reply.started":"2023-11-01T00:50:14.945439Z","shell.execute_reply":"2023-11-01T00:56:25.787112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_preds = []\nX_test = test.values\nfor fold in range(num_fold):\n    y_pred = models[fold].predict(X_test)\n    y_preds.append(y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T00:56:49.709296Z","iopub.execute_input":"2023-11-01T00:56:49.709733Z","iopub.status.idle":"2023-11-01T00:56:55.479154Z","shell.execute_reply.started":"2023-11-01T00:56:49.709700Z","shell.execute_reply":"2023-11-01T00:56:55.478296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = np.stack(y_preds, axis=0).mean(axis=0)\ny_pred = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T01:00:48.597183Z","iopub.execute_input":"2023-11-01T01:00:48.597589Z","iopub.status.idle":"2023-11-01T01:00:48.608229Z","shell.execute_reply.started":"2023-11-01T01:00:48.597561Z","shell.execute_reply":"2023-11-01T01:00:48.607204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['Label'] = y_pred\nsample_submission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-01T01:03:26.109489Z","iopub.execute_input":"2023-11-01T01:03:26.109958Z","iopub.status.idle":"2023-11-01T01:03:26.181881Z","shell.execute_reply.started":"2023-11-01T01:03:26.109926Z","shell.execute_reply":"2023-11-01T01:03:26.180962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-11-01T01:03:33.058744Z","iopub.execute_input":"2023-11-01T01:03:33.059189Z","iopub.status.idle":"2023-11-01T01:03:33.069730Z","shell.execute_reply.started":"2023-11-01T01:03:33.059155Z","shell.execute_reply":"2023-11-01T01:03:33.068390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}