{"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":"2023-03-19T09:50:18.431033Z","iopub.execute_input":"2023-03-19T09:50:18.431514Z","iopub.status.idle":"2023-03-19T09:50:18.442594Z","shell.execute_reply.started":"2023-03-19T09:50:18.431468Z","shell.execute_reply":"2023-03-19T09:50:18.441308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\n# Импортируем библиотеки:\n\n# обработка изобрадений\nimport cv2\nimport numpy as np\n\n# ыизуализация изображений и гистограмм\nimport matplotlib.pyplot as plt\n\n# для отобрадения классов\nimport pandas as pd\n\n# для разбиения выборки\nfrom sklearn.model_selection import train_test_split\n\n# для модели линейной регресси \nfrom sklearn.linear_model import LogisticRegression\n\n# для модели деревьев решений\nfrom sklearn.ensemble import RandomForestClassifier\n\n# для модели нейронной сети\nimport tensorflow as tf\nfrom keras.models  import Sequential\nfrom keras.layers import Dense\nimport tensorflow.keras  as  keras\n\n\n# для оценки качества моделей\nfrom sklearn.metrics import accuracy_score ","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:18.445283Z","iopub.execute_input":"2023-03-19T09:50:18.445733Z","iopub.status.idle":"2023-03-19T09:50:18.459033Z","shell.execute_reply.started":"2023-03-19T09:50:18.445696Z","shell.execute_reply":"2023-03-19T09:50:18.457977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Загружаем данные датасет\n# Будем использовать только датасет train так как test не размечен\ntrain = np.loadtxt('/kaggle/input/digit-recognizer/train.csv', delimiter=',', skiprows=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:18.460996Z","iopub.execute_input":"2023-03-19T09:50:18.461421Z","iopub.status.idle":"2023-03-19T09:50:36.641010Z","shell.execute_reply.started":"2023-03-19T09:50:18.461367Z","shell.execute_reply":"2023-03-19T09:50:36.639926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:55:55.198393Z","iopub.execute_input":"2023-03-19T09:55:55.198798Z","iopub.status.idle":"2023-03-19T09:55:55.205767Z","shell.execute_reply.started":"2023-03-19T09:55:55.198763Z","shell.execute_reply":"2023-03-19T09:55:55.204668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# числовые значения изображение в переменную:\ntrain_label = train[:, 0]\n# приводим размерность к удобному для обаботки виду:\ntrain_img = np.resize(train[:, 1:], (train.shape[0], 28, 28))","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:36.642779Z","iopub.execute_input":"2023-03-19T09:50:36.643171Z","iopub.status.idle":"2023-03-19T09:50:36.820020Z","shell.execute_reply.started":"2023-03-19T09:50:36.643127Z","shell.execute_reply":"2023-03-19T09:50:36.818905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# проверим насколько выборка сбалансированная по каждому классу\ndf = pd.DataFrame(data=train_label)\ndf['x'] = 1\ndf.columns = ['img', 'num']\ndf.groupby('img').count()\n# выборка сбалансорованная и параметр accuracy можно корректно использовать для оцкенки качесвта модели","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:36.822347Z","iopub.execute_input":"2023-03-19T09:50:36.823895Z","iopub.status.idle":"2023-03-19T09:50:36.844101Z","shell.execute_reply.started":"2023-03-19T09:50:36.823854Z","shell.execute_reply":"2023-03-19T09:50:36.842992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# отобразим первые 10 изображений с истинными значениями в описании:\nfig = plt.figure(figsize=(10, 5))\nfor i, img in enumerate(train_img[0:10], 1):\n    subplot = fig.add_subplot(2, 5, i)\n    plt.imshow(img, cmap='gray');\n    subplot.set_title('%s' % train_label[i - 1]);","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:36.845908Z","iopub.execute_input":"2023-03-19T09:50:36.846260Z","iopub.status.idle":"2023-03-19T09:50:37.729162Z","shell.execute_reply.started":"2023-03-19T09:50:36.846226Z","shell.execute_reply":"2023-03-19T09:50:37.726880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Вычистляем градиент для учебной и тестовой выборки\ntrain_sobel_x = np.zeros_like(train_img)\ntrain_sobel_y = np.zeros_like(train_img)\nfor i in range(len(train_img)):\n    train_sobel_x[i] = cv2.Sobel(train_img[i], cv2.CV_64F, dx=1, dy=0, ksize=3)\n    train_sobel_y[i] = cv2.Sobel(train_img[i], cv2.CV_64F, dx=0, dy=1, ksize=3)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:37.730832Z","iopub.execute_input":"2023-03-19T09:50:37.731202Z","iopub.status.idle":"2023-03-19T09:50:38.535271Z","shell.execute_reply.started":"2023-03-19T09:50:37.731164Z","shell.execute_reply":"2023-03-19T09:50:38.534154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Вычисляем длину и угол градиента\ntrain_g, train_theta = cv2.cartToPolar(train_sobel_x, train_sobel_y)\n\n# Отобразим изображения и соответствующие гистограммы градиентов\nfig = plt.figure(figsize=(20, 10))\nfor i, img in enumerate(train_g[:7], 1 ):\n    subplot = fig.add_subplot(1, 7, i)\n    plt.imshow(img, cmap='gray');\n    subplot.set_title('%s' % train_label[i - 1]);\n    subplot = fig.add_subplot(3, 7, i)\n    plt.hist(train_theta[i - 1].flatten(),\n             bins=8, weights=train_g[i - 1].flatten())","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:38.537154Z","iopub.execute_input":"2023-03-19T09:50:38.537567Z","iopub.status.idle":"2023-03-19T09:50:40.234056Z","shell.execute_reply.started":"2023-03-19T09:50:38.537528Z","shell.execute_reply":"2023-03-19T09:50:40.232901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Вычисляем гистограммы\n# колво признаков\nn_fearures = 128\ntrain_hist = np.zeros((len(train_img), n_fearures))\nfor i in range(len(train_img)):\n    hist, borders = np.histogram(train_theta[i],\n                                 bins=n_fearures,\n                                 range=(0., 2. * np.pi),\n                                 weights=train_g[i])\n    train_hist[i] = hist\n\n\ntrain_hist = train_hist / np.linalg.norm(train_hist, axis=1)[:, None]\n\n# нормируем вектора гистограмм:\ntrain_hist = train_hist / np.linalg.norm(train_hist, axis=1)[:, None]\n\n# Т.о. мы сформировали признаки изображений на основе найденных гистограмм градиентов (HOG)\n# Признаками являются значения гистограм","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:40.236111Z","iopub.execute_input":"2023-03-19T09:50:40.236490Z","iopub.status.idle":"2023-03-19T09:50:45.701456Z","shell.execute_reply.started":"2023-03-19T09:50:40.236452Z","shell.execute_reply":"2023-03-19T09:50:45.700231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Сформируем тренироволчную и тестовую выбокри \ny_train, y_test, x_train, x_test = train_test_split(\n    train_label, train_hist, test_size=0.25,\n    random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:45.703184Z","iopub.execute_input":"2023-03-19T09:50:45.703602Z","iopub.status.idle":"2023-03-19T09:50:45.725638Z","shell.execute_reply.started":"2023-03-19T09:50:45.703561Z","shell.execute_reply":"2023-03-19T09:50:45.724484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# будем строить 3 модели:\n# линуйную, дреревья решение и полносвязную нейронную сеть\n\n# ЛИНЕЙНАЯ МОДЕЛЬ\nlg = LogisticRegression()\nlg.fit(x_train, y_train)\nlg_accuracy= accuracy_score(y_test, lg.predict(x_test))\n\n# ДЕРЕВЬЯ\nrfc = RandomForestClassifier()\nrfc.fit(x_train, y_train)\nrfc_accuracy= accuracy_score(y_test, rfc.predict(x_test))\n\n\n# НЕЙРОННАЯ СЕТЬ\n# выделим из тестовой валидационную выборку для избежания переобучения\ny_train_seq, y_val_seq, x_train_seq, x_val_seq = train_test_split(\n    y_train, x_train, test_size=0.2,\n    random_state=42)\n\n# в данный момент y  предсатвлено числом, переведем его в вектор (фактически это one hot encoding)\ny_train_labels = tf.keras.utils.to_categorical(y_train_seq, 10)\ny_val_labels = tf.keras.utils.to_categorical(y_val_seq, 10)\ny_test_labels = tf.keras.utils.to_categorical(y_test,10)\n\n\n# создаем модель с 1м скрытым слоем\nseq_model = Sequential()\nseq_model.add(Dense(256, input_dim=n_fearures, activation='relu'))\nseq_model.add(Dense(10, activation='softmax'))\nseq_model.summary()\n\n# компилируем модель с оптимизатором, функ потерь и метрикой \nseq_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\n# зададим метод останова по val_acc\nearly_stopping_callback = keras.callbacks.EarlyStopping(monitor='val_accuracy', \n                                        patience=2)\n\n\n# обучаем модель\nseq_model.fit(\n    x_train_seq,\n    y_train_labels,\n    batch_size=64,\n    epochs=10,\n    verbose=1,\n    validation_data=(x_val_seq, y_val_labels),\n    callbacks=early_stopping_callback\n    )\n\ny_pred = seq_model.predict(x_test)\ny_pred_labels = np.argmax(y_pred, axis=1)\nseq_accuracy= accuracy_score(y_test, y_pred_labels)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:50:45.727703Z","iopub.execute_input":"2023-03-19T09:50:45.728147Z","iopub.status.idle":"2023-03-19T09:51:40.460916Z","shell.execute_reply.started":"2023-03-19T09:50:45.728104Z","shell.execute_reply":"2023-03-19T09:51:40.459854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Точность модели логистическая ргрессия:', round(lg_accuracy*100), '%')\nprint('Точность модели слйчайный лес:', round(rfc_accuracy*100), '%')\nprint('Точность модели полносвязняй н.сеть:', round(seq_accuracy*100), '%')","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:51:40.462323Z","iopub.execute_input":"2023-03-19T09:51:40.462698Z","iopub.status.idle":"2023-03-19T09:51:40.469955Z","shell.execute_reply.started":"2023-03-19T09:51:40.462660Z","shell.execute_reply":"2023-03-19T09:51:40.468765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = np.loadtxt('/kaggle/input/digit-recognizer/test.csv', delimiter=',', skiprows=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:52:36.338673Z","iopub.execute_input":"2023-03-19T09:52:36.341193Z","iopub.status.idle":"2023-03-19T09:52:48.225105Z","shell.execute_reply.started":"2023-03-19T09:52:36.341143Z","shell.execute_reply":"2023-03-19T09:52:48.223910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:54:45.300670Z","iopub.execute_input":"2023-03-19T09:54:45.301596Z","iopub.status.idle":"2023-03-19T09:54:45.309004Z","shell.execute_reply.started":"2023-03-19T09:54:45.301543Z","shell.execute_reply":"2023-03-19T09:54:45.307790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-19T09:55:04.694061Z","iopub.execute_input":"2023-03-19T09:55:04.694690Z","iopub.status.idle":"2023-03-19T09:55:04.701111Z","shell.execute_reply.started":"2023-03-19T09:55:04.694614Z","shell.execute_reply":"2023-03-19T09:55:04.700037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img = np.resize(test, (test.shape[0], 28, 28))","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:00:31.699300Z","iopub.execute_input":"2023-03-19T10:00:31.700315Z","iopub.status.idle":"2023-03-19T10:00:31.768728Z","shell.execute_reply.started":"2023-03-19T10:00:31.700266Z","shell.execute_reply":"2023-03-19T10:00:31.767593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_img.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:00:34.437887Z","iopub.execute_input":"2023-03-19T10:00:34.438467Z","iopub.status.idle":"2023-03-19T10:00:34.445097Z","shell.execute_reply.started":"2023-03-19T10:00:34.438430Z","shell.execute_reply":"2023-03-19T10:00:34.444029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# отобразим первые 10 изображений :\nfig = plt.figure(figsize=(10, 5))\nfor i, img in enumerate(test_img[0:10], 1):\n    subplot = fig.add_subplot(2, 5, i)\n    plt.imshow(img, cmap='gray');\n    ","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:00:37.004520Z","iopub.execute_input":"2023-03-19T10:00:37.005218Z","iopub.status.idle":"2023-03-19T10:00:37.965688Z","shell.execute_reply.started":"2023-03-19T10:00:37.005179Z","shell.execute_reply":"2023-03-19T10:00:37.964611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Вычистляем градиент для учебной и тестовой выборки\ntest_sobel_x = np.zeros_like(test_img)\ntest_sobel_y = np.zeros_like(test_img)\nfor i in range(len(test_img)):\n    test_sobel_x[i] = cv2.Sobel(test_img[i], cv2.CV_64F, dx=1, dy=0, ksize=3)\n    test_sobel_y[i] = cv2.Sobel(test_img[i], cv2.CV_64F, dx=0, dy=1, ksize=3)\n    \n# Вычисляем длину и угол градиента\ntest_g, test_theta = cv2.cartToPolar(test_sobel_x, test_sobel_y)\n\n# Отобразим изображения и соответствующие гистограммы градиентов\nfig = plt.figure(figsize=(20, 10))\nfor i, img in enumerate(test_g[:7], 1 ):\n    subplot = fig.add_subplot(1, 7, i)\n    plt.imshow(img, cmap='gray');\n    subplot = fig.add_subplot(3, 7, i)\n    plt.hist(test_theta[i - 1].flatten(),\n             bins=8, weights=test_g[i - 1].flatten())    ","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:01:30.194764Z","iopub.execute_input":"2023-03-19T10:01:30.195734Z","iopub.status.idle":"2023-03-19T10:01:32.446799Z","shell.execute_reply.started":"2023-03-19T10:01:30.195677Z","shell.execute_reply":"2023-03-19T10:01:32.445693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Вычисляем гистограммы\n# колво признаков\nn_fearures = 128\ntest_hist = np.zeros((len(test_img), n_fearures))\nfor i in range(len(test_img)):\n    hist, borders = np.histogram(test_theta[i],\n                                 bins=n_fearures,\n                                 range=(0., 2. * np.pi),\n                                 weights=test_g[i])\n    test_hist[i] = hist\n\n\ntest_hist = test_hist / np.linalg.norm(test_hist, axis=1)[:, None]\n\n# нормируем вектора гистограмм:\ntest_hist = test_hist / np.linalg.norm(test_hist, axis=1)[:, None]\n\n# Т.о. мы сформировали признаки изображений на основе найденных гистограмм градиентов (HOG)\n# Признаками являются значения гистограм","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:04:41.024460Z","iopub.execute_input":"2023-03-19T10:04:41.024835Z","iopub.status.idle":"2023-03-19T10:04:44.385972Z","shell.execute_reply.started":"2023-03-19T10:04:41.024803Z","shell.execute_reply":"2023-03-19T10:04:44.384914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_hist.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:04:51.641085Z","iopub.execute_input":"2023-03-19T10:04:51.641756Z","iopub.status.idle":"2023-03-19T10:04:51.648140Z","shell.execute_reply.started":"2023-03-19T10:04:51.641718Z","shell.execute_reply":"2023-03-19T10:04:51.647122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = seq_model.predict(test_hist)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:05:16.653061Z","iopub.execute_input":"2023-03-19T10:05:16.654093Z","iopub.status.idle":"2023-03-19T10:05:19.277265Z","shell.execute_reply.started":"2023-03-19T10:05:16.654041Z","shell.execute_reply":"2023-03-19T10:05:19.276177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_pred = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:37:48.589325Z","iopub.execute_input":"2023-03-19T10:37:48.590017Z","iopub.status.idle":"2023-03-19T10:37:48.595493Z","shell.execute_reply.started":"2023-03-19T10:37:48.589979Z","shell.execute_reply":"2023-03-19T10:37:48.594183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# отобразим первые 10 изображений :\nfig = plt.figure(figsize=(10, 5))\nfor i, img in enumerate(test_img[0:10], 1):\n    subplot = fig.add_subplot(2, 5, i)\n    plt.imshow(img, cmap='gray');\n    subplot.set_title('%s' % test_pred[i - 1]);","metadata":{"execution":{"iopub.status.busy":"2023-03-19T10:38:30.248725Z","iopub.execute_input":"2023-03-19T10:38:30.249087Z","iopub.status.idle":"2023-03-19T10:38:31.720319Z","shell.execute_reply.started":"2023-03-19T10:38:30.249055Z","shell.execute_reply":"2023-03-19T10:38:31.718126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(\n    {'imageID': list(range(1, len(test_pred)+1)),\n     'Label': test_pred\n    })","metadata":{"execution":{"iopub.status.busy":"2023-03-19T11:02:44.529072Z","iopub.execute_input":"2023-03-19T11:02:44.530138Z","iopub.status.idle":"2023-03-19T11:02:44.543368Z","shell.execute_reply.started":"2023-03-19T11:02:44.530083Z","shell.execute_reply":"2023-03-19T11:02:44.542237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-03-19T11:02:47.718770Z","iopub.execute_input":"2023-03-19T11:02:47.719127Z","iopub.status.idle":"2023-03-19T11:02:47.731750Z","shell.execute_reply.started":"2023-03-19T11:02:47.719094Z","shell.execute_reply":"2023-03-19T11:02:47.729577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"my_submission1.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-19T11:03:10.469860Z","iopub.execute_input":"2023-03-19T11:03:10.470454Z","iopub.status.idle":"2023-03-19T11:03:10.508898Z","shell.execute_reply.started":"2023-03-19T11:03:10.470410Z","shell.execute_reply":"2023-03-19T11:03:10.507747Z"},"trusted":true},"execution_count":null,"outputs":[]}]}