{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":77476,"databundleVersionId":8462666,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-05-09T10:27:21.604074Z","iopub.execute_input":"2024-05-09T10:27:21.604662Z","iopub.status.idle":"2024-05-09T10:27:22.208358Z","shell.execute_reply.started":"2024-05-09T10:27:21.604614Z","shell.execute_reply":"2024-05-09T10:27:22.207222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1. 기본 세팅 및 train data의 label 분리","metadata":{}},{"cell_type":"markdown","source":"**주어진 예제에 대한 설명을 분석해보자.**","metadata":{}},{"cell_type":"markdown","source":"Zalando's Fashion MNIST: https://github.com/zalandoresearch/fashion-mnist 를 사용하고 있습니다\n\nFashion-MNIST는 60,000개의 예제로 구성된 훈련 세트와 10,000개의 예제로 구성된 Zalando의 기사 이미지 데이터 세트입니다. 각 예제는 10개 클래스의 레이블과 연결된 28x28 그레이스케일 이미지입니다. Zalando는 Fashion-MNIST가 머신 러닝 알고리즘을 벤치마킹하기 위한 원래 MNIST 데이터 세트를 직접적으로 대체하는 역할을 할 계획입니다. 훈련 및 테스트 분할의 동일한 이미지 크기와 구조를 공유합니다.\n\n원래 MNIST 데이터 세트에는 손으로 쓴 숫자가 많이 포함되어 있습니다. AI/ML/Data Science 커뮤니티의 구성원들은 이 데이터 세트를 좋아하고 알고리즘을 검증하는 벤치마크로 사용합니다. 사실 MNIST는 종종 연구자들이 시도하는 첫 번째 데이터 세트입니다. \"MNIST에서 작동하지 않으면 전혀 작동하지 않을 것입니다.\"라고 그들은 말했습니다. \"음, MNIST에서 작동하면 여전히 다른 사람들에게 작동하지 않을 수 있습니다.\"\n\n잘란도, **기존 MNIST 데이터셋 교체 추진**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\ntrain_data = pd.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/test.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:22.211155Z","iopub.execute_input":"2024-05-09T10:27:22.211792Z","iopub.status.idle":"2024-05-09T10:27:32.178790Z","shell.execute_reply.started":"2024-05-09T10:27:22.211751Z","shell.execute_reply":"2024-05-09T10:27:32.177490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.180610Z","iopub.execute_input":"2024-05-09T10:27:32.181101Z","iopub.status.idle":"2024-05-09T10:27:32.220876Z","shell.execute_reply.started":"2024-05-09T10:27:32.181059Z","shell.execute_reply":"2024-05-09T10:27:32.219173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.222592Z","iopub.execute_input":"2024-05-09T10:27:32.223086Z","iopub.status.idle":"2024-05-09T10:27:32.249412Z","shell.execute_reply.started":"2024-05-09T10:27:32.223044Z","shell.execute_reply":"2024-05-09T10:27:32.248189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"784 columns가 아닌 785 columns가 나오는 이유는 label 때문이다. 분리시켜보자.","metadata":{}},{"cell_type":"code","source":"train_labels = train_data[\"label\"]\nx_train = train_data.drop(labels =[\"label\"], axis=1)\n\nprint(x_train.shape)\nprint(test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.254366Z","iopub.execute_input":"2024-05-09T10:27:32.255030Z","iopub.status.idle":"2024-05-09T10:27:32.420164Z","shell.execute_reply.started":"2024-05-09T10:27:32.254994Z","shell.execute_reply":"2024-05-09T10:27:32.418985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train image에는 6만개, test image에는 1만개 저장되어 있다는 사실을 알 수 있다.","metadata":{}},{"cell_type":"markdown","source":"이제 결측값이 있는지 확인해보자.","metadata":{}},{"cell_type":"code","source":"missing_values = x_train.isnull().sum()\nprint(missing_values[missing_values > 0])","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.421466Z","iopub.execute_input":"2024-05-09T10:27:32.421798Z","iopub.status.idle":"2024-05-09T10:27:32.471488Z","shell.execute_reply.started":"2024-05-09T10:27:32.421769Z","shell.execute_reply":"2024-05-09T10:27:32.470159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"없다.","metadata":{}},{"cell_type":"markdown","source":"바뀐 값 좀 확인해보자.","metadata":{}},{"cell_type":"code","source":"x_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.472983Z","iopub.execute_input":"2024-05-09T10:27:32.473327Z","iopub.status.idle":"2024-05-09T10:27:32.497372Z","shell.execute_reply.started":"2024-05-09T10:27:32.473298Z","shell.execute_reply":"2024-05-09T10:27:32.496014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.499314Z","iopub.execute_input":"2024-05-09T10:27:32.499778Z","iopub.status.idle":"2024-05-09T10:27:32.511821Z","shell.execute_reply.started":"2024-05-09T10:27:32.499737Z","shell.execute_reply":"2024-05-09T10:27:32.510360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"잘 되어 있는 것을 확인할 수 있다.","metadata":{}},{"cell_type":"markdown","source":"# 2. 3차원 데이터로 변환","metadata":{}},{"cell_type":"markdown","source":"픽셀 데이터를 0~1사이의 데이터로 정규화한다.","metadata":{}},{"cell_type":"code","source":"x_train = x_train / 255.0\ntrain_labels = train_labels / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.513372Z","iopub.execute_input":"2024-05-09T10:27:32.513816Z","iopub.status.idle":"2024-05-09T10:27:32.648024Z","shell.execute_reply.started":"2024-05-09T10:27:32.513775Z","shell.execute_reply":"2024-05-09T10:27:32.646196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"어떤 타입인지 확인해보자.","metadata":{}},{"cell_type":"code","source":"print(type(x_train))\nprint(type(train_labels))\nprint(type(test_data))","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.650572Z","iopub.execute_input":"2024-05-09T10:27:32.651093Z","iopub.status.idle":"2024-05-09T10:27:32.658862Z","shell.execute_reply.started":"2024-05-09T10:27:32.651007Z","shell.execute_reply":"2024-05-09T10:27:32.657443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"pandas의 데이터를 numpy의 데이터로 바꾸어서 관리하기 편하게 해보자.","metadata":{}},{"cell_type":"code","source":"x_train = x_train.to_numpy()\ntrain_labels = train_labels.to_numpy()\ntest_data = test_data.to_numpy()\nprint(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.661139Z","iopub.execute_input":"2024-05-09T10:27:32.661595Z","iopub.status.idle":"2024-05-09T10:27:32.683130Z","shell.execute_reply.started":"2024-05-09T10:27:32.661554Z","shell.execute_reply":"2024-05-09T10:27:32.681476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"28x28의 데이터로 바꾸어보자.","metadata":{}},{"cell_type":"code","source":"x_train = x_train.reshape(-1, 28, 28)\ntest_data = test_data.reshape(-1, 28, 28)\nprint(x_train.shape)\nprint(test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.684810Z","iopub.execute_input":"2024-05-09T10:27:32.686575Z","iopub.status.idle":"2024-05-09T10:27:32.696701Z","shell.execute_reply.started":"2024-05-09T10:27:32.686499Z","shell.execute_reply":"2024-05-09T10:27:32.695227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. 라벨 값과 데이터 맞춰보고 EDA","metadata":{}},{"cell_type":"markdown","source":"There are 10 categories.\n\n0 T-shirt/top\n\n1 Trouser\n\n2 Pullover\n\n3 Dress\n\n4 Coat\n\n5 Sandal\n\n6 Shirt\n\n7 Sneaker\n\n8 Bag\n\n9 Ankle boot\n\n캐글에서 주어진 각 라벨에 대한 정보이다. 이것을 활용해보자.","metadata":{}},{"cell_type":"code","source":"label_names = [\n    'T-shirt/top',\n    'Trouser',\n    'Pullover',\n    'Dress',\n    'coat',\n    'Sandal',\n    'Shirt',\n    'Sneaker',\n    'Bag',\n    'Ankle boot'\n]","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.698546Z","iopub.execute_input":"2024-05-09T10:27:32.700778Z","iopub.status.idle":"2024-05-09T10:27:32.710665Z","shell.execute_reply.started":"2024-05-09T10:27:32.700730Z","shell.execute_reply":"2024-05-09T10:27:32.708141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"잘 모르겠는데 train label은 pandas를 써야될 것 같다..","metadata":{}},{"cell_type":"code","source":"train_labels = pd.Series(train_labels)\nprint(type(train_labels))","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.716644Z","iopub.execute_input":"2024-05-09T10:27:32.717055Z","iopub.status.idle":"2024-05-09T10:27:32.727848Z","shell.execute_reply.started":"2024-05-09T10:27:32.717022Z","shell.execute_reply":"2024-05-09T10:27:32.726436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"라벨에 대한 정보를 알아보자.","metadata":{}},{"cell_type":"code","source":"train_labels.info()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.729563Z","iopub.execute_input":"2024-05-09T10:27:32.729983Z","iopub.status.idle":"2024-05-09T10:27:32.754974Z","shell.execute_reply.started":"2024-05-09T10:27:32.729914Z","shell.execute_reply":"2024-05-09T10:27:32.753650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"이제 28 x 28로 reshape한 데이터를 그림으로 표현해보자. 이 때 라벨과 데이터가 일치해야한다.","metadata":{}},{"cell_type":"code","source":"plt.figure()\nplt.imshow(x_train[500])\nplt.colorbar()\nplt.grid(False)\nplt.show()\nprint(label_names[int(train_labels[500])])","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:32.756610Z","iopub.execute_input":"2024-05-09T10:27:32.757117Z","iopub.status.idle":"2024-05-09T10:27:33.323337Z","shell.execute_reply.started":"2024-05-09T10:27:32.757077Z","shell.execute_reply":"2024-05-09T10:27:33.322009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"다른 그림들도 라벨과 데이터가 일치하는지 알아보도록 하자.","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(x_train[i+10000], cmap = plt.cm.binary)\n    plt.xlabel(label_names[int(train_labels[i])])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:27:33.325120Z","iopub.execute_input":"2024-05-09T10:27:33.325664Z","iopub.status.idle":"2024-05-09T10:27:34.758942Z","shell.execute_reply.started":"2024-05-09T10:27:33.325621Z","shell.execute_reply":"2024-05-09T10:27:34.757693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"이미지와 라벨이 각 인덱스에 맞게 되어 있다는 사실을 알 수 있다.","metadata":{}},{"cell_type":"code","source":"x_train = x_train.astype('float64') \ntest_data = test_data.astype('float64') \nprint(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:29:14.568503Z","iopub.execute_input":"2024-05-09T10:29:14.568897Z","iopub.status.idle":"2024-05-09T10:29:14.743154Z","shell.execute_reply.started":"2024-05-09T10:29:14.568867Z","shell.execute_reply":"2024-05-09T10:29:14.741209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3. PCA","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nx_train = x_train.reshape(x_train.shape[0], -1)\nprint(x_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:29:29.679329Z","iopub.execute_input":"2024-05-09T10:29:29.679775Z","iopub.status.idle":"2024-05-09T10:29:29.688008Z","shell.execute_reply.started":"2024-05-09T10:29:29.679743Z","shell.execute_reply":"2024-05-09T10:29:29.686173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA를 사용하여 차원 축소 (예: 50차원으로 축소)\npca = PCA(n_components=.9)\nX_pca = pca.fit_transform(x_train)\nprint(X_pca.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:30:27.699055Z","iopub.execute_input":"2024-05-09T10:30:27.699473Z","iopub.status.idle":"2024-05-09T10:30:37.191903Z","shell.execute_reply.started":"2024-05-09T10:30:27.699430Z","shell.execute_reply":"2024-05-09T10:30:37.190879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA로 축소한 데이터 시각화\nplt.figure(figsize=(10, 10))\nscatter = plt.scatter(X_pca[:, 0], X_pca[:, 1], c=train_labels.astype(int), cmap='viridis', alpha=0.5)\nplt.colorbar(scatter, ticks=range(10))\nplt.xlabel('Principal Component 1')\nplt.ylabel('Principal Component 2')\nplt.title('MNIST Dataset with PCA')\nplt.grid(True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T10:29:44.448615Z","iopub.execute_input":"2024-05-09T10:29:44.449090Z","iopub.status.idle":"2024-05-09T10:29:46.325459Z","shell.execute_reply.started":"2024-05-09T10:29:44.449057Z","shell.execute_reply":"2024-05-09T10:29:46.324415Z"},"trusted":true},"execution_count":null,"outputs":[]}]}