{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"},{"sourceId":7634242,"sourceType":"datasetVersion","datasetId":4448577},{"sourceId":7634245,"sourceType":"datasetVersion","datasetId":4448579}],"dockerImageVersionId":30646,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"https://www.kaggle.com/code/darkknight98/mnist-digits-dataset-a-simple-cnn-99\nhttps://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-scratch-for-mnist-handwritten-digit-classification/","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:10.106765Z","iopub.execute_input":"2024-02-16T03:11:10.107227Z","iopub.status.idle":"2024-02-16T03:11:10.113795Z","shell.execute_reply.started":"2024-02-16T03:11:10.107197Z","shell.execute_reply":"2024-02-16T03:11:10.112629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/half-images/train_halfImages.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:10.115655Z","iopub.execute_input":"2024-02-16T03:11:10.115950Z","iopub.status.idle":"2024-02-16T03:11:11.988654Z","shell.execute_reply.started":"2024-02-16T03:11:10.115927Z","shell.execute_reply":"2024-02-16T03:11:11.987368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features = train_df.loc[:, \"pixel0\":]\ntrain_labels = train_df.loc[:, \"label\"]","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:11.989751Z","iopub.execute_input":"2024-02-16T03:11:11.990039Z","iopub.status.idle":"2024-02-16T03:11:11.995976Z","shell.execute_reply.started":"2024-02-16T03:11:11.990018Z","shell.execute_reply":"2024-02-16T03:11:11.994928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features = np.array(train_features)\ntrain_labels = np.array(train_labels)\n\ntrain_features = train_features.reshape(train_features.shape[0], 28, 28, 1) # 42000 data points\nprint(train_features.shape, train_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:11.997882Z","iopub.execute_input":"2024-02-16T03:11:11.998361Z","iopub.status.idle":"2024-02-16T03:11:12.031278Z","shell.execute_reply.started":"2024-02-16T03:11:11.998336Z","shell.execute_reply":"2024-02-16T03:11:12.029920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, BatchNormalization\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import Adadelta\nfrom tensorflow.python.keras.utils import np_utils\n\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\nfrom tensorflow.keras.callbacks import LearningRateScheduler","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:12.033587Z","iopub.execute_input":"2024-02-16T03:11:12.034124Z","iopub.status.idle":"2024-02-16T03:11:12.040766Z","shell.execute_reply.started":"2024-02-16T03:11:12.034085Z","shell.execute_reply":"2024-02-16T03:11:12.039323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = np_utils.to_categorical(train_labels, num_classes = 10)\nprint(\"Shape of y_train after encoding: \", train_labels.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:12.042048Z","iopub.execute_input":"2024-02-16T03:11:12.042361Z","iopub.status.idle":"2024-02-16T03:11:12.062822Z","shell.execute_reply.started":"2024-02-16T03:11:12.042335Z","shell.execute_reply":"2024-02-16T03:11:12.061589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_compile_model(input_shape=(28, 28, 1), optimizer='adam', loss='categorical_crossentropy'):\n    model = Sequential()\n    model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape = input_shape))\n    model.add(MaxPool2D((2, 2)))\n    model.add(Flatten())\n    model.add(Dropout(0.4))\n    model.add(Dense(10, activation='softmax'))\n    model.compile(optimizer=optimizer, loss=loss, metrics=[\"accuracy\"])\n    return model\n    \ndef train_model(model, train, test, num_epochs, split):\n    hist = model.fit(train, test, shuffle=True, epochs=num_epochs, validation_split=split)\n    return hist\n\ndef plot_performance(hist, metric, val_metric):\n    plt.plot(hist.history[metric],label = str('Training ' + metric))\n    plt.plot(hist.history[val_metric],label = str('Validation ' + metric))\n    plt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:12.064386Z","iopub.execute_input":"2024-02-16T03:11:12.064983Z","iopub.status.idle":"2024-02-16T03:11:12.073923Z","shell.execute_reply.started":"2024-02-16T03:11:12.064954Z","shell.execute_reply":"2024-02-16T03:11:12.072359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"compiled_model = build_compile_model((28, 28, 1), 'adam', 'categorical_crossentropy')\nmodel_hist = train_model(compiled_model, train_features, train_labels, 50, 0.2)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:11:12.075214Z","iopub.execute_input":"2024-02-16T03:11:12.075552Z","iopub.status.idle":"2024-02-16T03:16:34.724060Z","shell.execute_reply.started":"2024-02-16T03:11:12.075525Z","shell.execute_reply":"2024-02-16T03:16:34.722228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_performance(model_hist, 'accuracy', 'val_accuracy')","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:16:34.725641Z","iopub.execute_input":"2024-02-16T03:16:34.726034Z","iopub.status.idle":"2024-02-16T03:16:34.926861Z","shell.execute_reply.started":"2024-02-16T03:16:34.726004Z","shell.execute_reply":"2024-02-16T03:16:34.925721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_performance(model_hist, 'loss', 'val_loss')","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:16:34.929295Z","iopub.execute_input":"2024-02-16T03:16:34.929563Z","iopub.status.idle":"2024-02-16T03:16:35.123921Z","shell.execute_reply.started":"2024-02-16T03:16:34.929541Z","shell.execute_reply":"2024-02-16T03:16:35.122800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_arr = np.array(test_df)\ntest_df_arr = test_df_arr.reshape(test_df.shape[0], 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:16:35.125210Z","iopub.execute_input":"2024-02-16T03:16:35.125823Z","iopub.status.idle":"2024-02-16T03:16:35.168961Z","shell.execute_reply.started":"2024-02-16T03:16:35.125786Z","shell.execute_reply":"2024-02-16T03:16:35.167272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = compiled_model.predict(test_df_arr)\n\npreds_arr = []\n\nfor i in preds:\n    preds_arr.append(np.argmax(i))","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:16:35.170234Z","iopub.execute_input":"2024-02-16T03:16:35.170995Z","iopub.status.idle":"2024-02-16T03:16:39.066463Z","shell.execute_reply.started":"2024-02-16T03:16:35.170960Z","shell.execute_reply":"2024-02-16T03:16:39.065336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub =  pd.DataFrame({\n        \"ImageId\": test_df.index+1,\n        \"Label\": preds_arr\n    })\n\nsub.to_csv('submission.csv', index=False)\nsub","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:23:34.023453Z","iopub.execute_input":"2024-02-16T03:23:34.023837Z","iopub.status.idle":"2024-02-16T03:23:34.096800Z","shell.execute_reply.started":"2024-02-16T03:23:34.023803Z","shell.execute_reply":"2024-02-16T03:23:34.095549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_df.columns)\n#print(compiled_model.evaluate(test_df.drop(\"label\"), test_df[\"label\"], batch_size=128))","metadata":{"execution":{"iopub.status.busy":"2024-02-16T03:33:20.814726Z","iopub.execute_input":"2024-02-16T03:33:20.815113Z","iopub.status.idle":"2024-02-16T03:33:20.820616Z","shell.execute_reply.started":"2024-02-16T03:33:20.815086Z","shell.execute_reply":"2024-02-16T03:33:20.819864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}