{"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":"import numpy as np\nimport pandas as pd\nimport os","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.027521,"end_time":"2021-04-13T02:14:10.012685","exception":false,"start_time":"2021-04-13T02:14:09.985164","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:10.447481Z","iopub.execute_input":"2021-06-22T14:09:10.447816Z","iopub.status.idle":"2021-06-22T14:09:10.453103Z","shell.execute_reply.started":"2021-06-22T14:09:10.447784Z","shell.execute_reply":"2021-06-22T14:09:10.451977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.layers import Flatten\nfrom keras.callbacks import EarlyStopping\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.utils import np_utils\n\nfrom sklearn.preprocessing import LabelBinarizer\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","papermill":{"duration":7.182562,"end_time":"2021-04-13T02:14:17.209923","exception":false,"start_time":"2021-04-13T02:14:10.027361","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:10.460773Z","iopub.execute_input":"2021-06-22T14:09:10.461018Z","iopub.status.idle":"2021-06-22T14:09:10.467362Z","shell.execute_reply.started":"2021-06-22T14:09:10.460994Z","shell.execute_reply":"2021-06-22T14:09:10.466458Z"},"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')","metadata":{"papermill":{"duration":5.249793,"end_time":"2021-04-13T02:14:22.473405","exception":false,"start_time":"2021-04-13T02:14:17.223612","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:10.469255Z","iopub.execute_input":"2021-06-22T14:09:10.469695Z","iopub.status.idle":"2021-06-22T14:09:13.735738Z","shell.execute_reply.started":"2021-06-22T14:09:10.469659Z","shell.execute_reply":"2021-06-22T14:09:13.734879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\ntrain.head()","metadata":{"papermill":{"duration":0.055463,"end_time":"2021-04-13T02:14:22.542899","exception":false,"start_time":"2021-04-13T02:14:22.487436","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.738701Z","iopub.execute_input":"2021-06-22T14:09:13.739289Z","iopub.status.idle":"2021-06-22T14:09:13.758327Z","shell.execute_reply.started":"2021-06-22T14:09:13.739249Z","shell.execute_reply":"2021-06-22T14:09:13.757492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test.shape)\ntest.head()","metadata":{"papermill":{"duration":0.035275,"end_time":"2021-04-13T02:14:22.593049","exception":false,"start_time":"2021-04-13T02:14:22.557774","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.759940Z","iopub.execute_input":"2021-06-22T14:09:13.760512Z","iopub.status.idle":"2021-06-22T14:09:13.780842Z","shell.execute_reply.started":"2021-06-22T14:09:13.760473Z","shell.execute_reply":"2021-06-22T14:09:13.780068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.iloc[:,1:]\ny0 = train.iloc[:,0]\n\ntestX = test.iloc[:,1:]","metadata":{"papermill":{"duration":0.02369,"end_time":"2021-04-13T02:14:22.633357","exception":false,"start_time":"2021-04-13T02:14:22.609667","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.782000Z","iopub.execute_input":"2021-06-22T14:09:13.782658Z","iopub.status.idle":"2021-06-22T14:09:13.787465Z","shell.execute_reply.started":"2021-06-22T14:09:13.782621Z","shell.execute_reply":"2021-06-22T14:09:13.786578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.head()","metadata":{"papermill":{"duration":0.03715,"end_time":"2021-04-13T02:14:22.686348","exception":false,"start_time":"2021-04-13T02:14:22.649198","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.788730Z","iopub.execute_input":"2021-06-22T14:09:13.789371Z","iopub.status.idle":"2021-06-22T14:09:13.811753Z","shell.execute_reply.started":"2021-06-22T14:09:13.789318Z","shell.execute_reply":"2021-06-22T14:09:13.810996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y0","metadata":{"papermill":{"duration":0.026311,"end_time":"2021-04-13T02:14:22.728697","exception":false,"start_time":"2021-04-13T02:14:22.702386","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.812979Z","iopub.execute_input":"2021-06-22T14:09:13.813344Z","iopub.status.idle":"2021-06-22T14:09:13.821291Z","shell.execute_reply.started":"2021-06-22T14:09:13.813310Z","shell.execute_reply":"2021-06-22T14:09:13.820134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"binencoder = LabelBinarizer()\ny = binencoder.fit_transform(y0)\ny","metadata":{"papermill":{"duration":0.037196,"end_time":"2021-04-13T02:14:22.782264","exception":false,"start_time":"2021-04-13T02:14:22.745068","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.822985Z","iopub.execute_input":"2021-06-22T14:09:13.823530Z","iopub.status.idle":"2021-06-22T14:09:13.844418Z","shell.execute_reply.started":"2021-06-22T14:09:13.823492Z","shell.execute_reply":"2021-06-22T14:09:13.843489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_images = X.values.reshape(-1,28,28)\ntest_images = testX.values.reshape(-1,28,28)\n\nprint(X_images.shape)\nprint(test_images.shape)","metadata":{"papermill":{"duration":0.026429,"end_time":"2021-04-13T02:14:22.825891","exception":false,"start_time":"2021-04-13T02:14:22.799462","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.847396Z","iopub.execute_input":"2021-06-22T14:09:13.847736Z","iopub.status.idle":"2021-06-22T14:09:13.853184Z","shell.execute_reply.started":"2021-06-22T14:09:13.847702Z","shell.execute_reply":"2021-06-22T14:09:13.852227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_images[5])\nplt.show()","metadata":{"papermill":{"duration":0.162334,"end_time":"2021-04-13T02:14:23.005254","exception":false,"start_time":"2021-04-13T02:14:22.84292","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.855320Z","iopub.execute_input":"2021-06-22T14:09:13.855733Z","iopub.status.idle":"2021-06-22T14:09:13.974549Z","shell.execute_reply.started":"2021-06-22T14:09:13.855695Z","shell.execute_reply":"2021-06-22T14:09:13.973484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X_images, y, test_size = 0.2, random_state=90)","metadata":{"papermill":{"duration":0.437013,"end_time":"2021-04-13T02:14:23.460099","exception":false,"start_time":"2021-04-13T02:14:23.023086","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:13.975984Z","iopub.execute_input":"2021-06-22T14:09:13.976333Z","iopub.status.idle":"2021-06-22T14:09:14.514651Z","shell.execute_reply.started":"2021-06-22T14:09:13.976296Z","shell.execute_reply":"2021-06-22T14:09:14.513770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"papermill":{"duration":0.025801,"end_time":"2021-04-13T02:14:23.503614","exception":false,"start_time":"2021-04-13T02:14:23.477813","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:14.516009Z","iopub.execute_input":"2021-06-22T14:09:14.516378Z","iopub.status.idle":"2021-06-22T14:09:14.522365Z","shell.execute_reply.started":"2021-06-22T14:09:14.516339Z","shell.execute_reply":"2021-06-22T14:09:14.521399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255\nX_test = X_test/255\n\nX_train = X_train.reshape(-1,28,28,1).astype('float32')\nX_test = X_test.reshape(-1,28,28,1).astype('float32')","metadata":{"papermill":{"duration":0.2087,"end_time":"2021-04-13T02:14:23.730908","exception":false,"start_time":"2021-04-13T02:14:23.522208","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:14.523850Z","iopub.execute_input":"2021-06-22T14:09:14.524292Z","iopub.status.idle":"2021-06-22T14:09:14.740542Z","shell.execute_reply.started":"2021-06-22T14:09:14.524255Z","shell.execute_reply":"2021-06-22T14:09:14.739682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images2 = test_images/255\ntest = test_images2.reshape(-1,28,28,1).astype('float32')","metadata":{"papermill":{"duration":0.133886,"end_time":"2021-04-13T02:14:23.883128","exception":false,"start_time":"2021-04-13T02:14:23.749242","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:14.741862Z","iopub.execute_input":"2021-06-22T14:09:14.742220Z","iopub.status.idle":"2021-06-22T14:09:14.759858Z","shell.execute_reply.started":"2021-06-22T14:09:14.742182Z","shell.execute_reply":"2021-06-22T14:09:14.759153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_model = Sequential()\n\nconv_model.add(Conv2D(32,(4,4),input_shape = (28,28,1),activation = 'relu'))\nconv_model.add(MaxPooling2D(pool_size=(2,2)))\nconv_model.add(Conv2D(64,(3,3),activation = 'relu'))\nconv_model.add(MaxPooling2D(pool_size=(2,2)))\nconv_model.add(Dropout(0.2))\nconv_model.add(Flatten())\nconv_model.add(Dense(128,activation='relu'))\nconv_model.add(Dense(50, activation='relu'))\nconv_model.add(Dense(10, activation='softmax'))\nconv_model.compile(loss='categorical_crossentropy', optimizer='adam',metrics=['accuracy'])\n","metadata":{"papermill":{"duration":0.162772,"end_time":"2021-04-13T02:14:24.063988","exception":false,"start_time":"2021-04-13T02:14:23.901216","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:14.761102Z","iopub.execute_input":"2021-06-22T14:09:14.761454Z","iopub.status.idle":"2021-06-22T14:09:14.824799Z","shell.execute_reply.started":"2021-06-22T14:09:14.761419Z","shell.execute_reply":"2021-06-22T14:09:14.824033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conv_model.summary()","metadata":{"papermill":{"duration":0.031272,"end_time":"2021-04-13T02:14:24.113729","exception":false,"start_time":"2021-04-13T02:14:24.082457","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:14.826037Z","iopub.execute_input":"2021-06-22T14:09:14.826384Z","iopub.status.idle":"2021-06-22T14:09:14.836921Z","shell.execute_reply.started":"2021-06-22T14:09:14.826348Z","shell.execute_reply":"2021-06-22T14:09:14.835833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = conv_model.fit(X_train, y_train, validation_split=0.2, epochs=20, batch_size=92, verbose=2)\nresult","metadata":{"papermill":{"duration":304.053557,"end_time":"2021-04-13T02:19:28.186186","exception":false,"start_time":"2021-04-13T02:14:24.132629","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:14.838495Z","iopub.execute_input":"2021-06-22T14:09:14.838936Z","iopub.status.idle":"2021-06-22T14:09:55.615970Z","shell.execute_reply.started":"2021-06-22T14:09:14.838899Z","shell.execute_reply":"2021-06-22T14:09:55.614370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(result.history)\nhistory_df.loc[:, ['loss', 'val_loss', 'accuracy', 'val_accuracy']].plot()","metadata":{"papermill":{"duration":0.401879,"end_time":"2021-04-13T02:19:28.621701","exception":false,"start_time":"2021-04-13T02:19:28.219822","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:55.617254Z","iopub.execute_input":"2021-06-22T14:09:55.617632Z","iopub.status.idle":"2021-06-22T14:09:55.780185Z","shell.execute_reply.started":"2021-06-22T14:09:55.617582Z","shell.execute_reply":"2021-06-22T14:09:55.779253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores_train = conv_model.evaluate(X_train, y_train, verbose=0)\nscores_train[1]","metadata":{"papermill":{"duration":4.990577,"end_time":"2021-04-13T02:19:33.646418","exception":false,"start_time":"2021-04-13T02:19:28.655841","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:55.781520Z","iopub.execute_input":"2021-06-22T14:09:55.781858Z","iopub.status.idle":"2021-06-22T14:09:58.607061Z","shell.execute_reply.started":"2021-06-22T14:09:55.781823Z","shell.execute_reply":"2021-06-22T14:09:58.606275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores_test = conv_model.evaluate(X_test, y_test, verbose=0)\nscores_test[1]","metadata":{"papermill":{"duration":1.173985,"end_time":"2021-04-13T02:19:34.854372","exception":false,"start_time":"2021-04-13T02:19:33.680387","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:58.608360Z","iopub.execute_input":"2021-06-22T14:09:58.608729Z","iopub.status.idle":"2021-06-22T14:09:59.331198Z","shell.execute_reply.started":"2021-06-22T14:09:58.608692Z","shell.execute_reply":"2021-06-22T14:09:59.330380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = conv_model.predict(test)","metadata":{"papermill":{"duration":3.871089,"end_time":"2021-04-13T02:19:38.761057","exception":false,"start_time":"2021-04-13T02:19:34.889968","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-06-22T14:09:59.332455Z","iopub.execute_input":"2021-06-22T14:09:59.332795Z","iopub.status.idle":"2021-06-22T14:09:59.573560Z","shell.execute_reply.started":"2021-06-22T14:09:59.332760Z","shell.execute_reply":"2021-06-22T14:09:59.572669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred2=[]\nfor item in y_pred:\n    value2=np.argmax(item)      \n    y_pred2+=[value2]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T14:09:59.577232Z","iopub.execute_input":"2021-06-22T14:09:59.577514Z","iopub.status.idle":"2021-06-22T14:09:59.605088Z","shell.execute_reply.started":"2021-06-22T14:09:59.577489Z","shell.execute_reply":"2021-06-22T14:09:59.604368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample=pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsample","metadata":{"execution":{"iopub.status.busy":"2021-06-22T14:09:59.606240Z","iopub.execute_input":"2021-06-22T14:09:59.606542Z","iopub.status.idle":"2021-06-22T14:09:59.622022Z","shell.execute_reply.started":"2021-06-22T14:09:59.606517Z","shell.execute_reply":"2021-06-22T14:09:59.620991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit=sample\nsubmit['label']=y_pred2\nsubmit.to_csv('submission.csv',index=False)\nsubmit","metadata":{"execution":{"iopub.status.busy":"2021-06-22T14:09:59.623479Z","iopub.execute_input":"2021-06-22T14:09:59.624007Z","iopub.status.idle":"2021-06-22T14:09:59.684821Z","shell.execute_reply.started":"2021-06-22T14:09:59.623966Z","shell.execute_reply":"2021-06-22T14:09:59.683916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}