{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-18T19:30:49.474905Z","iopub.execute_input":"2021-11-18T19:30:49.475261Z","iopub.status.idle":"2021-11-18T19:30:49.486369Z","shell.execute_reply.started":"2021-11-18T19:30:49.475224Z","shell.execute_reply":"2021-11-18T19:30:49.485399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Загрузка обучающей и тестовой выборки","metadata":{}},{"cell_type":"code","source":"data_train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ndata_test = pd.read_csv(\"../input/digit-recognizer/test.csv\")\ndata_train.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:49.487859Z","iopub.execute_input":"2021-11-18T19:30:49.488268Z","iopub.status.idle":"2021-11-18T19:30:54.189991Z","shell.execute_reply.started":"2021-11-18T19:30:49.488223Z","shell.execute_reply":"2021-11-18T19:30:54.188921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.191475Z","iopub.execute_input":"2021-11-18T19:30:54.191712Z","iopub.status.idle":"2021-11-18T19:30:54.209806Z","shell.execute_reply.started":"2021-11-18T19:30:54.191682Z","shell.execute_reply":"2021-11-18T19:30:54.208946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.211969Z","iopub.execute_input":"2021-11-18T19:30:54.212207Z","iopub.status.idle":"2021-11-18T19:30:54.224261Z","shell.execute_reply.started":"2021-11-18T19:30:54.212177Z","shell.execute_reply":"2021-11-18T19:30:54.223390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.225605Z","iopub.execute_input":"2021-11-18T19:30:54.225866Z","iopub.status.idle":"2021-11-18T19:30:54.236107Z","shell.execute_reply.started":"2021-11-18T19:30:54.225837Z","shell.execute_reply":"2021-11-18T19:30:54.235405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Установка признаков для обучающей выборки","metadata":{}},{"cell_type":"code","source":"X_train = data_train.drop('label', axis = 1)\ny_train = data_train['label']\nprint(X_train.shape, y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.237060Z","iopub.execute_input":"2021-11-18T19:30:54.237313Z","iopub.status.idle":"2021-11-18T19:30:54.352820Z","shell.execute_reply.started":"2021-11-18T19:30:54.237273Z","shell.execute_reply":"2021-11-18T19:30:54.352104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Разделение данных на обучающую и на тестовую выборку с размером набора данных для проверки 30%.","metadata":{}},{"cell_type":"code","source":"train_x, test_x, train_y, test_y = train_test_split(X_train, y_train, test_size = 0.3, random_state = 108)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.353834Z","iopub.execute_input":"2021-11-18T19:30:54.354130Z","iopub.status.idle":"2021-11-18T19:30:54.626231Z","shell.execute_reply.started":"2021-11-18T19:30:54.354102Z","shell.execute_reply":"2021-11-18T19:30:54.625402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Переформатирование данных из вектора в формат 2-мерных изображений","metadata":{}},{"cell_type":"code","source":"train_x = np.array(train_x).reshape(train_x.shape[0],28,28,1)/255\ntest_x = np.array(test_x).reshape(test_x.shape[0],28,28,1)/255","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.627725Z","iopub.execute_input":"2021-11-18T19:30:54.628181Z","iopub.status.idle":"2021-11-18T19:30:54.878306Z","shell.execute_reply.started":"2021-11-18T19:30:54.628135Z","shell.execute_reply":"2021-11-18T19:30:54.877465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Построение модели","metadata":{}},{"cell_type":"code","source":"model = Sequential([layers.Flatten(input_shape=(28,28)),\n                    layers.Dense(128, activation=\"relu\"),\n                    layers.Dense(10) \n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.880423Z","iopub.execute_input":"2021-11-18T19:30:54.880752Z","iopub.status.idle":"2021-11-18T19:30:54.961115Z","shell.execute_reply.started":"2021-11-18T19:30:54.880721Z","shell.execute_reply":"2021-11-18T19:30:54.960310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Настройка модели для обучения","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam', \n                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), \n                  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.963216Z","iopub.execute_input":"2021-11-18T19:30:54.963554Z","iopub.status.idle":"2021-11-18T19:30:54.980076Z","shell.execute_reply.started":"2021-11-18T19:30:54.963510Z","shell.execute_reply":"2021-11-18T19:30:54.979279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Обучение модели","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_x, train_y,\n                     epochs=20,\n                     verbose=True,\n                     validation_data=(test_x, test_y),\n                     batch_size=10)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:30:54.981580Z","iopub.execute_input":"2021-11-18T19:30:54.981860Z","iopub.status.idle":"2021-11-18T19:33:28.844967Z","shell.execute_reply.started":"2021-11-18T19:30:54.981814Z","shell.execute_reply":"2021-11-18T19:33:28.844141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:33:28.846285Z","iopub.execute_input":"2021-11-18T19:33:28.846533Z","iopub.status.idle":"2021-11-18T19:33:28.853899Z","shell.execute_reply.started":"2021-11-18T19:33:28.846503Z","shell.execute_reply":"2021-11-18T19:33:28.853048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Предобработка тестового набора данных","metadata":{}},{"cell_type":"code","source":"data_test = np.array(data_test).reshape(data_test.shape[0], 28, 28, 1)/255","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:33:28.855451Z","iopub.execute_input":"2021-11-18T19:33:28.855944Z","iopub.status.idle":"2021-11-18T19:33:29.001273Z","shell.execute_reply.started":"2021-11-18T19:33:28.855901Z","shell.execute_reply":"2021-11-18T19:33:29.000296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:33:29.002528Z","iopub.execute_input":"2021-11-18T19:33:29.002849Z","iopub.status.idle":"2021-11-18T19:33:29.008870Z","shell.execute_reply.started":"2021-11-18T19:33:29.002814Z","shell.execute_reply":"2021-11-18T19:33:29.007959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Распознавание рукописных цифр","metadata":{}},{"cell_type":"code","source":"predictions = np.argmax(model.predict(data_test), axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:33:29.010358Z","iopub.execute_input":"2021-11-18T19:33:29.011057Z","iopub.status.idle":"2021-11-18T19:33:30.251536Z","shell.execute_reply.started":"2021-11-18T19:33:29.011012Z","shell.execute_reply":"2021-11-18T19:33:30.250798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:33:30.253199Z","iopub.execute_input":"2021-11-18T19:33:30.253785Z","iopub.status.idle":"2021-11-18T19:33:30.276488Z","shell.execute_reply.started":"2021-11-18T19:33:30.253739Z","shell.execute_reply":"2021-11-18T19:33:30.275612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Label']  = predictions","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:33:30.277622Z","iopub.execute_input":"2021-11-18T19:33:30.277850Z","iopub.status.idle":"2021-11-18T19:33:30.282560Z","shell.execute_reply.started":"2021-11-18T19:33:30.277822Z","shell.execute_reply":"2021-11-18T19:33:30.281710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Запись результатов в выходной файл","metadata":{}},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-18T19:33:30.283728Z","iopub.execute_input":"2021-11-18T19:33:30.283955Z","iopub.status.idle":"2021-11-18T19:33:30.356386Z","shell.execute_reply.started":"2021-11-18T19:33:30.283926Z","shell.execute_reply":"2021-11-18T19:33:30.355604Z"},"trusted":true},"execution_count":null,"outputs":[]}]}