{"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-01-16T14:38:15.005423Z","iopub.execute_input":"2023-01-16T14:38:15.006422Z","iopub.status.idle":"2023-01-16T14:38:15.066396Z","shell.execute_reply.started":"2023-01-16T14:38:15.006281Z","shell.execute_reply":"2023-01-16T14:38:15.065314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:42:38.674267Z","iopub.execute_input":"2023-01-16T14:42:38.674651Z","iopub.status.idle":"2023-01-16T14:42:38.679604Z","shell.execute_reply.started":"2023-01-16T14:42:38.674622Z","shell.execute_reply":"2023-01-16T14:42:38.678446Z"},"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/Dig-MNIST.csv')\nreal_test = pd.read_csv('../input/Kannada-MNIST/test.csv')\nsample_submission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:51:26.106310Z","iopub.execute_input":"2023-01-16T14:51:26.107423Z","iopub.status.idle":"2023-01-16T14:51:29.305025Z","shell.execute_reply.started":"2023-01-16T14:51:26.107378Z","shell.execute_reply":"2023-01-16T14:51:29.304044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.models import Model, Sequential\nfrom keras.layers import Conv2D, MaxPool2D, Input, Dense,ReLU, Flatten, Dropout, BatchNormalization\nfrom keras.utils import np_utils","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:19:21.502145Z","iopub.execute_input":"2023-01-16T15:19:21.503156Z","iopub.status.idle":"2023-01-16T15:19:21.509546Z","shell.execute_reply.started":"2023-01-16T15:19:21.503098Z","shell.execute_reply":"2023-01-16T15:19:21.508388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:47:22.708708Z","iopub.execute_input":"2023-01-16T14:47:22.709069Z","iopub.status.idle":"2023-01-16T14:47:22.732115Z","shell.execute_reply.started":"2023-01-16T14:47:22.709038Z","shell.execute_reply":"2023-01-16T14:47:22.731096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_test.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:47:41.465024Z","iopub.execute_input":"2023-01-16T14:47:41.466046Z","iopub.status.idle":"2023-01-16T14:47:41.483693Z","shell.execute_reply.started":"2023-01-16T14:47:41.466000Z","shell.execute_reply":"2023-01-16T14:47:41.482582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"treino\", train.shape)\nprint(\"teste real\", real_test)\nprint(\"submission\", sample_submission.shape)\nprint(\"teste dig_mnist\", test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:52:04.655655Z","iopub.execute_input":"2023-01-16T14:52:04.656023Z","iopub.status.idle":"2023-01-16T14:52:04.671171Z","shell.execute_reply.started":"2023-01-16T14:52:04.655991Z","shell.execute_reply":"2023-01-16T14:52:04.669825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train.iloc[:,1:].to_numpy()\ny_train = train.iloc[:,0].to_numpy()\n\nx_test = test.iloc[:,1:].to_numpy()\ny_test = test.iloc[:,0].to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:56:10.529697Z","iopub.execute_input":"2023-01-16T14:56:10.530072Z","iopub.status.idle":"2023-01-16T14:56:10.536485Z","shell.execute_reply.started":"2023-01-16T14:56:10.530040Z","shell.execute_reply":"2023-01-16T14:56:10.535546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:56:32.530724Z","iopub.execute_input":"2023-01-16T14:56:32.531080Z","iopub.status.idle":"2023-01-16T14:56:32.537759Z","shell.execute_reply.started":"2023-01-16T14:56:32.531049Z","shell.execute_reply":"2023-01-16T14:56:32.536750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:56:44.248895Z","iopub.execute_input":"2023-01-16T14:56:44.249291Z","iopub.status.idle":"2023-01-16T14:56:44.255618Z","shell.execute_reply.started":"2023-01-16T14:56:44.249258Z","shell.execute_reply":"2023-01-16T14:56:44.254686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:56:55.942831Z","iopub.execute_input":"2023-01-16T14:56:55.943204Z","iopub.status.idle":"2023-01-16T14:56:55.950269Z","shell.execute_reply.started":"2023-01-16T14:56:55.943172Z","shell.execute_reply":"2023-01-16T14:56:55.948919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,10))\nfor i in range(25):\n    plt.subplot(5,5, i+1)\n    plt.imshow(x_train[i].reshape(28,28), cmap='gray')\n    plt.title(y_train[i])","metadata":{"execution":{"iopub.status.busy":"2023-01-16T14:59:28.750429Z","iopub.execute_input":"2023-01-16T14:59:28.750788Z","iopub.status.idle":"2023-01-16T14:59:30.807840Z","shell.execute_reply.started":"2023-01-16T14:59:28.750758Z","shell.execute_reply":"2023-01-16T14:59:30.806946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train/255\nx_train = x_train.astype(\"float32\")\nx_test = x_test/255\nx_test = x_test.astype(\"float32\")","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:04:08.863744Z","iopub.execute_input":"2023-01-16T15:04:08.864140Z","iopub.status.idle":"2023-01-16T15:04:09.026109Z","shell.execute_reply.started":"2023-01-16T15:04:08.864091Z","shell.execute_reply":"2023-01-16T15:04:09.025026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:04:20.858056Z","iopub.execute_input":"2023-01-16T15:04:20.858644Z","iopub.status.idle":"2023-01-16T15:04:20.866268Z","shell.execute_reply.started":"2023-01-16T15:04:20.858585Z","shell.execute_reply":"2023-01-16T15:04:20.864784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:06:17.064902Z","iopub.execute_input":"2023-01-16T15:06:17.065282Z","iopub.status.idle":"2023-01-16T15:06:17.648860Z","shell.execute_reply.started":"2023-01-16T15:06:17.065249Z","shell.execute_reply":"2023-01-16T15:06:17.647381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(y_train.shape)\nprint(x_test.shape)\nprint(y_test.shape)\nprint(\"valor minimo: \",y_train.min())\nprint(\"valor maximo: \",y_train.max())","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:08:32.819503Z","iopub.execute_input":"2023-01-16T15:08:32.819856Z","iopub.status.idle":"2023-01-16T15:08:32.826661Z","shell.execute_reply.started":"2023-01-16T15:08:32.819827Z","shell.execute_reply":"2023-01-16T15:08:32.825581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)\nx_test = x_test.reshape(x_test.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:10:09.576420Z","iopub.execute_input":"2023-01-16T15:10:09.576795Z","iopub.status.idle":"2023-01-16T15:10:09.582632Z","shell.execute_reply.started":"2023-01-16T15:10:09.576763Z","shell.execute_reply":"2023-01-16T15:10:09.581502Z"},"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":{"execution":{"iopub.status.busy":"2023-01-16T15:11:06.329262Z","iopub.execute_input":"2023-01-16T15:11:06.329628Z","iopub.status.idle":"2023-01-16T15:11:06.335556Z","shell.execute_reply.started":"2023-01-16T15:11:06.329595Z","shell.execute_reply":"2023-01-16T15:11:06.334411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo = keras.Sequential(\n    [\n        Conv2D(32,(3,3), activation = 'relu'),\n        Conv2D(64,(3,3), activation = 'relu'),\n        MaxPool2D((2,2)),\n        Flatten(),\n        Dense(128, activation = 'relu'),\n        Dense(64, activation = 'relu'),\n        Dropout(0.2),\n        Dense(10, activation = 'softmax')\n    ]\n)\n\nmodelo.compile(optimizer='adam',\n               loss='sparse_categorical_crossentropy',\n               metrics = 'accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:19:42.728654Z","iopub.execute_input":"2023-01-16T15:19:42.729034Z","iopub.status.idle":"2023-01-16T15:19:48.204748Z","shell.execute_reply.started":"2023-01-16T15:19:42.729000Z","shell.execute_reply":"2023-01-16T15:19:48.202572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = modelo.fit(x_train, y_train, epochs = 50, validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:32:21.524705Z","iopub.execute_input":"2023-01-16T15:32:21.525115Z","iopub.status.idle":"2023-01-16T15:36:43.770647Z","shell.execute_reply.started":"2023-01-16T15:32:21.525080Z","shell.execute_reply":"2023-01-16T15:36:43.769439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label = 'loss')\nplt.plot(history.history['val_loss'], label = 'val_loss')\nplt.xlabel('Epochs')\nplt.ylabel('loss')\nplt.legend(loc = \"lower right\");","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:23:39.277290Z","iopub.execute_input":"2023-01-16T15:23:39.277708Z","iopub.status.idle":"2023-01-16T15:23:39.525021Z","shell.execute_reply.started":"2023-01-16T15:23:39.277673Z","shell.execute_reply":"2023-01-16T15:23:39.523857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'], label = 'accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend(loc = \"lower right\");","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:24:34.783581Z","iopub.execute_input":"2023-01-16T15:24:34.784014Z","iopub.status.idle":"2023-01-16T15:24:35.053655Z","shell.execute_reply.started":"2023-01-16T15:24:34.783977Z","shell.execute_reply":"2023-01-16T15:24:35.052190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"modelo.evaluate(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:25:03.221429Z","iopub.execute_input":"2023-01-16T15:25:03.221843Z","iopub.status.idle":"2023-01-16T15:25:04.572908Z","shell.execute_reply.started":"2023-01-16T15:25:03.221807Z","shell.execute_reply":"2023-01-16T15:25:04.571666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"real_test = real_test.iloc[:,1:].to_numpy()\nreal_test = real_test/255\nreal_test = real_test.astype(\"float32\")\nreal_test = real_test.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:26:43.263650Z","iopub.execute_input":"2023-01-16T15:26:43.264740Z","iopub.status.idle":"2023-01-16T15:26:43.304198Z","shell.execute_reply.started":"2023-01-16T15:26:43.264677Z","shell.execute_reply":"2023-01-16T15:26:43.303020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = modelo.predict(real_test)\npredictions = predictions.argmax(axis = 1)\npredictions[:3]","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:27:39.656432Z","iopub.execute_input":"2023-01-16T15:27:39.657089Z","iopub.status.idle":"2023-01-16T15:27:40.472477Z","shell.execute_reply.started":"2023-01-16T15:27:39.657048Z","shell.execute_reply":"2023-01-16T15:27:40.471218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['label'] = predictions\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:28:47.032112Z","iopub.execute_input":"2023-01-16T15:28:47.032561Z","iopub.status.idle":"2023-01-16T15:28:47.043161Z","shell.execute_reply.started":"2023-01-16T15:28:47.032525Z","shell.execute_reply":"2023-01-16T15:28:47.042023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T15:29:25.213013Z","iopub.execute_input":"2023-01-16T15:29:25.214091Z","iopub.status.idle":"2023-01-16T15:29:25.232888Z","shell.execute_reply.started":"2023-01-16T15:29:25.214049Z","shell.execute_reply":"2023-01-16T15:29:25.231780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}