{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Digit Recognizer\nBu projede el yazısı rakamları tanıyan bir model geliştiriyoruz. Model verileri Kaggle yarışmalarından biri olan Digit Recognizer'den elde edilmiş ve kod yazımı için Kaggle ortamı kullanılmıştır. \n<br><br>\n<img src=\"https://www.onceokuloncesi.com/imagehosting/5049f61e2991049.jpg\">","metadata":{}},{"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":"2024-05-09T17:40:51.605567Z","iopub.execute_input":"2024-05-09T17:40:51.605911Z","iopub.status.idle":"2024-05-09T17:40:52.475706Z","shell.execute_reply.started":"2024-05-09T17:40:51.605883Z","shell.execute_reply":"2024-05-09T17:40:52.474809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:40:52.477975Z","iopub.execute_input":"2024-05-09T17:40:52.478786Z","iopub.status.idle":"2024-05-09T17:40:53.695066Z","shell.execute_reply.started":"2024-05-09T17:40:52.478751Z","shell.execute_reply":"2024-05-09T17:40:53.693594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:40:53.697037Z","iopub.execute_input":"2024-05-09T17:40:53.698459Z","iopub.status.idle":"2024-05-09T17:40:57.420342Z","shell.execute_reply.started":"2024-05-09T17:40:53.698411Z","shell.execute_reply":"2024-05-09T17:40:57.419058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"label\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:40:57.421849Z","iopub.execute_input":"2024-05-09T17:40:57.422198Z","iopub.status.idle":"2024-05-09T17:40:57.437951Z","shell.execute_reply.started":"2024-05-09T17:40:57.422154Z","shell.execute_reply":"2024-05-09T17:40:57.437080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_images = df.sample(20)\nselected_images = selected_images.reset_index(drop=True)\n\nfig, axes = plt.subplots(5, 4, figsize=(10, 10))\n\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(selected_images.iloc[i, 1:].values.reshape(28, 28), cmap='gray')\n    ax.axis(\"off\")\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:40:57.440548Z","iopub.execute_input":"2024-05-09T17:40:57.440847Z","iopub.status.idle":"2024-05-09T17:40:58.292179Z","shell.execute_reply.started":"2024-05-09T17:40:57.440822Z","shell.execute_reply":"2024-05-09T17:40:58.291303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Modelleme","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:40:58.293556Z","iopub.execute_input":"2024-05-09T17:40:58.293923Z","iopub.status.idle":"2024-05-09T17:40:58.442755Z","shell.execute_reply.started":"2024-05-09T17:40:58.293890Z","shell.execute_reply":"2024-05-09T17:40:58.441984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=df.drop(\"label\", axis=1)\ny=df[[\"label\"]]\nx.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:40:58.443955Z","iopub.execute_input":"2024-05-09T17:40:58.444298Z","iopub.status.idle":"2024-05-09T17:40:58.528924Z","shell.execute_reply.started":"2024-05-09T17:40:58.444267Z","shell.execute_reply":"2024-05-09T17:40:58.528014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\n\nx = x / 255.0\ny = to_categorical(y)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:40:58.530363Z","iopub.execute_input":"2024-05-09T17:40:58.531026Z","iopub.status.idle":"2024-05-09T17:41:10.625643Z","shell.execute_reply.started":"2024-05-09T17:40:58.530995Z","shell.execute_reply":"2024-05-09T17:41:10.624587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.2,random_state=42)\nx_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:41:10.626909Z","iopub.execute_input":"2024-05-09T17:41:10.627628Z","iopub.status.idle":"2024-05-09T17:41:10.835801Z","shell.execute_reply.started":"2024-05-09T17:41:10.627591Z","shell.execute_reply":"2024-05-09T17:41:10.834890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, Flatten, Input, MaxPooling2D, Dropout,BatchNormalization, Reshape\nfrom sklearn.metrics import f1_score, confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:41:10.836964Z","iopub.execute_input":"2024-05-09T17:41:10.837322Z","iopub.status.idle":"2024-05-09T17:41:10.842366Z","shell.execute_reply.started":"2024-05-09T17:41:10.837290Z","shell.execute_reply":"2024-05-09T17:41:10.841534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Dense(512, activation='relu', input_shape=(784,)))\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:41:10.843571Z","iopub.execute_input":"2024-05-09T17:41:10.843856Z","iopub.status.idle":"2024-05-09T17:41:11.593335Z","shell.execute_reply.started":"2024-05-09T17:41:10.843834Z","shell.execute_reply":"2024-05-09T17:41:11.592580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, validation_data=(x_test, y_test), batch_size=32, epochs=25, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:41:11.594451Z","iopub.execute_input":"2024-05-09T17:41:11.594818Z","iopub.status.idle":"2024-05-09T17:42:14.391229Z","shell.execute_reply.started":"2024-05-09T17:41:11.594785Z","shell.execute_reply":"2024-05-09T17:42:14.390039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss, accuracy = model.evaluate(x_test, y_test)\nloss, accuracy","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:14.393302Z","iopub.execute_input":"2024-05-09T17:42:14.393730Z","iopub.status.idle":"2024-05-09T17:42:14.870299Z","shell.execute_reply.started":"2024-05-09T17:42:14.393692Z","shell.execute_reply":"2024-05-09T17:42:14.869408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"], label=\"Accuracy\")\nplt.plot(history.history[\"val_accuracy\"], label=\"Val Accuracy\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:14.875373Z","iopub.execute_input":"2024-05-09T17:42:14.875681Z","iopub.status.idle":"2024-05-09T17:42:15.184356Z","shell.execute_reply.started":"2024-05-09T17:42:14.875655Z","shell.execute_reply":"2024-05-09T17:42:15.183440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:15.185289Z","iopub.execute_input":"2024-05-09T17:42:15.185574Z","iopub.status.idle":"2024-05-09T17:42:16.159167Z","shell.execute_reply.started":"2024-05-09T17:42:15.185550Z","shell.execute_reply":"2024-05-09T17:42:16.158342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.round(pred), y_test","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:16.160403Z","iopub.execute_input":"2024-05-09T17:42:16.160698Z","iopub.status.idle":"2024-05-09T17:42:16.169106Z","shell.execute_reply.started":"2024-05-09T17:42:16.160673Z","shell.execute_reply":"2024-05-09T17:42:16.168080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"confusion_matrix(np.argmax(y_test, axis=1), np.argmax(np.round(pred), axis=1))","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:16.170453Z","iopub.execute_input":"2024-05-09T17:42:16.171219Z","iopub.status.idle":"2024-05-09T17:42:16.193976Z","shell.execute_reply.started":"2024-05-09T17:42:16.171187Z","shell.execute_reply":"2024-05-09T17:42:16.193160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.heatmap(confusion_matrix(np.argmax(y_test, axis=1), np.argmax(np.round(pred), axis=1)), annot=True);","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:16.194956Z","iopub.execute_input":"2024-05-09T17:42:16.195223Z","iopub.status.idle":"2024-05-09T17:42:16.821724Z","shell.execute_reply.started":"2024-05-09T17:42:16.195200Z","shell.execute_reply":"2024-05-09T17:42:16.820870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(classification_report(np.argmax(y_test, axis=1), np.argmax(np.round(pred), axis=1)))","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:16.822788Z","iopub.execute_input":"2024-05-09T17:42:16.823065Z","iopub.status.idle":"2024-05-09T17:42:16.843362Z","shell.execute_reply.started":"2024-05-09T17:42:16.823039Z","shell.execute_reply":"2024-05-09T17:42:16.842435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_images = df.sample(20)\nselected_images = selected_images.reset_index(drop=True)\n\ndef tahmin(x):\n    x=np.array([x])\n    x=x/255.0\n    pred=model.predict(x)\n    pred=np.round(np.argmax(pred, axis=1))[0]\n    return pred\n\nfig, axes = plt.subplots(5, 4, figsize=(10, 10))\n\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(selected_images.iloc[i, 1:].values.reshape(28, 28), cmap='gray')\n    ax.axis(\"off\")\n    t=tahmin(selected_images.iloc[i, 1:])\n    ax.text(0.5, -0.2, \"Orjinal Adı:\"+str(selected_images.iloc[i, :1].values), transform=ax.transAxes, ha='center')\n    ax.text(0.5, -0.4, \"Tahmin Edilen:\"+str(t), transform=ax.transAxes, ha='center')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:16.844702Z","iopub.execute_input":"2024-05-09T17:42:16.845610Z","iopub.status.idle":"2024-05-09T17:42:19.330584Z","shell.execute_reply.started":"2024-05-09T17:42:16.845570Z","shell.execute_reply":"2024-05-09T17:42:19.329596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:19.331835Z","iopub.execute_input":"2024-05-09T17:42:19.332201Z","iopub.status.idle":"2024-05-09T17:42:21.511567Z","shell.execute_reply.started":"2024-05-09T17:42:19.332165Z","shell.execute_reply":"2024-05-09T17:42:21.510662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x, y, batch_size=32, epochs=25, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:42:21.512841Z","iopub.execute_input":"2024-05-09T17:42:21.513122Z","iopub.status.idle":"2024-05-09T17:43:23.288945Z","shell.execute_reply.started":"2024-05-09T17:42:21.513098Z","shell.execute_reply":"2024-05-09T17:43:23.287856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xt=test\nxt = xt / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:23.290578Z","iopub.execute_input":"2024-05-09T17:43:23.291239Z","iopub.status.idle":"2024-05-09T17:43:23.331365Z","shell.execute_reply.started":"2024-05-09T17:43:23.291195Z","shell.execute_reply":"2024-05-09T17:43:23.330576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=np.argmax(model.predict(xt), axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:23.332599Z","iopub.execute_input":"2024-05-09T17:43:23.333219Z","iopub.status.idle":"2024-05-09T17:43:25.154521Z","shell.execute_reply.started":"2024-05-09T17:43:23.333184Z","shell.execute_reply":"2024-05-09T17:43:25.153515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.156369Z","iopub.execute_input":"2024-05-09T17:43:25.157180Z","iopub.status.idle":"2024-05-09T17:43:25.163197Z","shell.execute_reply.started":"2024-05-09T17:43:25.157135Z","shell.execute_reply":"2024-05-09T17:43:25.162200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Label\"]=pred","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.164355Z","iopub.execute_input":"2024-05-09T17:43:25.164648Z","iopub.status.idle":"2024-05-09T17:43:25.174880Z","shell.execute_reply.started":"2024-05-09T17:43:25.164624Z","shell.execute_reply":"2024-05-09T17:43:25.174189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['ImageId'] = test.index + 1","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.175850Z","iopub.execute_input":"2024-05-09T17:43:25.176113Z","iopub.status.idle":"2024-05-09T17:43:25.187093Z","shell.execute_reply.started":"2024-05-09T17:43:25.176090Z","shell.execute_reply":"2024-05-09T17:43:25.186302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.188010Z","iopub.execute_input":"2024-05-09T17:43:25.188257Z","iopub.status.idle":"2024-05-09T17:43:25.209004Z","shell.execute_reply.started":"2024-05-09T17:43:25.188234Z","shell.execute_reply":"2024-05-09T17:43:25.208149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[[\"ImageId\", \"Label\"]].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.209920Z","iopub.execute_input":"2024-05-09T17:43:25.210194Z","iopub.status.idle":"2024-05-09T17:43:25.255097Z","shell.execute_reply.started":"2024-05-09T17:43:25.210170Z","shell.execute_reply":"2024-05-09T17:43:25.254086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Bu yöntemle Kaggle'da 0.8605 skor elde edildi. Şimdi de CNN yöntemimi deneyelim.","metadata":{}},{"cell_type":"markdown","source":"## CNN ile Modelleme","metadata":{}},{"cell_type":"code","source":"x = np.array(df.drop(\"label\", axis=1))\nx = np.array(x).reshape(42000, 28, 28, 1)\nx = x / 255.0\nx.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.256356Z","iopub.execute_input":"2024-05-09T17:43:25.257078Z","iopub.status.idle":"2024-05-09T17:43:25.636287Z","shell.execute_reply.started":"2024-05-09T17:43:25.257043Z","shell.execute_reply":"2024-05-09T17:43:25.635358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x[0], cmap=\"gray\")","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.637522Z","iopub.execute_input":"2024-05-09T17:43:25.637909Z","iopub.status.idle":"2024-05-09T17:43:25.824047Z","shell.execute_reply.started":"2024-05-09T17:43:25.637875Z","shell.execute_reply":"2024-05-09T17:43:25.823200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = np.array(df[\"label\"])\nx.shape, y.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.825358Z","iopub.execute_input":"2024-05-09T17:43:25.825729Z","iopub.status.idle":"2024-05-09T17:43:25.832353Z","shell.execute_reply.started":"2024-05-09T17:43:25.825696Z","shell.execute_reply":"2024-05-09T17:43:25.831567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, Dense, Flatten, Input, MaxPooling2D, Dropout,BatchNormalization, Reshape\nfrom sklearn.metrics import f1_score, confusion_matrix, classification_report","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.833640Z","iopub.execute_input":"2024-05-09T17:43:25.833920Z","iopub.status.idle":"2024-05-09T17:43:25.843150Z","shell.execute_reply.started":"2024-05-09T17:43:25.833886Z","shell.execute_reply":"2024-05-09T17:43:25.842484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=.2,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.844118Z","iopub.execute_input":"2024-05-09T17:43:25.844369Z","iopub.status.idle":"2024-05-09T17:43:25.854487Z","shell.execute_reply.started":"2024-05-09T17:43:25.844347Z","shell.execute_reply":"2024-05-09T17:43:25.853671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(64, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(128, (3, 3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(rate=0.5))\nmodel.add(Dense(10, activation='softmax'))\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:25.855735Z","iopub.execute_input":"2024-05-09T17:43:25.856123Z","iopub.status.idle":"2024-05-09T17:43:26.026130Z","shell.execute_reply.started":"2024-05-09T17:43:25.856093Z","shell.execute_reply":"2024-05-09T17:43:26.025283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(x, y, batch_size=32, epochs=25, validation_split=0.2)\n#history=model.fit(x_train,y_train,validation_data=(x_test,y_test),batch_size=32, epochs=25, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:43:26.027169Z","iopub.execute_input":"2024-05-09T17:43:26.027434Z","iopub.status.idle":"2024-05-09T17:44:58.336416Z","shell.execute_reply.started":"2024-05-09T17:43:26.027411Z","shell.execute_reply":"2024-05-09T17:44:58.335603Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"selected_images = df.sample(20)\nselected_images = selected_images.reset_index(drop=True)\n\ndef tahmin(x):\n    x = np.array(x).reshape(1, 28, 28, 1)\n    x=x/255.0\n    pred=model.predict(x)\n    pred=np.round(np.argmax(pred, axis=1))[0]\n    return pred\n\nfig, axes = plt.subplots(5, 4, figsize=(10, 10))\n\nfor i, ax in enumerate(axes.flat):\n    ax.imshow(selected_images.iloc[i, 1:].values.reshape(28, 28), cmap='gray')\n    ax.axis(\"off\")\n    t=tahmin(selected_images.iloc[i, 1:])\n    ax.text(0.5, -0.2, \"Orjinal Adı:\"+str(selected_images.iloc[i, :1].values), transform=ax.transAxes, ha='center')\n    ax.text(0.5, -0.4, \"Tahmin Edilen:\"+str(t), transform=ax.transAxes, ha='center')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:44:58.337852Z","iopub.execute_input":"2024-05-09T17:44:58.338129Z","iopub.status.idle":"2024-05-09T17:45:00.943842Z","shell.execute_reply.started":"2024-05-09T17:44:58.338104Z","shell.execute_reply":"2024-05-09T17:45:00.942988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history[\"accuracy\"], label=\"Accuracy\")\nplt.plot(history.history[\"val_accuracy\"], label=\"Val Accuracy\")\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:00.945548Z","iopub.execute_input":"2024-05-09T17:45:00.945998Z","iopub.status.idle":"2024-05-09T17:45:01.198433Z","shell.execute_reply.started":"2024-05-09T17:45:00.945961Z","shell.execute_reply":"2024-05-09T17:45:01.197550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nxt = test\nxt.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:01.199803Z","iopub.execute_input":"2024-05-09T17:45:01.200400Z","iopub.status.idle":"2024-05-09T17:45:03.254647Z","shell.execute_reply.started":"2024-05-09T17:45:01.200366Z","shell.execute_reply":"2024-05-09T17:45:03.253729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xt = np.array(xt).reshape(28000, 28, 28, 1)\nxt = xt / 255.0\nxt.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:03.255888Z","iopub.execute_input":"2024-05-09T17:45:03.256233Z","iopub.status.idle":"2024-05-09T17:45:03.396559Z","shell.execute_reply.started":"2024-05-09T17:45:03.256202Z","shell.execute_reply":"2024-05-09T17:45:03.395573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=np.argmax(model.predict(xt), axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:03.403315Z","iopub.execute_input":"2024-05-09T17:45:03.403623Z","iopub.status.idle":"2024-05-09T17:45:05.675038Z","shell.execute_reply.started":"2024-05-09T17:45:03.403597Z","shell.execute_reply":"2024-05-09T17:45:05.674154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:05.676166Z","iopub.execute_input":"2024-05-09T17:45:05.676489Z","iopub.status.idle":"2024-05-09T17:45:05.684569Z","shell.execute_reply.started":"2024-05-09T17:45:05.676462Z","shell.execute_reply":"2024-05-09T17:45:05.683734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[\"Label\"]=pred\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:05.685790Z","iopub.execute_input":"2024-05-09T17:45:05.686138Z","iopub.status.idle":"2024-05-09T17:45:05.706387Z","shell.execute_reply.started":"2024-05-09T17:45:05.686105Z","shell.execute_reply":"2024-05-09T17:45:05.705478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['ImageId'] = test.index + 1","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:05.707579Z","iopub.execute_input":"2024-05-09T17:45:05.707972Z","iopub.status.idle":"2024-05-09T17:45:05.715701Z","shell.execute_reply.started":"2024-05-09T17:45:05.707941Z","shell.execute_reply":"2024-05-09T17:45:05.714965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test[[\"ImageId\", \"Label\"]].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:05.716825Z","iopub.execute_input":"2024-05-09T17:45:05.717232Z","iopub.status.idle":"2024-05-09T17:45:05.761597Z","shell.execute_reply.started":"2024-05-09T17:45:05.717201Z","shell.execute_reply":"2024-05-09T17:45:05.760792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Bu son yaptığımız modelleme ile Kaggle yarışmasında 0.98682 skor elde edildi.\n\nModeli kaydedip streamlit uygulamasına dönüştürelim.","metadata":{}},{"cell_type":"code","source":"model.save(\"digit_model.keras\")","metadata":{"execution":{"iopub.status.busy":"2024-05-09T17:45:05.762730Z","iopub.execute_input":"2024-05-09T17:45:05.762984Z","iopub.status.idle":"2024-05-09T17:45:05.843621Z","shell.execute_reply.started":"2024-05-09T17:45:05.762961Z","shell.execute_reply":"2024-05-09T17:45:05.842800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Yapılan streamlit uygulamasına <a href=\"https://huggingface.co/spaces/abdullatifkaban/Digit_Recognition\" target=_blank>Hugging Face</a> üzerinden ulaşılabilir.","metadata":{}}]}