{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\n#找出你要用的資料集, 瀏覽資料夾有哪些檔案, 顯示出來\n# 完整的路徑：包含資料夾 + 檔案名稱\n\nfiles = []\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        #把檔名存下來\n        files.append(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":"2023-08-20T06:20:37.343493Z","iopub.execute_input":"2023-08-20T06:20:37.343873Z","iopub.status.idle":"2023-08-20T06:20:37.352756Z","shell.execute_reply.started":"2023-08-20T06:20:37.343846Z","shell.execute_reply":"2023-08-20T06:20:37.351472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:20:44.702368Z","iopub.execute_input":"2023-08-20T06:20:44.702771Z","iopub.status.idle":"2023-08-20T06:20:44.711016Z","shell.execute_reply.started":"2023-08-20T06:20:44.702742Z","shell.execute_reply":"2023-08-20T06:20:44.709188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(files[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:21:49.092901Z","iopub.execute_input":"2023-08-20T06:21:49.093275Z","iopub.status.idle":"2023-08-20T06:21:51.503995Z","shell.execute_reply.started":"2023-08-20T06:21:49.093251Z","shell.execute_reply":"2023-08-20T06:21:51.502574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:22:17.461511Z","iopub.execute_input":"2023-08-20T06:22:17.461843Z","iopub.status.idle":"2023-08-20T06:22:17.469143Z","shell.execute_reply.started":"2023-08-20T06:22:17.461815Z","shell.execute_reply":"2023-08-20T06:22:17.468025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 28 x 28 pixel 圖：flatten 攤開攤平 784 vecor","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.columns","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:23:15.390886Z","iopub.execute_input":"2023-08-20T06:23:15.391211Z","iopub.status.idle":"2023-08-20T06:23:15.396603Z","shell.execute_reply.started":"2023-08-20T06:23:15.391188Z","shell.execute_reply":"2023-08-20T06:23:15.395949Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train中的每一個row就是一張小圖片 + 標籤\nY = df_train['label']\n","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:28:32.835328Z","iopub.execute_input":"2023-08-20T06:28:32.835640Z","iopub.status.idle":"2023-08-20T06:28:32.840710Z","shell.execute_reply.started":"2023-08-20T06:28:32.835618Z","shell.execute_reply":"2023-08-20T06:28:32.839506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.iloc[:,1:]\nX # X 依舊是dataframe","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:28:35.026362Z","iopub.execute_input":"2023-08-20T06:28:35.026725Z","iopub.status.idle":"2023-08-20T06:28:35.047917Z","shell.execute_reply.started":"2023-08-20T06:28:35.026697Z","shell.execute_reply":"2023-08-20T06:28:35.046520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = Y.values\nX = X.values","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:28:37.269627Z","iopub.execute_input":"2023-08-20T06:28:37.269990Z","iopub.status.idle":"2023-08-20T06:28:37.275244Z","shell.execute_reply.started":"2023-08-20T06:28:37.269964Z","shell.execute_reply":"2023-08-20T06:28:37.274414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualization 畫圖\n# 原本 784 vector 轉成28x28 畫出來\n\nimport matplotlib.pyplot as plt\ni = 3 # 第i張圖\nimage = X[i,:].reshape(28, 28)\nplt.imshow(image)\n\nprint(\"the label is\",Y[i])","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:50:17.241643Z","iopub.execute_input":"2023-08-20T06:50:17.241993Z","iopub.status.idle":"2023-08-20T06:50:17.436125Z","shell.execute_reply.started":"2023-08-20T06:50:17.241967Z","shell.execute_reply":"2023-08-20T06:50:17.434781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 看16張圖\n\nfor i in range(16):\n    \n    plt.subplot(4,4, i+1)  # 畫面分割4x4, 第i +1 張小圖\n    image = X[i,:].reshape(28, 28)\n    plt.imshow(image)\n     ","metadata":{"execution":{"iopub.status.busy":"2023-08-20T06:56:29.453674Z","iopub.execute_input":"2023-08-20T06:56:29.454058Z","iopub.status.idle":"2023-08-20T06:56:30.983105Z","shell.execute_reply.started":"2023-08-20T06:56:29.454012Z","shell.execute_reply":"2023-08-20T06:56:30.982019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.cluster import KMeans\nK = 20\nkmeans = KMeans(n_clusters=K).fit(X)\ncluster_centers = kmeans.cluster_centers_ ","metadata":{"execution":{"iopub.status.busy":"2023-08-20T07:17:42.570663Z","iopub.execute_input":"2023-08-20T07:17:42.571018Z","iopub.status.idle":"2023-08-20T07:18:24.530653Z","shell.execute_reply.started":"2023-08-20T07:17:42.570993Z","shell.execute_reply":"2023-08-20T07:18:24.529673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,8))\nfor i in range(20):\n    \n    plt.subplot(4,5, i+1)  # 畫面分割2x5, 第i +1 張小圖\n    image = cluster_centers[i,:].reshape(28, 28)\n    plt.imshow(image)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-20T07:18:27.401131Z","iopub.execute_input":"2023-08-20T07:18:27.401465Z","iopub.status.idle":"2023-08-20T07:18:29.516589Z","shell.execute_reply.started":"2023-08-20T07:18:27.401428Z","shell.execute_reply":"2023-08-20T07:18:29.515244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}