{"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)\nimport matplotlib.pyplot as plt\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-18T18:14:28.861685Z","iopub.execute_input":"2021-07-18T18:14:28.862059Z","iopub.status.idle":"2021-07-18T18:14:28.873661Z","shell.execute_reply.started":"2021-07-18T18:14:28.862029Z","shell.execute_reply":"2021-07-18T18:14:28.872485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Давайте считаем данные с csv ","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")\nsubm = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:14:28.877936Z","iopub.execute_input":"2021-07-18T18:14:28.87825Z","iopub.status.idle":"2021-07-18T18:14:33.448161Z","shell.execute_reply.started":"2021-07-18T18:14:28.878222Z","shell.execute_reply":"2021-07-18T18:14:33.447163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Запишем их в переменные и проверим размерности","metadata":{}},{"cell_type":"code","source":"train_x = train.drop([\"label\"], axis = 1)\ntrain_y = train[\"label\"]\n\ntest_x = test\ntest_y = subm.iloc[:,1]\n\nprint(train_x.shape)\nprint(train_y.shape)\nprint(test_x.shape)\nprint(test_y.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:14:33.4495Z","iopub.execute_input":"2021-07-18T18:14:33.449823Z","iopub.status.idle":"2021-07-18T18:14:33.564728Z","shell.execute_reply.started":"2021-07-18T18:14:33.449795Z","shell.execute_reply":"2021-07-18T18:14:33.563711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Выведем несколько фото, что бы посмотреть данные","metadata":{}},{"cell_type":"code","source":"for_photo = np.array(train_x).reshape((-1,28,28))\nfor_photo1 = np.array(test_x).reshape((-1,28,28))\n\ncols = 8\nrow = 2\n\nplt.imshow(for_photo1[25000,:])\nprint(test_y[25000])\n\nfigi = plt.figure(figsize=(row*20,cols))\n\nfor i in range(cols):\n    for j in range(row):\n        photo = i+j\n        ax = figi.add_subplot(row, cols, i*row + j + 1)\n        ax.axis('off')\n        ax.grid('off')\n        ax.imshow((for_photo/255)[photo, :])\n        ax.set_title([train_y[photo]])","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:35.426302Z","iopub.execute_input":"2021-07-18T18:16:35.426641Z","iopub.status.idle":"2021-07-18T18:16:38.413606Z","shell.execute_reply.started":"2021-07-18T18:16:35.426613Z","shell.execute_reply":"2021-07-18T18:16:38.412654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Нормализируем данные","metadata":{}},{"cell_type":"code","source":"train_x_norm = train_x/255\ntest_x_norm = test_x/255","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:14:36.543649Z","iopub.execute_input":"2021-07-18T18:14:36.543938Z","iopub.status.idle":"2021-07-18T18:14:36.654505Z","shell.execute_reply.started":"2021-07-18T18:14:36.543911Z","shell.execute_reply":"2021-07-18T18:14:36.653772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Импортируем нужные библиотеки для обучения модели","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn import svm","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:14:36.655566Z","iopub.execute_input":"2021-07-18T18:14:36.65584Z","iopub.status.idle":"2021-07-18T18:14:37.009264Z","shell.execute_reply.started":"2021-07-18T18:14:36.655814Z","shell.execute_reply":"2021-07-18T18:14:37.008364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Заполним и обучим модель","metadata":{}},{"cell_type":"code","source":"model = svm.SVC(gamma = 0.001, C=100).fit(train_x_norm, train_y)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:14:37.010413Z","iopub.execute_input":"2021-07-18T18:14:37.010678Z","iopub.status.idle":"2021-07-18T18:16:26.424069Z","shell.execute_reply.started":"2021-07-18T18:14:37.010652Z","shell.execute_reply":"2021-07-18T18:16:26.421096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Посмотрим, как хорошо модель предугадывает значения на тестовых данных","metadata":{}},{"cell_type":"code","source":"\npred = model.predict(test_x_norm)\n    \n    \nfigk = plt.figure(figsize=(row*20,cols))\n\nfor i in range(20):\n    photo = i\n    ax = figk.add_subplot(1, 20, i+1)\n    ax.axis('off')\n    ax.grid('off')\n    ax.imshow((for_photo1/255)[photo, :])\n    ax.set_title([pred[photo]])\n","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.424976Z","iopub.status.idle":"2021-07-18T18:16:26.425401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Формируем ответ","metadata":{}},{"cell_type":"code","source":"imageid = list(range(1,28001))\nsubmission = pd.DataFrame({\"ImageId\": imageid, \"Label\": pred})\nsubmission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.426841Z","iopub.status.idle":"2021-07-18T18:16:26.427484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Смотрим, нормально ли сформировался файл","metadata":{}},{"cell_type":"code","source":"figk = plt.figure(figsize=(row*20,cols))\nfor i in range(20):\n    photo = i\n    ax = figk.add_subplot(1, 20, i+1)\n    ax.axis('off')\n    ax.grid('off')\n    ax.imshow((for_photo1/255)[photo, :])\n    ax.set_title([pred[photo]])","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.428892Z","iopub.status.idle":"2021-07-18T18:16:26.429528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = pd.read_csv(\"submission.csv\")\ns[:20]","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.430817Z","iopub.status.idle":"2021-07-18T18:16:26.431444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.datasets import mnist","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:28:05.311071Z","iopub.execute_input":"2021-07-18T18:28:05.311638Z","iopub.status.idle":"2021-07-18T18:28:05.316431Z","shell.execute_reply.started":"2021-07-18T18:28:05.31159Z","shell.execute_reply":"2021-07-18T18:28:05.31539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train, y_train), (X_test, y_test) = mnist.load_data()\nprint(X_train.reshape(-1,28*28))\nprint(y_train.shape)\nX_train = X_train.reshape(-1,28*28)/255\nX_test = X_test.reshape(-1,28*28)/255\nX_train = np.vstack((X_train, X_test))\nX_train = np.vstack((X_train, train_x_norm))\n\ny_train = np.hstack((y_train, y_test))\ny_train = np.hstack((y_train, train_y))\nprint(X_train.shape)\nprint(y_train.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:28:24.229643Z","iopub.execute_input":"2021-07-18T18:28:24.230226Z","iopub.status.idle":"2021-07-18T18:28:25.27413Z","shell.execute_reply.started":"2021-07-18T18:28:24.23019Z","shell.execute_reply":"2021-07-18T18:28:25.273037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for_photo = np.array(X_train).reshape((-1,28,28))\nfor_photo1 = np.array(X_train).reshape((-1,28,28))\n\ncols = 8\nrow = 2\nnum = 111020\nplt.imshow(for_photo1[num,:])\nprint(y_train[num])\n\nfigi = plt.figure(figsize=(row*20,cols))\n\nfor i in range(cols):\n    for j in range(row):\n        photo = i+j\n        ax = figi.add_subplot(row, cols, i*row + j + 1)\n        ax.axis('off')\n        ax.grid('off')\n        ax.imshow((for_photo/255)[photo, :])\n        ax.set_title([y_train[photo]])","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:29:25.803283Z","iopub.execute_input":"2021-07-18T18:29:25.803664Z","iopub.status.idle":"2021-07-18T18:29:31.115756Z","shell.execute_reply.started":"2021-07-18T18:29:25.803629Z","shell.execute_reply":"2021-07-18T18:29:31.114768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model1 = svm.SVC(gamma = 0.001, C=100).fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.436006Z","iopub.status.idle":"2021-07-18T18:16:26.436401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model1.predict(test_x_norm)\n\nimageid = list(range(1,28001))\nsubmission = pd.DataFrame({\"ImageId\": imageid, \"Label\": pred})\nsubmission.to_csv(\"submission.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.437208Z","iopub.status.idle":"2021-07-18T18:16:26.4376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = 8\nrow = 2\nfor_photo1 = np.array(test_x).reshape((-1,28,28))\n\nfigk = plt.figure(figsize=(row*20,cols))\nfor i in range(20):\n    photo = i\n    ax = figk.add_subplot(1, 20, i+1)\n    ax.axis('off')\n    ax.grid('off')\n    ax.imshow((for_photo1/255)[photo, :])\n    ax.set_title([pred[photo]])","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.438457Z","iopub.status.idle":"2021-07-18T18:16:26.438923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\n\n# save\njoblib.dump(model1, \"model.pkl\")","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.43986Z","iopub.status.idle":"2021-07-18T18:16:26.440253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_x_norm.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-18T18:16:26.441034Z","iopub.status.idle":"2021-07-18T18:16:26.441421Z"},"trusted":true},"execution_count":null,"outputs":[]}]}