{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.6","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"}],"dockerImageVersionId":29955,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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# 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 5GB 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-01-16T17:30:28.307651Z","iopub.execute_input":"2024-01-16T17:30:28.308090Z","iopub.status.idle":"2024-01-16T17:30:28.317409Z","shell.execute_reply.started":"2024-01-16T17:30:28.308049Z","shell.execute_reply":"2024-01-16T17:30:28.316506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import initial training data\n\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import cross_val_score\n\ntraining_file_path = '../input/digit-recognizer/train.csv'\ntest_file_path = '../input/digit-recognizer/test.csv'\n\ntraining_df_initial = pd.read_csv(training_file_path)\n\ntraining_df_target = training_df_initial['label']\n\ntraining_df = training_df_initial.drop('label', axis = 1)\n\ntesting_data = pd.read_csv(test_file_path)\n\n\nprint(testing_data.info())  \n","metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2024-01-16T17:30:36.628972Z","iopub.execute_input":"2024-01-16T17:30:36.629270Z","iopub.status.idle":"2024-01-16T17:30:40.949766Z","shell.execute_reply.started":"2024-01-16T17:30:36.629243Z","shell.execute_reply":"2024-01-16T17:30:40.948945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating the train test split data  \nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test =train_test_split(training_df, training_df_target, test_size = 0.35, random_state = 42)\n\n#Dimensions of the training set data\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-01-16T17:30:47.845201Z","iopub.execute_input":"2024-01-16T17:30:47.845568Z","iopub.status.idle":"2024-01-16T17:30:48.069774Z","shell.execute_reply.started":"2024-01-16T17:30:47.845532Z","shell.execute_reply":"2024-01-16T17:30:48.068730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Image of a random digit in the training set \nsome_digit = training_df_initial.iloc[233,1:]\nsome_digit_image = some_digit.values.reshape(28, 28)\n\nplt.imshow(some_digit_image, cmap = 'binary') \nplt.axis('off')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-01-16T17:30:53.746294Z","iopub.execute_input":"2024-01-16T17:30:53.746760Z","iopub.status.idle":"2024-01-16T17:30:53.834305Z","shell.execute_reply.started":"2024-01-16T17:30:53.746722Z","shell.execute_reply":"2024-01-16T17:30:53.833041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Random image of testing data \n\nsubmitted=testing_data.iloc[476, :]\n\nsubmitted_value_image = submitted.values.reshape(28,28)\n\nplt.imshow(submitted_value_image, cmap = 'binary') \nplt.axis('off')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-01-16T17:30:56.606275Z","iopub.execute_input":"2024-01-16T17:30:56.606681Z","iopub.status.idle":"2024-01-16T17:30:56.657727Z","shell.execute_reply.started":"2024-01-16T17:30:56.606643Z","shell.execute_reply":"2024-01-16T17:30:56.656584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Distribution of training values \nimport seaborn as sns\nsns.countplot(training_df_target)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-16T17:31:00.151179Z","iopub.execute_input":"2024-01-16T17:31:00.151540Z","iopub.status.idle":"2024-01-16T17:31:00.416355Z","shell.execute_reply.started":"2024-01-16T17:31:00.151510Z","shell.execute_reply":"2024-01-16T17:31:00.414829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating callback class for larger training models\n\nimport tensorflow as tf\n\nclass myCallback(tf.keras.callbacks.Callback):\n  def on_epoch_end(self, epoch, logs={}):\n    '''\n    Halts the training after reaching 60 percent accuracy\n\n    Args:\n      epoch (integer) - index of epoch (required but unused in the function definition below)\n      logs (dict) - metric results from the training epoch\n    '''\n\n    # Check accuracy\n    if(logs.get('accuracy') == 1):\n\n      # Stop if threshold is met\n      print(\"\\nAccuracy is at least than 0.9 so cancelling training!\")\n      self.model.stop_training = True\n\n# Instantiate class\ncallbacks = myCallback()","metadata":{"execution":{"iopub.status.busy":"2024-01-16T18:06:03.709030Z","iopub.execute_input":"2024-01-16T18:06:03.709374Z","iopub.status.idle":"2024-01-16T18:06:03.714753Z","shell.execute_reply.started":"2024-01-16T18:06:03.709326Z","shell.execute_reply":"2024-01-16T18:06:03.714064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Will normalize the data before flattening, divide data by 255\nimport tensorflow as tf\nimport numpy as np \n\n\n#Command to clear the memory \ntf.keras.backend.clear_session() \n\n\nX_valid, X_train = training_df[:4000] / 255.0, training_df[4000:] / 255.0\ny_valid, y_train = training_df_target[:4000] , training_df_target[4000:]\n\nmodel3    = tf.keras.models.Sequential([\n            tf.keras.layers.Flatten(input_shape = [28,28]),\n            tf.keras.layers.Dense(300, activation = 'relu'),\n            tf.keras.layers.Dense(100, activation = 'relu'),\n            tf.keras.layers.Dense(50, activation = 'relu'),\n            tf.keras.layers.Dense(10, activation='softmax') \n                                   ]) \n\nmodel3.compile(loss=\"sparse_categorical_crossentropy\",\n                  optimizer=\"sgd\",\n                  metrics=[\"accuracy\"])\n\nhistory = model3.fit(X_train, y_train, validation_data = (X_valid, y_valid), epochs=50, callbacks=[callbacks])","metadata":{"execution":{"iopub.status.busy":"2024-01-16T18:06:20.476220Z","iopub.execute_input":"2024-01-16T18:06:20.476746Z","iopub.status.idle":"2024-01-16T18:09:33.183309Z","shell.execute_reply.started":"2024-01-16T18:06:20.476710Z","shell.execute_reply":"2024-01-16T18:09:33.181443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Summary of most successful model\nmodel3.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-16T18:15:49.450057Z","iopub.execute_input":"2024-01-16T18:15:49.450470Z","iopub.status.idle":"2024-01-16T18:15:49.458897Z","shell.execute_reply.started":"2024-01-16T18:15:49.450432Z","shell.execute_reply":"2024-01-16T18:15:49.456864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Graphing Loss vs. Accuracy\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs = range(len(acc))\n\nplt.plot(epochs, acc, 'r', label='Training accuracy')\nplt.plot(epochs, val_acc, 'b', label='Validation accuracy')\nplt.title('Training and validation accuracy')\nplt.legend()\nplt.figure()\n\nplt.plot(epochs, loss, 'r', label='Training Loss')\nplt.plot(epochs, val_loss, 'b', label='Validation Loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-16T18:11:56.342083Z","iopub.execute_input":"2024-01-16T18:11:56.342729Z","iopub.status.idle":"2024-01-16T18:11:56.632369Z","shell.execute_reply.started":"2024-01-16T18:11:56.342663Z","shell.execute_reply":"2024-01-16T18:11:56.631013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predictions using keras model1 (94% accuracy)\n#y_new = model.predict_classes(testing_data)\n\n#predictions using keras model2 (96% accuracy)\n#y_new2 = model2.predict_classes(testing_data)\n\n#predictions using keras model3 (96% accuracy)\ny_new3 = model3.predict_classes(testing_data)\n\nprint(y_new3)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-16T18:12:30.642099Z","iopub.execute_input":"2024-01-16T18:12:30.642514Z","iopub.status.idle":"2024-01-16T18:12:32.100705Z","shell.execute_reply.started":"2024-01-16T18:12:30.642478Z","shell.execute_reply":"2024-01-16T18:12:32.098518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#working final submissions example, here we used the Keras predictions. \n\nsubmission_df = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\n\nsubmission_df['Label'] = y_new3\n\nsubmission_df.head()\n\nsubmission_df.to_csv('Keras_MNIST_submissions_Newest_Model3', index = False)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-16T18:12:37.499988Z","iopub.execute_input":"2024-01-16T18:12:37.500363Z","iopub.status.idle":"2024-01-16T18:12:37.792181Z","shell.execute_reply.started":"2024-01-16T18:12:37.500311Z","shell.execute_reply":"2024-01-16T18:12:37.790851Z"},"trusted":true},"execution_count":null,"outputs":[]}]}