{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\n\ntrain = pd.read_csv('../input/Kannada-MNIST/train.csv')\ntest = pd.read_csv('../input/Kannada-MNIST/test.csv')\nprint(f'trainデータ数：{len(train)}\\n')\nprint(f'testデータ数：{len(test)}')","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:03:26.542832Z","iopub.execute_input":"2021-07-04T17:03:26.543322Z","iopub.status.idle":"2021-07-04T17:03:31.670895Z","shell.execute_reply.started":"2021-07-04T17:03:26.543293Z","shell.execute_reply":"2021-07-04T17:03:31.669734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train[\"label\"]\ntrain_x = train.drop([\"label\"],axis=1)\ntrain_y,val_y,train_x,val_x = train_y[:50000],train_y[50000:],train_x[:50000],train_x[50000:]\n\ntest_y = test[\"id\"]\ntest_x = test.drop([\"id\"],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:03:34.523687Z","iopub.execute_input":"2021-07-04T17:03:34.524034Z","iopub.status.idle":"2021-07-04T17:03:34.683078Z","shell.execute_reply.started":"2021-07-04T17:03:34.523995Z","shell.execute_reply":"2021-07-04T17:03:34.682168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = np.array(train_x).reshape(-1,28,28,1)\nval_x = np.array(val_x).reshape(-1,28,28,1)\ntest_x = np.array(test_x).reshape(-1,28,28,1)\n\ntrain_x = train_x.astype('float32')\nval_x = val_x.astype('float32')\ntest_x = test_x.astype('float32')\n\ntrain_x /= 255\nval_x /= 255\ntest_x /= 255","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:03:56.548756Z","iopub.execute_input":"2021-07-04T17:03:56.549137Z","iopub.status.idle":"2021-07-04T17:03:56.80493Z","shell.execute_reply.started":"2021-07-04T17:03:56.549105Z","shell.execute_reply":"2021-07-04T17:03:56.803951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\ntrain_y = to_categorical(train_y,num_classes=10)\nval_y = to_categorical(val_y,num_classes=10)\ntrain_y[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:04:05.290919Z","iopub.execute_input":"2021-07-04T17:04:05.291271Z","iopub.status.idle":"2021-07-04T17:04:10.396448Z","shell.execute_reply.started":"2021-07-04T17:04:05.291243Z","shell.execute_reply":"2021-07-04T17:04:10.395764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential,load_model\nfrom keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPooling2D\n\nmodel = Sequential()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:04:18.25133Z","iopub.execute_input":"2021-07-04T17:04:18.251809Z","iopub.status.idle":"2021-07-04T17:04:18.285541Z","shell.execute_reply.started":"2021-07-04T17:04:18.251779Z","shell.execute_reply":"2021-07-04T17:04:18.284631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Conv2D(32,kernel_size=(5,5),activation='relu',input_shape=(28,28,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(64,kernel_size=(5,5),activation='relu',input_shape=(28,28,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:04:28.099643Z","iopub.execute_input":"2021-07-04T17:04:28.100005Z","iopub.status.idle":"2021-07-04T17:04:28.17568Z","shell.execute_reply.started":"2021-07-04T17:04:28.099973Z","shell.execute_reply":"2021-07-04T17:04:28.174926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='sgd',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:04:34.358278Z","iopub.execute_input":"2021-07-04T17:04:34.358745Z","iopub.status.idle":"2021-07-04T17:04:34.37414Z","shell.execute_reply.started":"2021-07-04T17:04:34.358716Z","shell.execute_reply":"2021-07-04T17:04:34.3728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_x,train_y,batch_size=256,epochs=20,validation_data=(val_x,val_y))","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:04:43.477981Z","iopub.execute_input":"2021-07-04T17:04:43.478318Z","iopub.status.idle":"2021-07-04T17:09:11.3839Z","shell.execute_reply.started":"2021-07-04T17:04:43.478289Z","shell.execute_reply":"2021-07-04T17:09:11.382986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_value = model.predict(test_x)\npredict_value2=[]\nfor item in predict_value:\n    value2=np.argmax(item)\n    predict_value2+=[value2]","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:10:02.705028Z","iopub.execute_input":"2021-07-04T17:10:02.705374Z","iopub.status.idle":"2021-07-04T17:10:03.259223Z","shell.execute_reply.started":"2021-07-04T17:10:02.705346Z","shell.execute_reply":"2021-07-04T17:10:03.258356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit=pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmit['label']=predict_value2\nsubmit.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T17:10:06.35204Z","iopub.execute_input":"2021-07-04T17:10:06.352718Z","iopub.status.idle":"2021-07-04T17:10:06.394917Z","shell.execute_reply.started":"2021-07-04T17:10:06.352666Z","shell.execute_reply":"2021-07-04T17:10:06.392747Z"},"trusted":true},"execution_count":null,"outputs":[]}]}