{"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":"2023-05-29T05:03:17.803162Z","iopub.execute_input":"2023-05-29T05:03:17.803559Z","iopub.status.idle":"2023-05-29T05:03:17.854422Z","shell.execute_reply.started":"2023-05-29T05:03:17.803527Z","shell.execute_reply":"2023-05-29T05:03:17.851374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest= pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:17.860135Z","iopub.execute_input":"2023-05-29T05:03:17.866027Z","iopub.status.idle":"2023-05-29T05:03:23.659258Z","shell.execute_reply.started":"2023-05-29T05:03:17.865981Z","shell.execute_reply":"2023-05-29T05:03:23.658358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:23.660765Z","iopub.execute_input":"2023-05-29T05:03:23.661102Z","iopub.status.idle":"2023-05-29T05:03:23.711145Z","shell.execute_reply.started":"2023-05-29T05:03:23.66107Z","shell.execute_reply":"2023-05-29T05:03:23.710203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:23.713566Z","iopub.execute_input":"2023-05-29T05:03:23.714094Z","iopub.status.idle":"2023-05-29T05:03:23.833997Z","shell.execute_reply.started":"2023-05-29T05:03:23.714063Z","shell.execute_reply":"2023-05-29T05:03:23.83309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = train.drop(['label'], axis=1)\ny_train = train['label']","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:23.835264Z","iopub.execute_input":"2023-05-29T05:03:23.836254Z","iopub.status.idle":"2023-05-29T05:03:23.918201Z","shell.execute_reply.started":"2023-05-29T05:03:23.83622Z","shell.execute_reply":"2023-05-29T05:03:23.917026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MinMaxScaler\nscaler = MinMaxScaler()","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:23.920175Z","iopub.execute_input":"2023-05-29T05:03:23.920602Z","iopub.status.idle":"2023-05-29T05:03:24.347889Z","shell.execute_reply.started":"2023-05-29T05:03:23.920565Z","shell.execute_reply":"2023-05-29T05:03:24.346996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = pd.DataFrame(scaler.fit_transform(x_train), columns=scaler.feature_names_in_)\ntest = pd.DataFrame(scaler.transform(test), columns=scaler.feature_names_in_)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:24.350256Z","iopub.execute_input":"2023-05-29T05:03:24.350633Z","iopub.status.idle":"2023-05-29T05:03:24.768448Z","shell.execute_reply.started":"2023-05-29T05:03:24.3506Z","shell.execute_reply":"2023-05-29T05:03:24.767474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\nx_train = x_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\ny_train = to_categorical(y_train, num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:24.769764Z","iopub.execute_input":"2023-05-29T05:03:24.770124Z","iopub.status.idle":"2023-05-29T05:03:32.365761Z","shell.execute_reply.started":"2023-05-29T05:03:24.770093Z","shell.execute_reply":"2023-05-29T05:03:32.36483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Flatten, Conv2D, MaxPool2D, BatchNormalization, Dropout\nfrom keras.optimizers import SGD","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:32.367026Z","iopub.execute_input":"2023-05-29T05:03:32.368407Z","iopub.status.idle":"2023-05-29T05:03:32.3739Z","shell.execute_reply.started":"2023-05-29T05:03:32.368373Z","shell.execute_reply":"2023-05-29T05:03:32.373049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters=32, kernel_size=(5,5), activation='relu', input_shape=(28,28,1)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=64, kernel_size=(5,5), activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(filters=128, kernel_size=(5,5),  activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(BatchNormalization())\n\n\nmodel.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:32.376967Z","iopub.execute_input":"2023-05-29T05:03:32.377605Z","iopub.status.idle":"2023-05-29T05:03:36.391195Z","shell.execute_reply.started":"2023-05-29T05:03:32.377571Z","shell.execute_reply":"2023-05-29T05:03:36.390288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=SGD(learning_rate=0.01, momentum=0.9,), loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.fit(x_train, y_train, validation_split=0.1, batch_size=50, epochs=50, verbose=2)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:03:36.392643Z","iopub.execute_input":"2023-05-29T05:03:36.392981Z","iopub.status.idle":"2023-05-29T05:07:00.111592Z","shell.execute_reply.started":"2023-05-29T05:03:36.392947Z","shell.execute_reply":"2023-05-29T05:07:00.110326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)\nn_predictions = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:07:22.731715Z","iopub.execute_input":"2023-05-29T05:07:22.732066Z","iopub.status.idle":"2023-05-29T05:07:25.522112Z","shell.execute_reply.started":"2023-05-29T05:07:22.732037Z","shell.execute_reply":"2023-05-29T05:07:25.52116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:08:00.301863Z","iopub.execute_input":"2023-05-29T05:08:00.30221Z","iopub.status.idle":"2023-05-29T05:08:00.326384Z","shell.execute_reply.started":"2023-05-29T05:08:00.302183Z","shell.execute_reply":"2023-05-29T05:08:00.325256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame()\ndf['ImageId'] = range(1, 28001)\ndf['Label'] = n_predictions\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-29T05:08:28.862672Z","iopub.execute_input":"2023-05-29T05:08:28.863124Z","iopub.status.idle":"2023-05-29T05:08:28.956487Z","shell.execute_reply.started":"2023-05-29T05:08:28.863089Z","shell.execute_reply":"2023-05-29T05:08:28.95549Z"},"trusted":true},"execution_count":null,"outputs":[]}]}