{"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\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":"2021-07-15T12:41:43.443755Z","iopub.execute_input":"2021-07-15T12:41:43.444580Z","iopub.status.idle":"2021-07-15T12:41:43.453739Z","shell.execute_reply.started":"2021-07-15T12:41:43.444531Z","shell.execute_reply":"2021-07-15T12:41:43.452641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntrain.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:43.455595Z","iopub.execute_input":"2021-07-15T12:41:43.455968Z","iopub.status.idle":"2021-07-15T12:41:47.563630Z","shell.execute_reply.started":"2021-07-15T12:41:43.455901Z","shell.execute_reply":"2021-07-15T12:41:47.562367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:47.566255Z","iopub.execute_input":"2021-07-15T12:41:47.566740Z","iopub.status.idle":"2021-07-15T12:41:49.904841Z","shell.execute_reply.started":"2021-07-15T12:41:47.566690Z","shell.execute_reply":"2021-07-15T12:41:49.903422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsample.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:49.907124Z","iopub.execute_input":"2021-07-15T12:41:49.907590Z","iopub.status.idle":"2021-07-15T12:41:49.935057Z","shell.execute_reply.started":"2021-07-15T12:41:49.907555Z","shell.execute_reply":"2021-07-15T12:41:49.933802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#繪圖套件\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:49.936865Z","iopub.execute_input":"2021-07-15T12:41:49.937250Z","iopub.status.idle":"2021-07-15T12:41:50.965988Z","shell.execute_reply.started":"2021-07-15T12:41:49.937216Z","shell.execute_reply":"2021-07-15T12:41:50.964871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x = train[\"label\"])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:50.967285Z","iopub.execute_input":"2021-07-15T12:41:50.967765Z","iopub.status.idle":"2021-07-15T12:41:51.197262Z","shell.execute_reply.started":"2021-07-15T12:41:50.967731Z","shell.execute_reply":"2021-07-15T12:41:51.196124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#變更數據形狀\ntrain_y = train['label']\ntrain.pop('label')\n\ntrain_x = train.values.reshape(-1, 28, 28, 1)\ntest_x = test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:51.198587Z","iopub.execute_input":"2021-07-15T12:41:51.199084Z","iopub.status.idle":"2021-07-15T12:41:51.323593Z","shell.execute_reply.started":"2021-07-15T12:41:51.199047Z","shell.execute_reply":"2021-07-15T12:41:51.322704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#據說這樣會讓類神經網路效果好上很多\ntrain_x = train_x/255\ntest_x = test_x/255","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:51.325667Z","iopub.execute_input":"2021-07-15T12:41:51.326153Z","iopub.status.idle":"2021-07-15T12:41:51.558783Z","shell.execute_reply.started":"2021-07-15T12:41:51.326118Z","shell.execute_reply":"2021-07-15T12:41:51.557818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tf來啦\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout,LSTM\nfrom tensorflow.keras import callbacks","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:51.560180Z","iopub.execute_input":"2021-07-15T12:41:51.560623Z","iopub.status.idle":"2021-07-15T12:41:58.492284Z","shell.execute_reply.started":"2021-07-15T12:41:51.560591Z","shell.execute_reply":"2021-07-15T12:41:58.491372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential([\n    Conv2D(32, kernel_size=(5,5) , activation='relu' , input_shape=(28,28,1)),\n    MaxPooling2D(pool_size=(2,2)),\n    \n    Conv2D(64, kernel_size=(3,3), activation ='relu'),\n    Dropout(0.25),\n    MaxPooling2D(pool_size=(2,2)),\n    \n    Conv2D(64, kernel_size=(3,3), activation ='relu'),\n    Dropout(0.25),\n    MaxPooling2D(pool_size=(2,2)),\n    \n    Flatten(),\n\n    Dense(64, activation='relu'),\n    Dense(32, activation='relu'),\n    Dense(16, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:58.493591Z","iopub.execute_input":"2021-07-15T12:41:58.494118Z","iopub.status.idle":"2021-07-15T12:41:58.857699Z","shell.execute_reply.started":"2021-07-15T12:41:58.494067Z","shell.execute_reply":"2021-07-15T12:41:58.856622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = callbacks.EarlyStopping(monitor='loss', patience=5)","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:58.859133Z","iopub.execute_input":"2021-07-15T12:41:58.859897Z","iopub.status.idle":"2021-07-15T12:41:58.864644Z","shell.execute_reply.started":"2021-07-15T12:41:58.859845Z","shell.execute_reply":"2021-07-15T12:41:58.863527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:58.866193Z","iopub.execute_input":"2021-07-15T12:41:58.866977Z","iopub.status.idle":"2021-07-15T12:41:58.893138Z","shell.execute_reply.started":"2021-07-15T12:41:58.866930Z","shell.execute_reply":"2021-07-15T12:41:58.892236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_x, train_y, epochs=30, batch_size=64, callbacks=[callback])","metadata":{"execution":{"iopub.status.busy":"2021-07-15T12:41:58.894719Z","iopub.execute_input":"2021-07-15T12:41:58.895375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y = model.predict(test_x)\npred_y = np.argmax(test_y,axis = 1) \npred_y","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmission_data['Label'] = pred_y","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_data.to_csv('submit.csv' ,index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}