{"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 in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import KFold\nfrom sklearn.metrics import accuracy_score\n\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\nimport gc\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-29T15:12:41.246509Z","iopub.execute_input":"2022-08-29T15:12:41.246778Z","iopub.status.idle":"2022-08-29T15:12:42.442647Z","shell.execute_reply.started":"2022-08-29T15:12:41.246730Z","shell.execute_reply":"2022-08-29T15:12:42.442070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\nsubmission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission.head()","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2022-08-29T15:12:45.689669Z","iopub.execute_input":"2022-08-29T15:12:45.690108Z","iopub.status.idle":"2022-08-29T15:12:49.451438Z","shell.execute_reply.started":"2022-08-29T15:12:45.690066Z","shell.execute_reply":"2022-08-29T15:12:49.450659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T15:12:51.991824Z","iopub.execute_input":"2022-08-29T15:12:51.992070Z","iopub.status.idle":"2022-08-29T15:12:52.016037Z","shell.execute_reply.started":"2022-08-29T15:12:51.992027Z","shell.execute_reply":"2022-08-29T15:12:52.015191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nX = train[train.columns[1:]].values\nY = train.label.values\n\ntrain_oof = np.zeros((X.shape[0], 10))\ntest_preds = 0\ntrain_oof.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-29T15:12:54.440524Z","iopub.execute_input":"2022-08-29T15:12:54.441043Z","iopub.status.idle":"2022-08-29T15:12:54.565673Z","shell.execute_reply.started":"2022-08-29T15:12:54.440993Z","shell.execute_reply":"2022-08-29T15:12:54.565043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_splits = 5\nkf = KFold(n_splits=n_splits, random_state=137, shuffle=True)\n\nfor jj, (train_index, val_index) in enumerate(kf.split(X)):\n    print(\"Fitting fold\", jj+1)\n    train_features = X[train_index]\n    train_target = Y[train_index]\n    \n    val_features = X[val_index]\n    val_target = Y[val_index]\n    \n    model = LogisticRegression(C=15, solver='lbfgs', multi_class='multinomial', max_iter=80)\n    model.fit(train_features, train_target)\n    val_pred = model.predict_proba(val_features)\n    train_oof[val_index] = val_pred\n    print(\"Fold accuracy:\", accuracy_score(val_target, np.argmax(val_pred, axis=1)))\n    test_preds += model.predict_proba(test[test.columns[1:]].values)/n_splits\n    del train_features, train_target, val_features, val_target\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-29T15:13:12.032294Z","iopub.execute_input":"2022-08-29T15:13:12.032593Z","iopub.status.idle":"2022-08-29T15:14:37.838009Z","shell.execute_reply.started":"2022-08-29T15:13:12.032544Z","shell.execute_reply":"2022-08-29T15:14:37.837245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(accuracy_score(Y, np.argmax(train_oof, axis=1)))","metadata":{"execution":{"iopub.status.busy":"2022-08-29T15:14:49.152633Z","iopub.execute_input":"2022-08-29T15:14:49.152940Z","iopub.status.idle":"2022-08-29T15:14:49.160527Z","shell.execute_reply.started":"2022-08-29T15:14:49.152886Z","shell.execute_reply":"2022-08-29T15:14:49.159594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = np.argmax(test_preds, axis=1)\nsubmission['label'] = preds\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head(20)","metadata":{"execution":{"iopub.status.busy":"2022-08-29T15:14:52.572394Z","iopub.execute_input":"2022-08-29T15:14:52.572808Z","iopub.status.idle":"2022-08-29T15:14:54.146594Z","shell.execute_reply.started":"2022-08-29T15:14:52.572759Z","shell.execute_reply":"2022-08-29T15:14:54.145732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('test_preds', test_preds)\nnp.save('train_oof', train_oof)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_preds","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}