{"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)\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":"2021-12-18T15:48:23.068876Z","iopub.execute_input":"2021-12-18T15:48:23.069200Z","iopub.status.idle":"2021-12-18T15:48:23.078421Z","shell.execute_reply.started":"2021-12-18T15:48:23.069170Z","shell.execute_reply":"2021-12-18T15:48:23.077425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import to_categorical\n\nfrom sklearn.model_selection import KFold\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:23.080142Z","iopub.execute_input":"2021-12-18T15:48:23.080624Z","iopub.status.idle":"2021-12-18T15:48:23.091272Z","shell.execute_reply.started":"2021-12-18T15:48:23.080588Z","shell.execute_reply":"2021-12-18T15:48:23.090285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:23.092949Z","iopub.execute_input":"2021-12-18T15:48:23.093362Z","iopub.status.idle":"2021-12-18T15:48:25.801473Z","shell.execute_reply.started":"2021-12-18T15:48:23.093326Z","shell.execute_reply":"2021-12-18T15:48:25.800827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = train_data.pop('label')","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:25.802661Z","iopub.execute_input":"2021-12-18T15:48:25.803153Z","iopub.status.idle":"2021-12-18T15:48:25.812280Z","shell.execute_reply.started":"2021-12-18T15:48:25.803103Z","shell.execute_reply":"2021-12-18T15:48:25.811614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_data(as_values=True, encode=True, plot=False):\n    train_data = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\n    train_labels = train_data.pop('label')\n    test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n    \n    # summarize loaded dataset\n    if not as_values:\n        print('Train: data={}, labels={}'.format(train_data.shape, train_labels.shape))\n        print('Test: data={}, labels={}'.format(test_data.shape, test_labels.shape))\n    \n    if as_values:\n        train_data = train_data.values\n        train_data = np.reshape(train_data, (train_data.shape[0], 28, 28, 1))\n        train_labels = train_labels.values\n        test_data = test_data.values\n        test_data = np.reshape(test_data, (test_data.shape[0], 28, 28, 1))\n#         test_labels = test_labels.values\n        \n        # one-hot encoding\n        if encode:\n                train_labels = to_categorical(train_labels)\n#                 test_labels = to_categorical(test_labels)\n        \n        # summarize loaded dataset\n        print('Train: data={}, labels={}'.format(train_data.shape, train_labels.shape))\n        print('Test: data={}'.format(test_data.shape))\n        \n    if plot:\n        # plot first few images\n        for i in range(9):\n            # define subplot\n            plt.subplot(330 + 1 + i)\n            # plot raw pixel data\n            plt.imshow(train_data[i], cmap=plt.get_cmap('gray'))\n        # show the figure\n        plt.show()\n    \n    return train_data, train_labels, test_data","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:25.814145Z","iopub.execute_input":"2021-12-18T15:48:25.814861Z","iopub.status.idle":"2021-12-18T15:48:25.829086Z","shell.execute_reply.started":"2021-12-18T15:48:25.814810Z","shell.execute_reply":"2021-12-18T15:48:25.827960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data, train_labels, test_data = load_data(plot=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:25.830502Z","iopub.execute_input":"2021-12-18T15:48:25.830738Z","iopub.status.idle":"2021-12-18T15:48:30.986553Z","shell.execute_reply.started":"2021-12-18T15:48:25.830708Z","shell.execute_reply":"2021-12-18T15:48:30.985321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_data)","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:30.988099Z","iopub.execute_input":"2021-12-18T15:48:30.988402Z","iopub.status.idle":"2021-12-18T15:48:30.994787Z","shell.execute_reply.started":"2021-12-18T15:48:30.988372Z","shell.execute_reply":"2021-12-18T15:48:30.993678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prepare the datset\ndef scale_pixels(train, test):\n    if not (type(train) == np.ndarray) and (type(test) == np.ndarray):\n        train = train.values\n        train = np.reshape(train, (-1, 28, 28))\n        test = test.values\n        test = np.reshape(test, (-1, 28, 28))\n\n    # convert from integers to floats\n    train_norm = train.astype('float32')\n    test_norm = test.astype('float32')\n    # normalize to range 0-1\n    train_norm = train_norm / 255.0\n    test_norm = test_norm / 255.0\n    # return normalized images\n    return train_norm, test_norm","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:30.996403Z","iopub.execute_input":"2021-12-18T15:48:30.996634Z","iopub.status.idle":"2021-12-18T15:48:31.008121Z","shell.execute_reply.started":"2021-12-18T15:48:30.996598Z","shell.execute_reply":"2021-12-18T15:48:31.006974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define a CNN Model\ndef define_model(summary=True):\n    # deifining the model\n    model = keras.Sequential()\n    model.add(keras.layers.Conv2D(32, (3,3), activation='relu', kernel_initializer='he_uniform', input_shape=(28, 28, 1)))\n    model.add(keras.layers.MaxPooling2D((2, 2)))\n    model.add(keras.layers.Flatten())\n    model.add(keras.layers.Dense(128, activation='relu', kernel_initializer='he_uniform'))\n    model.add(keras.layers.Dense(10, activation='softmax'))\n\n    if summary:\n        model.summary()\n\n    # compiling the model\n    opt = keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)  # standard values\n    model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:31.009877Z","iopub.execute_input":"2021-12-18T15:48:31.010209Z","iopub.status.idle":"2021-12-18T15:48:31.028887Z","shell.execute_reply.started":"2021-12-18T15:48:31.010164Z","shell.execute_reply":"2021-12-18T15:48:31.027795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# evaluate a model using k-fold cross-validation\ndef evaluate_model(train, test, n_folds=5):\n\tscores, histories = list(), list()\n\t# prepare cross validation\n\tkfold = KFold(n_folds, shuffle=True, random_state=1)\n\t# enumerate splits\n\tfor train_ix, test_ix in kfold.split(train):\n\t\t# define model\n\t\tmodel = define_model()\n\t\t# select rows for train and test\n\t\ttrainX, trainY, testX, testY = train[train_ix], test[train_ix], train[test_ix], test[test_ix]\n\t\t# fit model\n\t\thistory = model.fit(trainX, trainY, epochs=10, batch_size=32, validation_data=(testX, testY), verbose=0)\n\t\t# evaluate model\n\t\t_, acc = model.evaluate(testX, testY, verbose=0)\n\t\tprint('> %.3f' % (acc * 100.0))\n\t\t# stores scores\n\t\tscores.append(acc)\n\t\thistories.append(history)\n\treturn model, scores, histories","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:31.030553Z","iopub.execute_input":"2021-12-18T15:48:31.030906Z","iopub.status.idle":"2021-12-18T15:48:31.042898Z","shell.execute_reply.started":"2021-12-18T15:48:31.030859Z","shell.execute_reply":"2021-12-18T15:48:31.041975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plot the diagnostics\ndef summarize_diagnostics(histories):\n    for i in range(len(histories)):\n        # plot loss\n        plt.subplot(2, 1, 1)\n        plt.title('Cross Entropy Loss')\n        plt.plot(histories[i].history['loss'], color='blue', label='train')\n        plt.plot(histories[i].history['val_loss'], color='orange', label='test')\n        # plot accuracy\n        plt.subplot(2, 1, 2)\n        plt.title('Classification Accuracy')\n        plt.plot(histories[i].history['accuracy'], color='blue', label='train')\n        plt.plot(histories[i].history['val_accuracy'], color='orange', label='test')\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:31.044553Z","iopub.execute_input":"2021-12-18T15:48:31.045043Z","iopub.status.idle":"2021-12-18T15:48:31.058414Z","shell.execute_reply.started":"2021-12-18T15:48:31.044993Z","shell.execute_reply":"2021-12-18T15:48:31.057668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def summarize_performance(scores):\n    # print summary\n    print('Accuracy: mean=%.3f std=%.3f, n=%d' % (np.mean(scores)*100, np.std(scores)*100, len(scores)))\n    # box and whisker plots of results\n    plt.boxplot(scores)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:31.059408Z","iopub.execute_input":"2021-12-18T15:48:31.059631Z","iopub.status.idle":"2021-12-18T15:48:31.069547Z","shell.execute_reply.started":"2021-12-18T15:48:31.059600Z","shell.execute_reply":"2021-12-18T15:48:31.068914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def run():\n    # load dataset\n    train_data, train_labels, test_data = load_data()\n    # prepare pixel data\n    train_data, test_data = scale_pixels(train_data, test_data)\n    # evaluate model\n    model, scores, histories = evaluate_model(train_data, train_labels)\n    # learning curves\n    summarize_diagnostics(histories)\n    # summarize estimated performance\n    summarize_performance(scores)\n    \n    return model, test_data","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:31.070649Z","iopub.execute_input":"2021-12-18T15:48:31.070969Z","iopub.status.idle":"2021-12-18T15:48:31.082784Z","shell.execute_reply.started":"2021-12-18T15:48:31.070939Z","shell.execute_reply":"2021-12-18T15:48:31.082045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, test_data = run()","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:48:31.087569Z","iopub.execute_input":"2021-12-18T15:48:31.087932Z","iopub.status.idle":"2021-12-18T15:55:01.883775Z","shell.execute_reply.started":"2021-12-18T15:48:31.087887Z","shell.execute_reply":"2021-12-18T15:55:01.882782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_raw = model.predict(test_data)\npredictions = np.argmax(predictions_raw, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:55:43.298498Z","iopub.execute_input":"2021-12-18T15:55:43.299390Z","iopub.status.idle":"2021-12-18T15:55:45.299712Z","shell.execute_reply.started":"2021-12-18T15:55:43.299351Z","shell.execute_reply":"2021-12-18T15:55:45.298812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:55:49.570581Z","iopub.execute_input":"2021-12-18T15:55:49.571272Z","iopub.status.idle":"2021-12-18T15:55:49.577850Z","shell.execute_reply.started":"2021-12-18T15:55:49.571224Z","shell.execute_reply":"2021-12-18T15:55:49.577007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def submit_predictions():\n    submissions = pd.DataFrame({'ImageId': np.arange(1, predictions.shape[0]+1), 'Label': predictions})    \n    submissions.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:55:58.249857Z","iopub.execute_input":"2021-12-18T15:55:58.250162Z","iopub.status.idle":"2021-12-18T15:55:58.255099Z","shell.execute_reply.started":"2021-12-18T15:55:58.250132Z","shell.execute_reply":"2021-12-18T15:55:58.254059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_predictions()","metadata":{"execution":{"iopub.status.busy":"2021-12-18T15:56:03.865093Z","iopub.execute_input":"2021-12-18T15:56:03.865582Z","iopub.status.idle":"2021-12-18T15:56:03.925356Z","shell.execute_reply.started":"2021-12-18T15:56:03.865533Z","shell.execute_reply":"2021-12-18T15:56:03.924345Z"},"trusted":true},"execution_count":null,"outputs":[]}]}