{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":77476,"databundleVersionId":8462666,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-16T05:12:51.826131Z","iopub.execute_input":"2024-05-16T05:12:51.827076Z","iopub.status.idle":"2024-05-16T05:12:51.835102Z","shell.execute_reply.started":"2024-05-16T05:12:51.827040Z","shell.execute_reply":"2024-05-16T05:12:51.834092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.datasets import fashion_mnist\nfrom sklearn.decomposition import PCA","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:12:51.837001Z","iopub.execute_input":"2024-05-16T05:12:51.837786Z","iopub.status.idle":"2024-05-16T05:12:51.842040Z","shell.execute_reply.started":"2024-05-16T05:12:51.837751Z","shell.execute_reply":"2024-05-16T05:12:51.841092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:12:51.843113Z","iopub.execute_input":"2024-05-16T05:12:51.843424Z","iopub.status.idle":"2024-05-16T05:12:52.217626Z","shell.execute_reply.started":"2024-05-16T05:12:51.843397Z","shell.execute_reply":"2024-05-16T05:12:52.216617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1, 784) / 255.0\nX_test = X_test.reshape(-1, 784) / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:12:52.220468Z","iopub.execute_input":"2024-05-16T05:12:52.220779Z","iopub.status.idle":"2024-05-16T05:12:52.363494Z","shell.execute_reply.started":"2024-05-16T05:12:52.220754Z","shell.execute_reply":"2024-05-16T05:12:52.362402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:12:52.364783Z","iopub.execute_input":"2024-05-16T05:12:52.365099Z","iopub.status.idle":"2024-05-16T05:12:52.371030Z","shell.execute_reply.started":"2024-05-16T05:12:52.365075Z","shell.execute_reply":"2024-05-16T05:12:52.370126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca = PCA(n_components=300)\npca.fit(X_train)\nprint(np.sum(pca.explained_variance_ratio_))","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:12:52.372285Z","iopub.execute_input":"2024-05-16T05:12:52.372590Z","iopub.status.idle":"2024-05-16T05:13:01.920497Z","shell.execute_reply.started":"2024-05-16T05:12:52.372551Z","shell.execute_reply":"2024-05-16T05:13:01.919398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_pca = pca.transform(X_train)\nX_test_pca = pca.transform(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:01.921775Z","iopub.execute_input":"2024-05-16T05:13:01.922054Z","iopub.status.idle":"2024-05-16T05:13:02.530234Z","shell.execute_reply.started":"2024-05-16T05:13:01.922031Z","shell.execute_reply":"2024-05-16T05:13:02.528865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_pca.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:02.532116Z","iopub.execute_input":"2024-05-16T05:13:02.533276Z","iopub.status.idle":"2024-05-16T05:13:02.541040Z","shell.execute_reply.started":"2024-05-16T05:13:02.533228Z","shell.execute_reply":"2024-05-16T05:13:02.539929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras import Sequential\nfrom keras.layers import Dense\nmodel=Sequential()\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:02.543280Z","iopub.execute_input":"2024-05-16T05:13:02.544202Z","iopub.status.idle":"2024-05-16T05:13:02.559568Z","shell.execute_reply.started":"2024-05-16T05:13:02.544154Z","shell.execute_reply":"2024-05-16T05:13:02.558400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_pca","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:02.564776Z","iopub.execute_input":"2024-05-16T05:13:02.565676Z","iopub.status.idle":"2024-05-16T05:13:02.575800Z","shell.execute_reply.started":"2024-05-16T05:13:02.565630Z","shell.execute_reply":"2024-05-16T05:13:02.574124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(X_train_pca, y_train, epochs=30, batch_size=200, validation_data=(X_test_pca, y_test))","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:02.578058Z","iopub.execute_input":"2024-05-16T05:13:02.579056Z","iopub.status.idle":"2024-05-16T05:13:25.692794Z","shell.execute_reply.started":"2024-05-16T05:13:02.579005Z","shell.execute_reply":"2024-05-16T05:13:25.691954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted = model.predict(X_test_pca)\npredicted_array = np.argmax(predicted, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:25.693858Z","iopub.execute_input":"2024-05-16T05:13:25.694149Z","iopub.status.idle":"2024-05-16T05:13:26.498261Z","shell.execute_reply.started":"2024-05-16T05:13:25.694125Z","shell.execute_reply":"2024-05-16T05:13:26.497281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_array","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:26.499713Z","iopub.execute_input":"2024-05-16T05:13:26.500410Z","iopub.status.idle":"2024-05-16T05:13:26.506542Z","shell.execute_reply.started":"2024-05-16T05:13:26.500362Z","shell.execute_reply":"2024-05-16T05:13:26.505394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:26.507918Z","iopub.execute_input":"2024-05-16T05:13:26.508221Z","iopub.status.idle":"2024-05-16T05:13:26.516024Z","shell.execute_reply.started":"2024-05-16T05:13:26.508196Z","shell.execute_reply":"2024-05-16T05:13:26.515004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = model.evaluate(X_test_pca, y_test)\nprint(accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:26.517135Z","iopub.execute_input":"2024-05-16T05:13:26.517391Z","iopub.status.idle":"2024-05-16T05:13:27.525945Z","shell.execute_reply.started":"2024-05-16T05:13:26.517369Z","shell.execute_reply":"2024-05-16T05:13:27.525054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame()\nsub['ImageId'] = range(1, len(predicted_array)+1)\nsub['label'] = predicted_array\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:52.962202Z","iopub.execute_input":"2024-05-16T05:13:52.962954Z","iopub.status.idle":"2024-05-16T05:13:52.984275Z","shell.execute_reply.started":"2024-05-16T05:13:52.962918Z","shell.execute_reply":"2024-05-16T05:13:52.983253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-16T05:13:59.869777Z","iopub.execute_input":"2024-05-16T05:13:59.870531Z","iopub.status.idle":"2024-05-16T05:13:59.883503Z","shell.execute_reply.started":"2024-05-16T05:13:59.870500Z","shell.execute_reply":"2024-05-16T05:13:59.882649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}