{"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":"markdown","source":"# Um clássico do reconhecimento de dígitos","metadata":{}},{"cell_type":"markdown","source":"Antes de tudo vamos conhecer nossos dados.\n\nEsse é um exemplo clássico de Aprendizado de Máquina, onde temos que fazer a máquina nos dizer qual número foi escrito.","metadata":{"_uuid":"b0b38fd5-9d41-48bf-9363-4c5346953fcb","_cell_guid":"dcb32964-9ad2-4439-a300-fe5b1db00618","trusted":true}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimagens_disponiveis = pd.read_csv(\"../input/digit-recognizer/train.csv\")\nimagens_disponiveis.info()","metadata":{"_uuid":"6c5cf1b4-bd75-4b5a-bcb4-369b06a03f61","_cell_guid":"0de3e1c0-e9b1-4c47-a1eb-e2c9ea841bcf","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-05T18:51:27.713487Z","iopub.execute_input":"2023-06-05T18:51:27.713981Z","iopub.status.idle":"2023-06-05T18:51:32.419808Z","shell.execute_reply.started":"2023-06-05T18:51:27.713887Z","shell.execute_reply":"2023-06-05T18:51:32.418500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagens_disponiveis.head()","metadata":{"_uuid":"f295edc9-121f-4a67-a56d-116b35cc349b","_cell_guid":"315c447b-3240-4e33-a53f-1b64fe4f8457","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-05T18:51:32.422148Z","iopub.execute_input":"2023-06-05T18:51:32.424532Z","iopub.status.idle":"2023-06-05T18:51:32.456731Z","shell.execute_reply.started":"2023-06-05T18:51:32.424480Z","shell.execute_reply":"2023-06-05T18:51:32.455200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Nossos dados de entrada foram organizados assim: cada linha é um exemplo. A primeira coluna informa o número escrito na imagem, e as colunas restantes são os pixels da nossa imagem. As imagens são em preto e branco, quadradas, de tamanho 28 pixels por 28 pixels. Elas foram decompostas, no sentido em que escrevemos, em uma lista de 784 inteiros que representam o quão escuro o pixel é. Variando de 0 até 255","metadata":{"_uuid":"5b65fee8-a893-4085-8e32-0e9382a699cc","_cell_guid":"cef0bbdf-39fc-4138-979c-61b8840aaddb","trusted":true}},{"cell_type":"code","source":"#separando a classificação das imagens\n\ndigitos_disponiveis = imagens_disponiveis.pop('label')\ndigitos_disponiveis.head()","metadata":{"_uuid":"1666ab73-0ec9-483c-a04d-a61a6bc00ee4","_cell_guid":"2590d027-1fe7-448a-adab-6599f20c2355","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-05T18:51:32.458813Z","iopub.execute_input":"2023-06-05T18:51:32.459440Z","iopub.status.idle":"2023-06-05T18:51:32.476493Z","shell.execute_reply.started":"2023-06-05T18:51:32.459388Z","shell.execute_reply":"2023-06-05T18:51:32.475091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"digitos_tipos = digitos_disponiveis.unique()\nprint(np.sort(digitos_tipos))","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:32.479841Z","iopub.execute_input":"2023-06-05T18:51:32.481456Z","iopub.status.idle":"2023-06-05T18:51:32.489456Z","shell.execute_reply.started":"2023-06-05T18:51:32.481404Z","shell.execute_reply":"2023-06-05T18:51:32.488063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagens_disponiveis = imagens_disponiveis.astype('float32')\nimagens_disponiveis.info()","metadata":{"_uuid":"d639d95d-31b8-465f-96c7-89e5e09c2799","_cell_guid":"4fed6641-1e45-4d7c-9b68-c4b837d09d69","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-05T18:51:32.491759Z","iopub.execute_input":"2023-06-05T18:51:32.492757Z","iopub.status.idle":"2023-06-05T18:51:32.689641Z","shell.execute_reply.started":"2023-06-05T18:51:32.492693Z","shell.execute_reply":"2023-06-05T18:51:32.688200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imagens_disponiveis = imagens_disponiveis / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:32.691412Z","iopub.execute_input":"2023-06-05T18:51:32.691863Z","iopub.status.idle":"2023-06-05T18:51:32.778589Z","shell.execute_reply.started":"2023-06-05T18:51:32.691814Z","shell.execute_reply":"2023-06-05T18:51:32.777290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#visualizando as informações\n\nimg_numpy =  imagens_disponiveis[1:2].to_numpy()\nimagem = img_numpy.reshape(img_numpy.shape[0], 28, 28)\n\nplt.figure()\nplt.imshow(imagem[0]) # ,cmap=plt.get_cmap('gray')\nplt.show()","metadata":{"_uuid":"ae651181-904f-4011-a614-c96bc1bdb458","_cell_guid":"165354ae-52dc-4384-9486-e52348d05f7b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-06-05T18:51:32.780178Z","iopub.execute_input":"2023-06-05T18:51:32.780609Z","iopub.status.idle":"2023-06-05T18:51:33.164351Z","shell.execute_reply.started":"2023-06-05T18:51:32.780569Z","shell.execute_reply":"2023-06-05T18:51:33.163139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nimagens_treino,imagens_teste,digitos_treino,digitos_teste = train_test_split(\n    imagens_disponiveis,\n    digitos_disponiveis,\n    test_size = 0.2, \n    random_state = 42\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:33.165734Z","iopub.execute_input":"2023-06-05T18:51:33.166642Z","iopub.status.idle":"2023-06-05T18:51:33.970316Z","shell.execute_reply.started":"2023-06-05T18:51:33.166604Z","shell.execute_reply":"2023-06-05T18:51:33.969072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(digitos_treino[0])","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:33.972093Z","iopub.execute_input":"2023-06-05T18:51:33.972499Z","iopub.status.idle":"2023-06-05T18:51:33.979491Z","shell.execute_reply.started":"2023-06-05T18:51:33.972465Z","shell.execute_reply":"2023-06-05T18:51:33.978444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import np_utils\n\ndigitos_treino_categorical = np_utils.to_categorical(digitos_treino, 10)\ndigitos_teste_categorical = np_utils.to_categorical(digitos_teste, 10)\n\ntype(digitos_treino_categorical[0][0])","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:33.983092Z","iopub.execute_input":"2023-06-05T18:51:33.984191Z","iopub.status.idle":"2023-06-05T18:51:40.696753Z","shell.execute_reply.started":"2023-06-05T18:51:33.984137Z","shell.execute_reply":"2023-06-05T18:51:40.695219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# a rede","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(5, 2))\nax.bar(['1º camada', '2º camada', '3º camada', '4º camada'], [784, 392, 98, 10])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:40.698508Z","iopub.execute_input":"2023-06-05T18:51:40.699282Z","iopub.status.idle":"2023-06-05T18:51:40.878926Z","shell.execute_reply.started":"2023-06-05T18:51:40.699242Z","shell.execute_reply":"2023-06-05T18:51:40.877545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# criando a rede\n\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.models import Sequential\n\nmodel = Sequential()\nmodel.add(Dense(392, activation = 'relu', input_shape =(784,)))\nmodel.add(Dense(98, activation = 'relu'))\nmodel.add(Dense(10, activation ='softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:40.880523Z","iopub.execute_input":"2023-06-05T18:51:40.880986Z","iopub.status.idle":"2023-06-05T18:51:41.440369Z","shell.execute_reply.started":"2023-06-05T18:51:40.880950Z","shell.execute_reply":"2023-06-05T18:51:41.439021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss = 'categorical_crossentropy',\n    optimizer = 'Adam',\n    metrics = ['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:41.441817Z","iopub.execute_input":"2023-06-05T18:51:41.442238Z","iopub.status.idle":"2023-06-05T18:51:41.462467Z","shell.execute_reply.started":"2023-06-05T18:51:41.442201Z","shell.execute_reply":"2023-06-05T18:51:41.461156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"historico = model.fit(\n    imagens_treino,\n    digitos_treino_categorical,\n    batch_size = 100,\n    epochs=10,\n    validation_data=(imagens_teste, digitos_teste_categorical)\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:51:41.464688Z","iopub.execute_input":"2023-06-05T18:51:41.465091Z","iopub.status.idle":"2023-06-05T18:52:00.773301Z","shell.execute_reply.started":"2023-06-05T18:51:41.465056Z","shell.execute_reply":"2023-06-05T18:52:00.772151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"teste_erro, teste_acurarica = model.evaluate(imagens_teste, digitos_teste_categorical)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:52:00.775079Z","iopub.execute_input":"2023-06-05T18:52:00.775477Z","iopub.status.idle":"2023-06-05T18:52:01.662523Z","shell.execute_reply.started":"2023-06-05T18:52:00.775444Z","shell.execute_reply":"2023-06-05T18:52:01.661606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(teste_acurarica)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:52:01.664000Z","iopub.execute_input":"2023-06-05T18:52:01.664942Z","iopub.status.idle":"2023-06-05T18:52:01.670419Z","shell.execute_reply.started":"2023-06-05T18:52:01.664904Z","shell.execute_reply":"2023-06-05T18:52:01.669344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submetendo resultados","metadata":{}},{"cell_type":"code","source":"imagens_avaliacao = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nimagens_avaliacao = imagens_avaliacao.astype('float32')\nimagens_avaliacao = imagens_avaliacao / 255.0\n\ndigitos_resultantes = model.predict(imagens_avaliacao)\ndigitos_resultantes = np.argmax(digitos_resultantes, axis=1)\n\nresultados_df = pd.DataFrame(\n    {\n        'ImageId': range(1, len(digitos_resultantes)+1),\n        'Label': digitos_resultantes\n    }\n)\nresultados_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:52:01.671887Z","iopub.execute_input":"2023-06-05T18:52:01.672253Z","iopub.status.idle":"2023-06-05T18:52:06.028518Z","shell.execute_reply.started":"2023-06-05T18:52:01.672220Z","shell.execute_reply":"2023-06-05T18:52:06.027359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# entendendo os resultados","metadata":{}},{"cell_type":"code","source":"acc = historico.history['accuracy']\nval_acc = historico.history['val_accuracy']\nloss = historico.history['loss']\nval_loss = historico.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'bo', label='acurácia no treinamento')\nplt.plot(epochs, val_acc, 'b', label='acurácia na validação')\nplt.title('acurácia de treinamento e validação')\n\nplt.figure()\n\nplt.plot(epochs, loss, 'bo', label='perda no treinamento')\nplt.plot(epochs, val_loss, 'b', label='perda na validação')\nplt.title('perda de treinamento e validação')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-05T18:59:07.831705Z","iopub.execute_input":"2023-06-05T18:59:07.832150Z","iopub.status.idle":"2023-06-05T18:59:08.286241Z","shell.execute_reply.started":"2023-06-05T18:59:07.832099Z","shell.execute_reply":"2023-06-05T18:59:08.285226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Continua ...","metadata":{}}]}