{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.datasets import fetch_openml\nimport tensorflow as tf\ntf.compat.v1.enable_eager_execution()\nimport keras\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy.io import loadmat\nimport os\nimport urllib","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true,"execution":{"iopub.status.busy":"2024-12-03T10:55:45.758293Z","iopub.execute_input":"2024-12-03T10:55:45.758623Z","iopub.status.idle":"2024-12-03T10:55:48.987253Z","shell.execute_reply.started":"2024-12-03T10:55:45.758590Z","shell.execute_reply":"2024-12-03T10:55:48.986539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Load labels for trainig datafrom six.moves import urllib\nmnist_path = \"mnist-original.mat\"\n\n# download label test dataset from github.\nmnist_alternative_url = \"https://github.com/amplab/datascience-sp14/raw/master/lab7/mldata/mnist-original.mat\"\nresponse = urllib.request.urlopen(mnist_alternative_url)\nwith open(mnist_path, \"wb\") as f:\n    content = response.read()\n    f.write(content)\n\nmnist_raw = loadmat(mnist_path)\n\nX = mnist_raw[\"data\"].T\ny = mnist_raw[\"label\"][0].astype(np.int64)\n\nX_test = test_data.values\n\nX.shape, X_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T10:55:48.988478Z","iopub.execute_input":"2024-12-03T10:55:48.988960Z","iopub.status.idle":"2024-12-03T10:55:50.824787Z","shell.execute_reply.started":"2024-12-03T10:55:48.988931Z","shell.execute_reply":"2024-12-03T10:55:50.823966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Função para calcular distâncias pareadas\ndef pairwise_distances(x, y):\n    x_norm = tf.reduce_sum(tf.square(x), axis=1, keepdims=True)\n    y_norm = tf.reduce_sum(tf.square(y), axis=1, keepdims=True)\n    dists = x_norm - 2 * tf.matmul(x, y, transpose_b=True) + tf.transpose(y_norm)\n    return tf.sqrt(tf.maximum(dists, 0.0))  # Garantir que as distâncias não sejam negativas","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T10:55:50.825964Z","iopub.execute_input":"2024-12-03T10:55:50.826225Z","iopub.status.idle":"2024-12-03T10:55:50.831681Z","shell.execute_reply.started":"2024-12-03T10:55:50.826200Z","shell.execute_reply":"2024-12-03T10:55:50.830608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\n\n# Convertendo os dados para tensores TensorFlow\ntf_test = tf.convert_to_tensor(X_test, dtype=tf.float32)\ntf_train = tf.convert_to_tensor(X, dtype=tf.float32)\n\n# Criando o dataset com o tf.data\nds = tf.data.Dataset.from_tensor_slices(tf_test).batch(1000)  # Definindo batch_size\n\n_min_dists = []\n_arg_min_dists = []\n\n# Criando o iterador do dataset\niterator = iter(ds)\n\nwhile True:\n    try:\n        batch = next(iterator)  # Obtém o próximo batch\n        dists = pairwise_distances(tf_train, batch)\n        min_dist = tf.reduce_min(dists, axis=0)\n        arg_min_dist = tf.argmin(dists, axis=0)\n        _min_dists.append(min_dist)\n        _arg_min_dists.append(arg_min_dist)\n    except StopIteration:\n        break  # Sai do loop quando os dados acabam","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T10:55:50.833538Z","iopub.execute_input":"2024-12-03T10:55:50.833778Z","iopub.status.idle":"2024-12-03T10:55:54.223920Z","shell.execute_reply.started":"2024-12-03T10:55:50.833754Z","shell.execute_reply":"2024-12-03T10:55:54.222963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Concatenando os resultados\n_min_dists = tf.concat(_min_dists, axis=0)\n_arg_min_dists = tf.concat(_arg_min_dists, axis=0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T10:55:54.224958Z","iopub.execute_input":"2024-12-03T10:55:54.225222Z","iopub.status.idle":"2024-12-03T10:55:54.283933Z","shell.execute_reply.started":"2024-12-03T10:55:54.225198Z","shell.execute_reply":"2024-12-03T10:55:54.283174Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsub.Label = y[_arg_min_dists.cpu()]\nsub.to_csv('submission.csv', index=False)\n!head submission.csv","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T10:55:54.285538Z","iopub.execute_input":"2024-12-03T10:55:54.286538Z","iopub.status.idle":"2024-12-03T10:55:55.354660Z","shell.execute_reply.started":"2024-12-03T10:55:54.286467Z","shell.execute_reply":"2024-12-03T10:55:55.353794Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axs = plt.subplots(15, 15, figsize=(25,25))\nfor i, ax in enumerate(axs.flatten()):\n    ax.set_title(sub.Label[i])\n    ax.imshow(X_test[i].reshape(28,28))\n    ax.axis('off')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-03T10:55:55.355951Z","iopub.execute_input":"2024-12-03T10:55:55.356244Z","iopub.status.idle":"2024-12-03T10:56:06.521990Z","shell.execute_reply.started":"2024-12-03T10:55:55.356208Z","shell.execute_reply":"2024-12-03T10:56:06.521086Z"}},"outputs":[],"execution_count":null}]}