{"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)\nimport matplotlib.pyplot as plt\nfrom sklearn.decomposition import PCA\nfrom sklearn.preprocessing import StandardScaler\nfrom tensorflow.keras.datasets import fashion_mnist\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-08T16:10:19.803710Z","iopub.execute_input":"2024-05-08T16:10:19.804066Z","iopub.status.idle":"2024-05-08T16:10:19.811905Z","shell.execute_reply.started":"2024-05-08T16:10:19.804040Z","shell.execute_reply":"2024-05-08T16:10:19.810880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"먼저, train 데이터와 test 데이터를 불러오자","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/2024-smarcle-ks-3-fashion-mnist2/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-08T16:10:22.336475Z","iopub.execute_input":"2024-05-08T16:10:22.336853Z","iopub.status.idle":"2024-05-08T16:10:26.839757Z","shell.execute_reply.started":"2024-05-08T16:10:22.336823Z","shell.execute_reply":"2024-05-08T16:10:26.838636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"일단 train데이터를 보며 어떤 데이터가 들어있는지 파악해보자.","metadata":{}},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2024-05-08T16:10:29.076340Z","iopub.execute_input":"2024-05-08T16:10:29.076801Z","iopub.status.idle":"2024-05-08T16:10:29.101386Z","shell.execute_reply.started":"2024-05-08T16:10:29.076768Z","shell.execute_reply":"2024-05-08T16:10:29.100379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"총 60000개의 데이터로 구성되어있으며, 회색조 이미지가 28X28이므로 총 784개의 픽셀이 존재한다. 각 픽셀의 밝기 값이 저장되어있다.\n첫번째 열의 label은 각 이미지의 제품 종류로, 각 숫자는 다음 제품을 의미한다.\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","metadata":{}},{"cell_type":"code","source":"test","metadata":{"execution":{"iopub.status.busy":"2024-05-08T16:11:02.507374Z","iopub.execute_input":"2024-05-08T16:11:02.508602Z","iopub.status.idle":"2024-05-08T16:11:02.529261Z","shell.execute_reply.started":"2024-05-08T16:11:02.508566Z","shell.execute_reply":"2024-05-08T16:11:02.528188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"총 10000개의 데이터가 있다","metadata":{}},{"cell_type":"code","source":"# Fashion MNIST 데이터 불러오기\n(train_images, train_labels), (_, _) = fashion_mnist.load_data()\n\n# 이미지 데이터를 1차원으로 평탄화하고 정규화\nX = train_images.reshape(-1, 28*28) / 255.0\n# PCA를 위해 데이터를 표준화\nscaler = StandardScaler()\nX_scaled = scaler.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T16:10:58.175376Z","iopub.execute_input":"2024-05-08T16:10:58.176267Z","iopub.status.idle":"2024-05-08T16:10:59.353855Z","shell.execute_reply.started":"2024-05-08T16:10:58.176234Z","shell.execute_reply":"2024-05-08T16:10:59.352954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# PCA 모델 설정\npca = PCA(n_components=2)  # 주성분 개수를 2로 설정\n\n# PCA 적용\nX_pca = pca.fit_transform(X_scaled)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T16:11:07.711914Z","iopub.execute_input":"2024-05-08T16:11:07.712647Z","iopub.status.idle":"2024-05-08T16:11:09.269188Z","shell.execute_reply.started":"2024-05-08T16:11:07.712616Z","shell.execute_reply":"2024-05-08T16:11:09.268260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 주성분 분석 결과 시각화\nplt.figure(figsize=(10, 6))\nfor label in range(10):\n    plt.scatter(X_pca[train_labels == label, 0], X_pca[train_labels == label, 1], label=str(label))\nplt.title('Fashion MNIST with PCA')\nplt.xlabel('Principal Component 1')\nplt.ylabel('Principal Component 2')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T16:11:31.023973Z","iopub.execute_input":"2024-05-08T16:11:31.024370Z","iopub.status.idle":"2024-05-08T16:11:32.107854Z","shell.execute_reply.started":"2024-05-08T16:11:31.024340Z","shell.execute_reply":"2024-05-08T16:11:32.106674Z"},"trusted":true},"execution_count":null,"outputs":[]}]}