{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":3004,"databundleVersionId":861823}],"dockerImageVersionId":31328,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras import layers, models\n\n# 1. Определение пути к данным \ndef find_data_path():\n    if os.path.exists('/kaggle/input/digit-recognizer/'):\n        return '/kaggle/input/digit-recognizer/'\n    for root, dirs, files in os.walk('/kaggle/input'):\n        if 'train.csv' in files:\n            return root + '/'\n\nDATA_PATH = find_data_path()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-03-25T07:59:09.867744Z","iopub.execute_input":"2026-03-25T07:59:09.868103Z","iopub.status.idle":"2026-03-25T07:59:09.878448Z","shell.execute_reply.started":"2026-03-25T07:59:09.868068Z","shell.execute_reply":"2026-03-25T07:59:09.877485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 2. Загрузка данных \ntrain_df = pd.read_csv(f\"{DATA_PATH}train.csv\")\ntest_df  = pd.read_csv(f\"{DATA_PATH}test.csv\")\n\nX_train_raw = train_df.drop(\"label\", axis=1).values   # признаки (784 пикселя)\ny_train = train_df[\"label\"].values                    # метки (0..9)\nX_test_raw = test_df.values                           # тестовые признаки","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T07:59:09.880570Z","iopub.execute_input":"2026-03-25T07:59:09.880911Z","iopub.status.idle":"2026-03-25T07:59:14.028057Z","shell.execute_reply.started":"2026-03-25T07:59:09.880882Z","shell.execute_reply":"2026-03-25T07:59:14.027163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 3. Предобработка \nIMG_SIZE = 28\nX_train = X_train_raw.reshape(-1, IMG_SIZE, IMG_SIZE, 1) / 255.0\nX_test  = X_test_raw.reshape(-1, IMG_SIZE, IMG_SIZE, 1) / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T07:59:14.029533Z","iopub.execute_input":"2026-03-25T07:59:14.029939Z","iopub.status.idle":"2026-03-25T07:59:14.261034Z","shell.execute_reply.started":"2026-03-25T07:59:14.029893Z","shell.execute_reply":"2026-03-25T07:59:14.260235Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 4. Построение модели \nmodel = models.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D((2, 2)),\n\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.BatchNormalization(),\n    layers.MaxPooling2D((2, 2)),\n\n    layers.Flatten(),\n    layers.Dense(128, activation='relu'),\n    layers.Dropout(0.3),\n    layers.Dense(10, activation='softmax')\n])\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T07:59:14.262208Z","iopub.execute_input":"2026-03-25T07:59:14.262539Z","iopub.status.idle":"2026-03-25T07:59:14.407972Z","shell.execute_reply.started":"2026-03-25T07:59:14.262510Z","shell.execute_reply":"2026-03-25T07:59:14.406900Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5. Обучение \nprint(\"Начало обучения...\")\nhistory = model.fit(X_train, y_train,\n                    epochs=10,\n                    batch_size=64,\n                    validation_split=0.1,\n                    verbose=1)   ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T07:59:14.410096Z","iopub.execute_input":"2026-03-25T07:59:14.410542Z","iopub.status.idle":"2026-03-25T08:03:28.386846Z","shell.execute_reply.started":"2026-03-25T07:59:14.410507Z","shell.execute_reply":"2026-03-25T08:03:28.385709Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 6. Предсказание и сохранение \npred_probs = model.predict(X_test)\npred_labels = np.argmax(pred_probs, axis=1)\n\nsubmission = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(pred_labels) + 1),\n    \"Label\": pred_labels\n})\nsubmission.to_csv('submission.csv', index=False)\n\nprint(\"Готово! Файл 'submission.csv' сохранён.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-03-25T08:03:28.388485Z","iopub.execute_input":"2026-03-25T08:03:28.388917Z","iopub.status.idle":"2026-03-25T08:03:34.896457Z","shell.execute_reply.started":"2026-03-25T08:03:28.388869Z","shell.execute_reply":"2026-03-25T08:03:34.895536Z"}},"outputs":[],"execution_count":null}]}