{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install MiniSom\nimport numpy as np\nfrom minisom import MiniSom\nfrom sklearn.decomposition import PCA\nfrom sklearn.manifold import TSNE\nfrom tensorflow.keras.datasets import mnist\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-12-16T16:02:12.787755Z","iopub.execute_input":"2024-12-16T16:02:12.788659Z","iopub.status.idle":"2024-12-16T16:02:35.604113Z","shell.execute_reply.started":"2024-12-16T16:02:12.788612Z","shell.execute_reply":"2024-12-16T16:02:35.603365Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Початкові налаштування","metadata":{}},{"cell_type":"code","source":"(x_train, y_train), (_, _) = mnist.load_data()\nx_train = x_train / 255.0\nx_train = x_train.reshape(x_train.shape[0], -1)\nscaler = StandardScaler()\nx_train = scaler.fit_transform(x_train)\nprint(x_train.size)\nX_sample = x_train[:10000]\ny_sample = y_train[:10000]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T17:15:33.268737Z","iopub.execute_input":"2024-12-16T17:15:33.269049Z","iopub.status.idle":"2024-12-16T17:15:34.283594Z","shell.execute_reply.started":"2024-12-16T17:15:33.269021Z","shell.execute_reply":"2024-12-16T17:15:34.282711Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"som = MiniSom(x=10, y=10, input_len=784, sigma=0.8, learning_rate=0.5)\nsom.random_weights_init(x_train)\nsom.train_random(x_train, 4000)","metadata":{"execution":{"iopub.status.busy":"2024-12-16T17:17:24.725907Z","iopub.execute_input":"2024-12-16T17:17:24.726261Z","iopub.status.idle":"2024-12-16T17:17:26.524132Z","shell.execute_reply.started":"2024-12-16T17:17:24.726225Z","shell.execute_reply":"2024-12-16T17:17:26.523422Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"som_grid = np.zeros((10, 10))\nfor x, label in zip(x_train, y_train):\n    w = som.winner(x)\n    som_grid[w] = label\n\nplt.figure(figsize=(10, 10))\nplt.imshow(som_grid, cmap='tab10', interpolation='nearest')\nplt.colorbar(ticks=range(10), label=\"Класи MNIST\")\nplt.title(\"SOM Visualization\")\nplt.xlabel(\"X\")\nplt.ylabel(\"Y\")\nplt.savefig('my_image_squares.png')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T17:17:26.525604Z","iopub.execute_input":"2024-12-16T17:17:26.525874Z","iopub.status.idle":"2024-12-16T17:17:36.594765Z","shell.execute_reply.started":"2024-12-16T17:17:26.525847Z","shell.execute_reply":"2024-12-16T17:17:36.593845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(x_train, y_train), (_, _) = mnist.load_data()\nx_train = x_train / 255.0\nprint(x_train.size)\nx_train = x_train.reshape(x_train.shape[0], -1)\nscaler = StandardScaler()\nx_train = scaler.fit_transform(x_train)\nX_sample = x_train[:20000]\ny_sample = y_train[:20000]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T17:35:54.646349Z","iopub.execute_input":"2024-12-16T17:35:54.646703Z","iopub.status.idle":"2024-12-16T17:35:55.647656Z","shell.execute_reply.started":"2024-12-16T17:35:54.646673Z","shell.execute_reply":"2024-12-16T17:35:55.646909Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pca = PCA(n_components=2)\npca_result = pca.fit_transform(X_sample)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T17:35:55.649141Z","iopub.execute_input":"2024-12-16T17:35:55.649454Z","iopub.status.idle":"2024-12-16T17:35:56.421140Z","shell.execute_reply.started":"2024-12-16T17:35:55.649427Z","shell.execute_reply":"2024-12-16T17:35:56.419469Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.scatter(pca_result[:, 0], pca_result[:, 1], c=y_sample, cmap='tab10', s=10)\nplt.title('PCA Visualization')\nplt.colorbar()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T17:35:56.422590Z","iopub.execute_input":"2024-12-16T17:35:56.424220Z","iopub.status.idle":"2024-12-16T17:35:56.975856Z","shell.execute_reply.started":"2024-12-16T17:35:56.424155Z","shell.execute_reply":"2024-12-16T17:35:56.975011Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tsne = TSNE(n_components=2, perplexity=30, n_iter=300)\ntsne_result = tsne.fit_transform(X_sample)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-16T13:38:42.130160Z","iopub.execute_input":"2024-12-16T13:38:42.130949Z","iopub.status.idle":"2024-12-16T13:39:40.234770Z","shell.execute_reply.started":"2024-12-16T13:38:42.130912Z","shell.execute_reply":"2024-12-16T13:39:40.233988Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.scatter(tsne_result[:, 0], tsne_result[:, 1], c=y_sample, cmap='tab10', s=10)\nplt.title('t-SNE Visualization')\nplt.colorbar()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-15T13:12:44.057804Z","iopub.execute_input":"2024-12-15T13:12:44.058051Z","iopub.status.idle":"2024-12-15T13:12:44.948753Z","shell.execute_reply.started":"2024-12-15T13:12:44.058025Z","shell.execute_reply":"2024-12-15T13:12:44.947858Z"}},"outputs":[],"execution_count":null}]}