{"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-08T18:42:40.001890Z","iopub.execute_input":"2024-05-08T18:42:40.002389Z","iopub.status.idle":"2024-05-08T18:42:40.019973Z","shell.execute_reply.started":"2024-05-08T18:42:40.002351Z","shell.execute_reply":"2024-05-08T18:42:40.018262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-08T18:42:42.435273Z","iopub.execute_input":"2024-05-08T18:42:42.435719Z","iopub.status.idle":"2024-05-08T18:42:50.479805Z","shell.execute_reply.started":"2024-05-08T18:42:42.435684Z","shell.execute_reply":"2024-05-08T18:42:50.478339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.datasets import fashion_mnist\n(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:42:51.035679Z","iopub.execute_input":"2024-05-08T18:42:51.036112Z","iopub.status.idle":"2024-05-08T18:42:51.472017Z","shell.execute_reply.started":"2024-05-08T18:42:51.036064Z","shell.execute_reply":"2024-05-08T18:42:51.470608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"전체적인 이미지의 모습은 이렇다!","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize=(10, 10))\nfor i in range(16):\n    plt.subplot(4, 4, i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(X_train[i], cmap=plt.cm.binary)\n    plt.title(\"Image label is: {}\".format(train))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:53:27.449105Z","iopub.execute_input":"2024-05-08T18:53:27.449545Z","iopub.status.idle":"2024-05-08T18:53:28.320189Z","shell.execute_reply.started":"2024-05-08T18:53:27.449513Z","shell.execute_reply":"2024-05-08T18:53:28.318929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## WTF is PCA?\nPCA(Principal Component Analysis), 주성분 분석이라는 뜻  \n선형대수학에서의 정사영을 통한 차원축소와 밀접한 관련이 있으며, 이를 데이터 분석에 적용시켜 과적합을 방지하고 효율적으로 차원을 축소하여 나은 데이터를 만드는 분석을 뜻한다.  \n데이터의 공분산 행렬을 구하고, 이의 eigen vector, eigen value를 구하여 정사영할 최적의 vector를 찾아, 정사영 후의 낮아진 차원을 얻는 분석이다.  \n  \n그럼 이걸 코드로 구현해보자(라고했지만 사실상 reshape이다.. 추후 수학적 개념에 맞춰 수정예정)","metadata":{}},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:30:20.394223Z","iopub.execute_input":"2024-05-08T18:30:20.394635Z","iopub.status.idle":"2024-05-08T18:30:20.402896Z","shell.execute_reply.started":"2024-05-08T18:30:20.394599Z","shell.execute_reply":"2024-05-08T18:30:20.401331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"2차원 데이터인 것을 확인할 수 있다.  \n이를 1차원 데이터로 바꾸어주는 것을 진행하겠음","metadata":{}},{"cell_type":"code","source":"X_train = X_train.reshape(X_train.shape[0], 784)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:30:24.350034Z","iopub.execute_input":"2024-05-08T18:30:24.350476Z","iopub.status.idle":"2024-05-08T18:30:24.356513Z","shell.execute_reply.started":"2024-05-08T18:30:24.350446Z","shell.execute_reply":"2024-05-08T18:30:24.355214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"index를 제외하면 각각1차원 배열이다.","metadata":{}},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:30:32.738947Z","iopub.execute_input":"2024-05-08T18:30:32.739439Z","iopub.status.idle":"2024-05-08T18:30:32.748340Z","shell.execute_reply.started":"2024-05-08T18:30:32.739404Z","shell.execute_reply":"2024-05-08T18:30:32.747027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"1차원으로 바뀐 것을 확인할 수 있음!","metadata":{}},{"cell_type":"markdown","source":"keras는 데이터를 0~1 사이의 값으로 변환한 다음 구동 시 최적의 성능을 보인다고한다.   \n-> **Data normalization**","metadata":{}},{"cell_type":"code","source":"X_train = X_train.astype('float64')\nX_train = X_train / 255","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:30:55.823713Z","iopub.execute_input":"2024-05-08T18:30:55.824172Z","iopub.status.idle":"2024-05-08T18:30:56.112919Z","shell.execute_reply.started":"2024-05-08T18:30:55.824139Z","shell.execute_reply":"2024-05-08T18:30:56.111516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"test데이터에도 마찬가지로 같은 작업(PCA) 실행","metadata":{}},{"cell_type":"code","source":"X_test = X_test.reshape(X_test.shape[0], 784).astype('float64') /255\n","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:30:58.214069Z","iopub.execute_input":"2024-05-08T18:30:58.214558Z","iopub.status.idle":"2024-05-08T18:30:58.249721Z","shell.execute_reply.started":"2024-05-08T18:30:58.214524Z","shell.execute_reply":"2024-05-08T18:30:58.248311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"0 - 티셔츠/상의  \n1 - 트라우저  \n2 - 풀오버  \n3 - 드레스  \n4 - 코트  \n5 - 샌달  \n6 - 셔츠  \n7 - 스니커즈  \n8 - 가방  \n9 - 앵클 부츠  ","metadata":{}},{"cell_type":"markdown","source":"15개 이하의, 각 번호들이 '의미하는 바' 가 있으므로 categorical data -> one hot encoding을 적용한다","metadata":{}},{"cell_type":"code","source":"len(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:31:02.811851Z","iopub.execute_input":"2024-05-08T18:31:02.812317Z","iopub.status.idle":"2024-05-08T18:31:02.820995Z","shell.execute_reply.started":"2024-05-08T18:31:02.812283Z","shell.execute_reply":"2024-05-08T18:31:02.819664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"keras의 to_categorical 함수를 통해 y(index)를 0~9까지 One-Hot Encoding 해주었다.","metadata":{}},{"cell_type":"code","source":"from tensorflow.python.keras.utils import np_utils\n\ny_OHC_train = np_utils.to_categorical(y_train, 10)\ny_OHC_test = np_utils.to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:31:14.883521Z","iopub.execute_input":"2024-05-08T18:31:14.883977Z","iopub.status.idle":"2024-05-08T18:31:14.893686Z","shell.execute_reply.started":"2024-05-08T18:31:14.883943Z","shell.execute_reply":"2024-05-08T18:31:14.892003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_OHC_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:31:20.457411Z","iopub.execute_input":"2024-05-08T18:31:20.457872Z","iopub.status.idle":"2024-05-08T18:31:20.465950Z","shell.execute_reply.started":"2024-05-08T18:31:20.457837Z","shell.execute_reply":"2024-05-08T18:31:20.464456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"아래는 모델링 코드. 입/출력층 외에는 추가하지 않았음","metadata":{}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense\nmodel = Sequential()\nmodel.add(Dense(512, input_dim=784, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:31:23.818899Z","iopub.execute_input":"2024-05-08T18:31:23.819368Z","iopub.status.idle":"2024-05-08T18:31:23.912545Z","shell.execute_reply.started":"2024-05-08T18:31:23.819335Z","shell.execute_reply":"2024-05-08T18:31:23.911267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',\n             optimizer='adam',\n             metrics=['accuracy'])\nhistory = model.fit(X_train, y_OHC_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:31:56.749052Z","iopub.execute_input":"2024-05-08T18:31:56.749576Z","iopub.status.idle":"2024-05-08T18:32:18.675374Z","shell.execute_reply.started":"2024-05-08T18:31:56.749534Z","shell.execute_reply":"2024-05-08T18:32:18.674016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"\\n Test Accuracy: %.4f\" % (model.evaluate(X_test, y_OHC_test)[1]))","metadata":{"execution":{"iopub.status.busy":"2024-05-08T18:32:29.694199Z","iopub.execute_input":"2024-05-08T18:32:29.694659Z","iopub.status.idle":"2024-05-08T18:32:30.737190Z","shell.execute_reply.started":"2024-05-08T18:32:29.694615Z","shell.execute_reply":"2024-05-08T18:32:30.735864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2024-05-08T12:24:19.042687Z","iopub.execute_input":"2024-05-08T12:24:19.043090Z","iopub.status.idle":"2024-05-08T12:24:19.087343Z","shell.execute_reply.started":"2024-05-08T12:24:19.043058Z","shell.execute_reply":"2024-05-08T12:24:19.085732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}