{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30527,"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\nfiles = []\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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-11-18T02:01:21.155079Z","iopub.execute_input":"2023-11-18T02:01:21.155409Z","iopub.status.idle":"2023-11-18T02:01:21.188315Z","shell.execute_reply.started":"2023-11-18T02:01:21.155379Z","shell.execute_reply":"2023-11-18T02:01:21.187378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files","metadata":{"execution":{"iopub.status.busy":"2023-11-18T02:01:24.429344Z","iopub.execute_input":"2023-11-18T02:01:24.429755Z","iopub.status.idle":"2023-11-18T02:01:24.438235Z","shell.execute_reply.started":"2023-11-18T02:01:24.429713Z","shell.execute_reply":"2023-11-18T02:01:24.437217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(files[1])\ndf_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-18T02:01:31.115776Z","iopub.execute_input":"2023-11-18T02:01:31.116204Z","iopub.status.idle":"2023-11-18T02:01:35.107184Z","shell.execute_reply.started":"2023-11-18T02:01:31.116171Z","shell.execute_reply":"2023-11-18T02:01:35.106189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:05:33.190476Z","iopub.execute_input":"2023-08-20T04:05:33.190916Z","iopub.status.idle":"2023-08-20T04:05:35.738314Z","shell.execute_reply.started":"2023-08-20T04:05:33.190881Z","shell.execute_reply":"2023-08-20T04:05:35.737315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = df_train.iloc[:,0]\nX = df_train.iloc[:,1:]","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:06:52.791406Z","iopub.execute_input":"2023-08-20T04:06:52.791803Z","iopub.status.idle":"2023-08-20T04:06:52.797184Z","shell.execute_reply.started":"2023-08-20T04:06:52.791771Z","shell.execute_reply":"2023-08-20T04:06:52.796001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\n#使用iloc 取出部份的數值\n# 利用.values得到單純的數值陣列\ni = 0\nimg_pixels_i = X.iloc[i,:].values\nplt.imshow(img_pixels_i.reshape(28, 28))","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:08:21.928478Z","iopub.execute_input":"2023-08-20T04:08:21.928931Z","iopub.status.idle":"2023-08-20T04:08:22.25879Z","shell.execute_reply.started":"2023-08-20T04:08:21.928894Z","shell.execute_reply":"2023-08-20T04:08:22.257817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# visualize a few samples","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfor i in range(16):\n    img_pixels_i = X.iloc[i,:].values\n    plt.subplot(4,4,i+1)  # 4x4當中的第i+1  張圖\n    plt.imshow(img_pixels_i.reshape(28, 28))\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:54:02.913129Z","iopub.execute_input":"2023-08-20T04:54:02.913522Z","iopub.status.idle":"2023-08-20T04:54:05.093592Z","shell.execute_reply.started":"2023-08-20T04:54:02.913492Z","shell.execute_reply":"2023-08-20T04:54:05.092266Z"},"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    img_pixels_i = X.iloc[i,:].values\n    plt.subplot(4,4,i+1)  # 4x4當中的第i+1  張圖\n    plt.imshow(img_pixels_i.reshape(28, 28))","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:55:44.32951Z","iopub.execute_input":"2023-08-20T04:55:44.330018Z","iopub.status.idle":"2023-08-20T04:55:47.039242Z","shell.execute_reply.started":"2023-08-20T04:55:44.329978Z","shell.execute_reply":"2023-08-20T04:55:47.037978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# cluster the digits","metadata":{}},{"cell_type":"code","source":"from sklearn.cluster import KMeans\nkmeans = KMeans(n_clusters=10, random_state=0, n_init=\"auto\").fit(X)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:58:10.701842Z","iopub.execute_input":"2023-08-20T04:58:10.702251Z","iopub.status.idle":"2023-08-20T04:58:13.455469Z","shell.execute_reply.started":"2023-08-20T04:58:10.702221Z","shell.execute_reply":"2023-08-20T04:58:13.454558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_labels = kmeans.fit_transform(X)\n","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:09:40.862286Z","iopub.execute_input":"2023-08-20T04:09:40.8627Z","iopub.status.idle":"2023-08-20T04:09:43.898459Z","shell.execute_reply.started":"2023-08-20T04:09:40.862665Z","shell.execute_reply":"2023-08-20T04:09:43.896676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Clusters的中心點","metadata":{}},{"cell_type":"code","source":"k_centers = kmeans.cluster_centers_","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:58:28.137955Z","iopub.execute_input":"2023-08-20T04:58:28.138382Z","iopub.status.idle":"2023-08-20T04:58:28.143905Z","shell.execute_reply.started":"2023-08-20T04:58:28.138346Z","shell.execute_reply":"2023-08-20T04:58:28.142711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,4))\nfor j, cc in enumerate(k_centers):\n    img = cc.reshape(28,28)\n    plt.subplot(2, 5, j+1)\n    plt.imshow(img)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:58:39.492122Z","iopub.execute_input":"2023-08-20T04:58:39.492537Z","iopub.status.idle":"2023-08-20T04:58:40.996304Z","shell.execute_reply.started":"2023-08-20T04:58:39.492502Z","shell.execute_reply":"2023-08-20T04:58:40.995097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# 有些數字不見了？","metadata":{}},{"cell_type":"code","source":"K = 12\n\nkmeans = KMeans(n_clusters=K, random_state=0, n_init=\"auto\").fit(X)\nk_centers = kmeans.cluster_centers_\nplt.figure(figsize = (10,4))\nfor j, cc in enumerate(k_centers):\n    img = cc.reshape(28,28)\n    plt.subplot(2, 6, j+1)\n    plt.imshow(img)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-08-20T04:58:55.09437Z","iopub.execute_input":"2023-08-20T04:58:55.094819Z","iopub.status.idle":"2023-08-20T04:59:03.639968Z","shell.execute_reply.started":"2023-08-20T04:58:55.094783Z","shell.execute_reply":"2023-08-20T04:59:03.636933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"K = 14\n\nkmeans = KMeans(n_clusters=K, random_state=0, n_init=\"auto\").fit(X)\nk_centers = kmeans.cluster_centers_\nplt.figure(figsize = (12,4))\nfor j, cc in enumerate(k_centers):\n    img = cc.reshape(28,28)\n    plt.subplot(2, 7, j+1)\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T05:00:17.909609Z","iopub.execute_input":"2023-08-20T05:00:17.910054Z","iopub.status.idle":"2023-08-20T05:00:28.916011Z","shell.execute_reply.started":"2023-08-20T05:00:17.910021Z","shell.execute_reply":"2023-08-20T05:00:28.914802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 試試看，K要選多大才能將所有數字分開？","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 想想看, 你要怎利用kmeans做手寫數字辨識？","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 主要成份分析\n\n將手寫數字投影在二維平面上面","metadata":{}},{"cell_type":"code","source":"from sklearn.decomposition import PCA\nk = 2\npca = PCA(n_components=2)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T05:06:02.257174Z","iopub.execute_input":"2023-08-20T05:06:02.258663Z","iopub.status.idle":"2023-08-20T05:06:02.263684Z","shell.execute_reply.started":"2023-08-20T05:06:02.258594Z","shell.execute_reply":"2023-08-20T05:06:02.262279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pca.fit(X)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T05:06:27.328386Z","iopub.execute_input":"2023-08-20T05:06:27.328849Z","iopub.status.idle":"2023-08-20T05:06:30.419622Z","shell.execute_reply.started":"2023-08-20T05:06:27.328811Z","shell.execute_reply":"2023-08-20T05:06:30.418366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_pca = pca.fit_transform(X)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T05:07:43.988357Z","iopub.execute_input":"2023-08-20T05:07:43.988748Z","iopub.status.idle":"2023-08-20T05:07:47.531752Z","shell.execute_reply.started":"2023-08-20T05:07:43.988717Z","shell.execute_reply":"2023-08-20T05:07:47.530693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 10))\nfor i  in range(10):\n    index_i = np.where(Y==i)[0]\n    \n    plt.scatter(X_pca[index_i,0], X_pca[index_i,1])\n    \nlabels = [str(i) for i in range(10)]\nplt.legend(labels)","metadata":{"execution":{"iopub.status.busy":"2023-08-20T05:14:59.102674Z","iopub.execute_input":"2023-08-20T05:14:59.103099Z","iopub.status.idle":"2023-08-20T05:15:00.480161Z","shell.execute_reply.started":"2023-08-20T05:14:59.103065Z","shell.execute_reply":"2023-08-20T05:15:00.478963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"可以觀察到相似的字, 位置也接近\n\n例如：9和7位置很多重疊\n\n1 和 0距離比較遠 （因為比較不像）","metadata":{}},{"cell_type":"code","source":"X_pca.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-20T05:09:37.533983Z","iopub.execute_input":"2023-08-20T05:09:37.534356Z","iopub.status.idle":"2023-08-20T05:09:37.54129Z","shell.execute_reply.started":"2023-08-20T05:09:37.534327Z","shell.execute_reply":"2023-08-20T05:09:37.540188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}