{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\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-07-29T07:06:44.018705Z","iopub.execute_input":"2024-07-29T07:06:44.019306Z","iopub.status.idle":"2024-07-29T07:06:44.377711Z","shell.execute_reply.started":"2024-07-29T07:06:44.019277Z","shell.execute_reply":"2024-07-29T07:06:44.376816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport seaborn as sns\n%matplotlib inline\n\nnp.random.seed(2)\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils import to_categorical\n#from keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop\n#from keras.preprocessing.image import ImageDataGenerator\nfrom keras.src.legacy.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\n\nsns.set(style='white', context='notebook', palette='deep')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:06:46.161443Z","iopub.execute_input":"2024-07-29T07:06:46.161901Z","iopub.status.idle":"2024-07-29T07:06:59.607804Z","shell.execute_reply.started":"2024-07-29T07:06:46.161874Z","shell.execute_reply":"2024-07-29T07:06:59.606743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Idea Notebooks\n\n* https://www.kaggle.com/code/yassineghouzam/introduction-to-cnn-keras-0-997-top-6/notebook\n* https://www.kaggle.com/code/masinasaisatish/cnn-with-accuracy-99-307","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\ntrain.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:14:14.341508Z","iopub.execute_input":"2024-07-29T07:14:14.342327Z","iopub.status.idle":"2024-07-29T07:14:18.482727Z","shell.execute_reply.started":"2024-07-29T07:14:14.342294Z","shell.execute_reply":"2024-07-29T07:14:18.481596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-07-29T06:38:15.599086Z","iopub.execute_input":"2024-07-29T06:38:15.599538Z","iopub.status.idle":"2024-07-29T06:38:15.608751Z","shell.execute_reply.started":"2024-07-29T06:38:15.599501Z","shell.execute_reply":"2024-07-29T06:38:15.607570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images(data):\n    fig, axes = plt.subplots(5, 5, figsize=(10, 10))\n    axes = axes.ravel()\n\n    for i in range(25):\n        img = data.iloc[i, 1:].values.reshape(28, 28)  # Reshape the image\n        label = data.iloc[i, 0]  # Get the label\n        axes[i].imshow(img, cmap='gray')\n        axes[i].set_title(label)\n        axes[i].axis('off')\n    \n    plt.subplots_adjust(hspace=0.5)\n    plt.show()\n\n\nplot_images(train)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:07:08.448968Z","iopub.execute_input":"2024-07-29T07:07:08.449581Z","iopub.status.idle":"2024-07-29T07:07:11.318169Z","shell.execute_reply.started":"2024-07-29T07:07:08.449519Z","shell.execute_reply":"2024-07-29T07:07:11.317216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\ny_train = train[\"label\"]\nX_train = train.drop(labels= \"label\", axis=1)\n\nX_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.25, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:14:37.115924Z","iopub.execute_input":"2024-07-29T07:14:37.116281Z","iopub.status.idle":"2024-07-29T07:14:37.405807Z","shell.execute_reply.started":"2024-07-29T07:14:37.116253Z","shell.execute_reply":"2024-07-29T07:14:37.404714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = sns.countplot(y_train)\n\ny_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T06:38:56.659726Z","iopub.execute_input":"2024-07-29T06:38:56.660250Z","iopub.status.idle":"2024-07-29T06:38:56.885540Z","shell.execute_reply.started":"2024-07-29T06:38:56.660209Z","shell.execute_reply":"2024-07-29T06:38:56.884318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T06:39:09.475425Z","iopub.execute_input":"2024-07-29T06:39:09.475877Z","iopub.status.idle":"2024-07-29T06:39:09.494759Z","shell.execute_reply.started":"2024-07-29T06:39:09.475847Z","shell.execute_reply":"2024-07-29T06:39:09.493496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\nWe perform a grayscale normalization to reduce the effect of illumination's differences.\n\nMoreover the CNN converg faster on [0..1] data than on [0..255].\n","metadata":{}},{"cell_type":"code","source":"train['pixel214']","metadata":{"execution":{"iopub.status.busy":"2024-07-29T06:39:30.086537Z","iopub.execute_input":"2024-07-29T06:39:30.086956Z","iopub.status.idle":"2024-07-29T06:39:30.096571Z","shell.execute_reply.started":"2024-07-29T06:39:30.086923Z","shell.execute_reply":"2024-07-29T06:39:30.095078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalize values, adjust values on a 0 - 1 scale.","metadata":{}},{"cell_type":"code","source":"X_train = X_train / 255.0\ntest = test / 255.0\n#from sklearn.preprocessing import StandardScaler, OneHotEncoder\n\n#StandardScaler(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:14:41.739228Z","iopub.execute_input":"2024-07-29T07:14:41.740080Z","iopub.status.idle":"2024-07-29T07:14:41.811514Z","shell.execute_reply.started":"2024-07-29T07:14:41.740047Z","shell.execute_reply":"2024-07-29T07:14:41.810493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train['pixel214']","metadata":{"execution":{"iopub.status.busy":"2024-07-29T06:53:49.024396Z","iopub.execute_input":"2024-07-29T06:53:49.024805Z","iopub.status.idle":"2024-07-29T06:53:49.034272Z","shell.execute_reply.started":"2024-07-29T06:53:49.024776Z","shell.execute_reply":"2024-07-29T06:53:49.032907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train = X_train.values.reshape(-1,28,28,1)\n# test = test.values.reshape(-1,28,28,1)\n\n# https://www.kaggle.com/code/ngbolin/mnist-dataset-digit-recognizer\nX_train = X_train.to_numpy().reshape(-1, 784) #(33600, 784)\nX_test = X_test.to_numpy().reshape(-1, 784) #(8400, 784)\n\ntest = test.to_numpy().reshape(-1, 784)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:14:44.962395Z","iopub.execute_input":"2024-07-29T07:14:44.962768Z","iopub.status.idle":"2024-07-29T07:14:44.968017Z","shell.execute_reply.started":"2024-07-29T07:14:44.962738Z","shell.execute_reply":"2024-07-29T07:14:44.966986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# g = plt.imshow(X_train[1][:,:,0])\n\ng = plt.imshow(X_train[1].reshape(1, 28, 28, 1)[:,:,0])","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:07:33.143758Z","iopub.execute_input":"2024-07-29T07:07:33.144568Z","iopub.status.idle":"2024-07-29T07:07:33.434717Z","shell.execute_reply.started":"2024-07-29T07:07:33.144519Z","shell.execute_reply":"2024-07-29T07:07:33.433772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n","metadata":{"execution":{"iopub.status.busy":"2024-07-08T08:31:31.459820Z","iopub.execute_input":"2024-07-08T08:31:31.460481Z","iopub.status.idle":"2024-07-08T08:31:31.589750Z","shell.execute_reply.started":"2024-07-08T08:31:31.460448Z","shell.execute_reply":"2024-07-08T08:31:31.588751Z"}}},{"cell_type":"code","source":"y_train = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:17:02.615441Z","iopub.execute_input":"2024-07-29T07:17:02.615818Z","iopub.status.idle":"2024-07-29T07:17:02.620703Z","shell.execute_reply.started":"2024-07-29T07:17:02.615787Z","shell.execute_reply":"2024-07-29T07:17:02.619903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:17:04.668315Z","iopub.execute_input":"2024-07-29T07:17:04.668765Z","iopub.status.idle":"2024-07-29T07:17:04.675355Z","shell.execute_reply.started":"2024-07-29T07:17:04.668733Z","shell.execute_reply":"2024-07-29T07:17:04.674332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:17:09.942434Z","iopub.execute_input":"2024-07-29T07:17:09.943110Z","iopub.status.idle":"2024-07-29T07:17:09.949467Z","shell.execute_reply.started":"2024-07-29T07:17:09.943079Z","shell.execute_reply":"2024-07-29T07:17:09.948475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:07:41.883365Z","iopub.execute_input":"2024-07-29T07:07:41.884050Z","iopub.status.idle":"2024-07-29T07:07:41.914549Z","shell.execute_reply.started":"2024-07-29T07:07:41.884018Z","shell.execute_reply":"2024-07-29T07:07:41.913479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#              width, height, rgb layers \n# input_shape    28,    28,       1\n\nmodel = Sequential()\n# model.add(layers.BatchNormalization())\n# model.add(layers.Dense(512, activation='relu', input_shape=(-1, 28,28,1)))\nmodel.add(layers.Dense(512, activation='relu', input_shape=[784]))\nmodel.add(layers.Dropout(0.3))\n#model.add(layers.BatchNormalization())\nmodel.add(layers.Dense(256, activation='relu'))\nmodel.add(layers.Dropout(0.3))\nmodel.add(layers.Dense(10))\n","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:20:16.006291Z","iopub.execute_input":"2024-07-29T07:20:16.006904Z","iopub.status.idle":"2024-07-29T07:20:16.055309Z","shell.execute_reply.started":"2024-07-29T07:20:16.006874Z","shell.execute_reply":"2024-07-29T07:20:16.054564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='sgd',\n    loss='mae',\n    metrics=['mae'],\n)\nEPOCHS = 100\nhistory = model.fit(\n    X_train, y_train,\n    validation_data=(X_test, y_test),\n    batch_size=64,\n    epochs=EPOCHS,\n    verbose=0,\n)\n\nhistory_df = pd.DataFrame(history.history)\nhistory_df.loc[0:, ['loss', 'val_loss']].plot()\nprint((\"Minimum Validation Loss: {:0.4f}\").format(history_df['val_loss'].min()))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:20:17.794233Z","iopub.execute_input":"2024-07-29T07:20:17.794578Z","iopub.status.idle":"2024-07-29T07:22:01.910190Z","shell.execute_reply.started":"2024-07-29T07:20:17.794544Z","shell.execute_reply":"2024-07-29T07:22:01.909322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:22:18.310980Z","iopub.execute_input":"2024-07-29T07:22:18.311791Z","iopub.status.idle":"2024-07-29T07:22:18.333517Z","shell.execute_reply.started":"2024-07-29T07:22:18.311756Z","shell.execute_reply":"2024-07-29T07:22:18.332724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(X_train, batch_size=200)","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:23:30.134513Z","iopub.execute_input":"2024-07-29T07:23:30.135332Z","iopub.status.idle":"2024-07-29T07:23:31.089151Z","shell.execute_reply.started":"2024-07-29T07:23:30.135301Z","shell.execute_reply":"2024-07-29T07:23:31.088286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(result[3])\n#print(X_train[1])\n\nplt.imshow(X_train[3].reshape(28,28,1))","metadata":{"execution":{"iopub.status.busy":"2024-07-29T07:30:50.743503Z","iopub.execute_input":"2024-07-29T07:30:50.743871Z","iopub.status.idle":"2024-07-29T07:30:51.053829Z","shell.execute_reply.started":"2024-07-29T07:30:50.743842Z","shell.execute_reply":"2024-07-29T07:30:51.052889Z"},"trusted":true},"execution_count":null,"outputs":[]}]}