{"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 warnings\n# filter warnings\nwarnings.filterwarnings('ignore')\nimport seaborn as sns\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":"2022-03-13T11:01:50.836953Z","iopub.execute_input":"2022-03-13T11:01:50.837242Z","iopub.status.idle":"2022-03-13T11:01:50.855619Z","shell.execute_reply.started":"2022-03-13T11:01:50.837211Z","shell.execute_reply":"2022-03-13T11:01:50.854807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nprint(train.shape)\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:50.857243Z","iopub.execute_input":"2022-03-13T11:01:50.857716Z","iopub.status.idle":"2022-03-13T11:01:53.697579Z","shell.execute_reply.started":"2022-03-13T11:01:50.857683Z","shell.execute_reply":"2022-03-13T11:01:53.696901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(test.shape)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:53.698819Z","iopub.execute_input":"2022-03-13T11:01:53.699538Z","iopub.status.idle":"2022-03-13T11:01:55.455669Z","shell.execute_reply.started":"2022-03-13T11:01:53.6995Z","shell.execute_reply":"2022-03-13T11:01:55.454815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train=train['label']                          # gercek sayilarin yazdigi kolon\nX_train=train.drop(labels=['label'],axis=1)     # gercek sayilarin yazdigi kolon haricindekiler (pixeller)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:55.457028Z","iopub.execute_input":"2022-03-13T11:01:55.457945Z","iopub.status.idle":"2022-03-13T11:01:55.596277Z","shell.execute_reply.started":"2022-03-13T11:01:55.457892Z","shell.execute_reply":"2022-03-13T11:01:55.595207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\ng=sns.countplot(Y_train,palette='winter')\nplt.title('Num of Digit Clases')\nfor p in g.patches:\n    g.annotate(f'\\n{p.get_height()}', (p.get_x()+0.2, p.get_height()), ha='center', va='top', color='black', size=10)\nY_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:55.59864Z","iopub.execute_input":"2022-03-13T11:01:55.598985Z","iopub.status.idle":"2022-03-13T11:01:55.854518Z","shell.execute_reply.started":"2022-03-13T11:01:55.598953Z","shell.execute_reply":"2022-03-13T11:01:55.853345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg=X_train.iloc[0].to_numpy()    # arraya cevirdik gorsellestirmek icin\nimg=img.reshape((28,28))          # 28*28 belirtilmis onda gore datayi duzenledik\nplt.imshow(img,cmap='gray')       # siyyah beyaz oalrak gorsellestirdik\nplt.title(train.iloc[0,0])\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:55.855796Z","iopub.execute_input":"2022-03-13T11:01:55.856016Z","iopub.status.idle":"2022-03-13T11:01:55.920182Z","shell.execute_reply.started":"2022-03-13T11:01:55.855988Z","shell.execute_reply":"2022-03-13T11:01:55.919276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimg=X_train.iloc[5].to_numpy()    # arraya cevirdik gorsellestirmek icin\nimg=img.reshape((28,28))          # 28*28 belirtilmis onda gore datayi duzenledik\nplt.imshow(img,cmap='gray')       # siyyah beyaz oalrak gorsellestirdik\nplt.title(train.iloc[0,0])\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:55.922273Z","iopub.execute_input":"2022-03-13T11:01:55.923002Z","iopub.status.idle":"2022-03-13T11:01:56.027048Z","shell.execute_reply.started":"2022-03-13T11:01:55.922951Z","shell.execute_reply":"2022-03-13T11:01:56.026096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Normalize\nX_train=X_train/255.0       # rgb max 255 olabilir dolayisi ile pixel degerleri max 255 tir\ntext=test/255\nprint('X_train shape:',X_train.shape)\nprint('test shape:',test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.029204Z","iopub.execute_input":"2022-03-13T11:01:56.029945Z","iopub.status.idle":"2022-03-13T11:01:56.154965Z","shell.execute_reply.started":"2022-03-13T11:01:56.029891Z","shell.execute_reply":"2022-03-13T11:01:56.154128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshape yapalim keras 3 boyutlu veri okur\nX_train=X_train.values.reshape(X_train.shape[0],28,28,1)     # veya alt satirdaki gibi -1 koyularak yapilabilir\ntest=test.values.reshape(-1,28,28,1)\nprint('x_Train Shape',X_train.shape)\nprint('test Shape',test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.156016Z","iopub.execute_input":"2022-03-13T11:01:56.156376Z","iopub.status.idle":"2022-03-13T11:01:56.164118Z","shell.execute_reply.started":"2022-03-13T11:01:56.1563Z","shell.execute_reply":"2022-03-13T11:01:56.163048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Label Encoding\nfrom keras.utils.np_utils import to_categorical # one hot encoding icin\nY_train=to_categorical(Y_train,num_classes=10)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.165656Z","iopub.execute_input":"2022-03-13T11:01:56.166064Z","iopub.status.idle":"2022-03-13T11:01:56.17828Z","shell.execute_reply.started":"2022-03-13T11:01:56.166026Z","shell.execute_reply":"2022-03-13T11:01:56.177545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_val,Y_train,Y_val=train_test_split(X_train,Y_train,test_size=0.1,random_state=2)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_test shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_test shape\",Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.179839Z","iopub.execute_input":"2022-03-13T11:01:56.180462Z","iopub.status.idle":"2022-03-13T11:01:56.606239Z","shell.execute_reply.started":"2022-03-13T11:01:56.180418Z","shell.execute_reply":"2022-03-13T11:01:56.605409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN","metadata":{}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools\nfrom keras.utils.np_utils import to_categorical     # convert one-hot encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPool2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.optimizers import adam_v2","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.607784Z","iopub.execute_input":"2022-03-13T11:01:56.608268Z","iopub.status.idle":"2022-03-13T11:01:56.615092Z","shell.execute_reply.started":"2022-03-13T11:01:56.608221Z","shell.execute_reply":"2022-03-13T11:01:56.614249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(filters=20, kernel_size=(5,5),padding=\"Same\",activation='relu',input_shape=(28,28,1)))\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n#\nmodel.add(Conv2D(filters=40,kernel_size=(3,3),padding='Same',activation='relu'))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Dropout(0.25))\n#fully conected\nmodel.add(Flatten())\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.616779Z","iopub.execute_input":"2022-03-13T11:01:56.617055Z","iopub.status.idle":"2022-03-13T11:01:56.69658Z","shell.execute_reply.started":"2022-03-13T11:01:56.617019Z","shell.execute_reply":"2022-03-13T11:01:56.695745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#define optimizer\n#optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.699327Z","iopub.execute_input":"2022-03-13T11:01:56.699655Z","iopub.status.idle":"2022-03-13T11:01:56.704239Z","shell.execute_reply.started":"2022-03-13T11:01:56.699621Z","shell.execute_reply":"2022-03-13T11:01:56.702809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#compile\nmodel.compile(optimizer = 'adam' , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.705639Z","iopub.execute_input":"2022-03-13T11:01:56.706565Z","iopub.status.idle":"2022-03-13T11:01:56.723171Z","shell.execute_reply.started":"2022-03-13T11:01:56.706526Z","shell.execute_reply":"2022-03-13T11:01:56.722227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20  # tekrar sayisi \nbatch_size = 250","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.724315Z","iopub.execute_input":"2022-03-13T11:01:56.724571Z","iopub.status.idle":"2022-03-13T11:01:56.734699Z","shell.execute_reply.started":"2022-03-13T11:01:56.72454Z","shell.execute_reply":"2022-03-13T11:01:56.734049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # dimesion reduction\n        rotation_range=5,  # randomly rotate images in the range 5 degrees\n        zoom_range = 0.1, # Randomly zoom image 10%\n        width_shift_range=0.1,  # randomly shift images horizontally 10%\n        height_shift_range=0.1,  # randomly shift images vertically 10%\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.735774Z","iopub.execute_input":"2022-03-13T11:01:56.736459Z","iopub.status.idle":"2022-03-13T11:01:56.844691Z","shell.execute_reply.started":"2022-03-13T11:01:56.736425Z","shell.execute_reply":"2022-03-13T11:01:56.843688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),\n                              epochs = epochs, validation_data = (X_val,Y_val), steps_per_epoch=X_train.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:01:56.845918Z","iopub.execute_input":"2022-03-13T11:01:56.846132Z","iopub.status.idle":"2022-03-13T11:07:45.648686Z","shell.execute_reply.started":"2022-03-13T11:01:56.846106Z","shell.execute_reply":"2022-03-13T11:07:45.647786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['val_loss'], color='b', label=\"validation loss\")\nplt.title(\"Test Loss\")\nplt.xlabel(\"Number of Epochs\")\nplt.ylabel(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:07:45.650151Z","iopub.execute_input":"2022-03-13T11:07:45.650405Z","iopub.status.idle":"2022-03-13T11:07:45.867184Z","shell.execute_reply.started":"2022-03-13T11:07:45.650373Z","shell.execute_reply":"2022-03-13T11:07:45.866362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#overfitting oldugunu gorduk epoch 5 uygundur\n\nhistory = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),\n                              epochs = 5, validation_data = (X_val,Y_val), steps_per_epoch=X_train.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:07:45.868381Z","iopub.execute_input":"2022-03-13T11:07:45.868702Z","iopub.status.idle":"2022-03-13T11:09:14.241235Z","shell.execute_reply.started":"2022-03-13T11:07:45.868668Z","shell.execute_reply":"2022-03-13T11:09:14.24001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix\nimport seaborn as sns\n# Predict the values from the validation dataset\nY_pred = model.predict(X_val)\n# Convert predictions classes to one hot vectors \nY_pred_classes = np.argmax(Y_pred,axis = 1) \n# Convert validation observations to one hot vectors\nY_true = np.argmax(Y_val,axis = 1) \n# compute the confusion matrix\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes) \n# plot the confusion matrix\nf,ax = plt.subplots(figsize=(8, 8))\nsns.heatmap(confusion_mtx, annot=True, linewidths=0.01,cmap=\"Greens\",linecolor=\"gray\", fmt= '.1f',ax=ax)\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()\n\n#overfitting","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:09:14.243151Z","iopub.execute_input":"2022-03-13T11:09:14.243448Z","iopub.status.idle":"2022-03-13T11:09:15.4843Z","shell.execute_reply.started":"2022-03-13T11:09:14.243404Z","shell.execute_reply":"2022-03-13T11:09:15.483727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test datasindan cikti alalim\nresults = model.predict(test)\n\n# en uygun degeri secelim\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:09:15.485538Z","iopub.execute_input":"2022-03-13T11:09:15.486213Z","iopub.status.idle":"2022-03-13T11:09:19.311256Z","shell.execute_reply.started":"2022-03-13T11:09:15.48618Z","shell.execute_reply":"2022-03-13T11:09:19.310594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-13T11:09:19.312756Z","iopub.execute_input":"2022-03-13T11:09:19.313231Z","iopub.status.idle":"2022-03-13T11:09:19.365989Z","shell.execute_reply.started":"2022-03-13T11:09:19.313199Z","shell.execute_reply":"2022-03-13T11:09:19.364896Z"},"trusted":true},"execution_count":null,"outputs":[]}]}