{"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":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nfrom tensorflow.keras.activations import relu , linear","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-12T13:36:26.958620Z","iopub.execute_input":"2023-06-12T13:36:26.959065Z","iopub.status.idle":"2023-06-12T13:36:26.968072Z","shell.execute_reply.started":"2023-06-12T13:36:26.959028Z","shell.execute_reply":"2023-06-12T13:36:26.965919Z"},"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-06-12T13:36:26.971137Z","iopub.execute_input":"2023-06-12T13:36:26.971741Z","iopub.status.idle":"2023-06-12T13:36:29.730492Z","shell.execute_reply.started":"2023-06-12T13:36:26.971685Z","shell.execute_reply":"2023-06-12T13:36:29.729637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.732077Z","iopub.execute_input":"2023-06-12T13:36:29.733507Z","iopub.status.idle":"2023-06-12T13:36:29.754737Z","shell.execute_reply.started":"2023-06-12T13:36:29.733424Z","shell.execute_reply":"2023-06-12T13:36:29.752886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.759154Z","iopub.execute_input":"2023-06-12T13:36:29.759593Z","iopub.status.idle":"2023-06-12T13:36:29.791079Z","shell.execute_reply.started":"2023-06-12T13:36:29.759528Z","shell.execute_reply":"2023-06-12T13:36:29.788715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(train.isnull().sum().sort_values(ascending = False))","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.794817Z","iopub.execute_input":"2023-06-12T13:36:29.795217Z","iopub.status.idle":"2023-06-12T13:36:29.839145Z","shell.execute_reply.started":"2023-06-12T13:36:29.795179Z","shell.execute_reply":"2023-06-12T13:36:29.837497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(test.isnull().sum().sort_values(ascending = False))","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.841404Z","iopub.execute_input":"2023-06-12T13:36:29.841769Z","iopub.status.idle":"2023-06-12T13:36:29.871991Z","shell.execute_reply.started":"2023-06-12T13:36:29.841735Z","shell.execute_reply":"2023-06-12T13:36:29.870240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Shape of Training Set : {train.shape}\")\nprint(f\"Shape of Test Set : {test.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.873634Z","iopub.execute_input":"2023-06-12T13:36:29.873993Z","iopub.status.idle":"2023-06-12T13:36:29.882254Z","shell.execute_reply.started":"2023-06-12T13:36:29.873957Z","shell.execute_reply":"2023-06-12T13:36:29.880115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train.iloc[:,1:]\nY = train.label","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.884704Z","iopub.execute_input":"2023-06-12T13:36:29.886046Z","iopub.status.idle":"2023-06-12T13:36:29.895340Z","shell.execute_reply.started":"2023-06-12T13:36:29.885989Z","shell.execute_reply":"2023-06-12T13:36:29.893510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Second Example Before Scaling**","metadata":{}},{"cell_type":"code","source":"image = X.iloc[1]\nplt.imshow(image.to_numpy().reshape(28,28), cmap='gray')\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.896820Z","iopub.execute_input":"2023-06-12T13:36:29.897185Z","iopub.status.idle":"2023-06-12T13:36:29.964908Z","shell.execute_reply.started":"2023-06-12T13:36:29.897140Z","shell.execute_reply":"2023-06-12T13:36:29.963999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nscaler = StandardScaler()\nX = scaler.fit_transform(X)\ntest = scaler.transform(test)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:29.966108Z","iopub.execute_input":"2023-06-12T13:36:29.966659Z","iopub.status.idle":"2023-06-12T13:36:30.424982Z","shell.execute_reply.started":"2023-06-12T13:36:29.966621Z","shell.execute_reply":"2023-06-12T13:36:30.423494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Second Example After Scaling**","metadata":{}},{"cell_type":"code","source":"image = X[1]\nplt.imshow(image.reshape(28,28), cmap='gray')\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:30.429003Z","iopub.execute_input":"2023-06-12T13:36:30.429409Z","iopub.status.idle":"2023-06-12T13:36:30.499398Z","shell.execute_reply.started":"2023-06-12T13:36:30.429372Z","shell.execute_reply":"2023-06-12T13:36:30.497777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train , x_cv , y_train , y_cv = train_test_split(X,Y,test_size = 0.2 , random_state = 0)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:30.501143Z","iopub.execute_input":"2023-06-12T13:36:30.501527Z","iopub.status.idle":"2023-06-12T13:36:30.791431Z","shell.execute_reply.started":"2023-06-12T13:36:30.501486Z","shell.execute_reply":"2023-06-12T13:36:30.790040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    tf.keras.Input(shape = (784,)),\n    Dense(units = 128 , activation = 'relu',name = 'layer1'),    \n    Dense(units = 64 , activation = 'relu',name = 'layer2'),   \n    Dense(units = 10 , activation = 'linear',name = 'layer3'),\n])","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:30.792756Z","iopub.execute_input":"2023-06-12T13:36:30.793228Z","iopub.status.idle":"2023-06-12T13:36:30.843761Z","shell.execute_reply.started":"2023-06-12T13:36:30.793187Z","shell.execute_reply":"2023-06-12T13:36:30.840925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = SparseCategoricalCrossentropy(from_logits = True) , optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0001),metrics = ['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:30.845699Z","iopub.execute_input":"2023-06-12T13:36:30.846133Z","iopub.status.idle":"2023-06-12T13:36:30.863876Z","shell.execute_reply.started":"2023-06-12T13:36:30.846095Z","shell.execute_reply":"2023-06-12T13:36:30.862238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train,y_train,epochs = 30)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:36:30.865855Z","iopub.execute_input":"2023-06-12T13:36:30.866202Z","iopub.status.idle":"2023-06-12T13:38:53.527447Z","shell.execute_reply.started":"2023-06-12T13:36:30.866170Z","shell.execute_reply":"2023-06-12T13:38:53.526163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv_loss, test_acc = model.evaluate(x_cv,y_cv)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:38:53.529291Z","iopub.execute_input":"2023-06-12T13:38:53.529742Z","iopub.status.idle":"2023-06-12T13:38:54.402144Z","shell.execute_reply.started":"2023-06-12T13:38:53.529697Z","shell.execute_reply":"2023-06-12T13:38:54.400331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test)\nprobabilites_of_predictions = tf.nn.softmax(predictions)\npredicted_labels = np.argmax(probabilites_of_predictions , axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:38:54.404738Z","iopub.execute_input":"2023-06-12T13:38:54.405187Z","iopub.status.idle":"2023-06-12T13:38:57.121468Z","shell.execute_reply.started":"2023-06-12T13:38:54.405142Z","shell.execute_reply":"2023-06-12T13:38:57.119922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_df = pd.DataFrame({'ImageId': range(1, len(predicted_labels)+1),\n                           'Label': predicted_labels})\nresults_df","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:38:57.123182Z","iopub.execute_input":"2023-06-12T13:38:57.123499Z","iopub.status.idle":"2023-06-12T13:38:57.138903Z","shell.execute_reply.started":"2023-06-12T13:38:57.123466Z","shell.execute_reply":"2023-06-12T13:38:57.136824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results_df.to_csv('predictions.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T13:38:57.141020Z","iopub.execute_input":"2023-06-12T13:38:57.141436Z","iopub.status.idle":"2023-06-12T13:38:57.174769Z","shell.execute_reply.started":"2023-06-12T13:38:57.141398Z","shell.execute_reply":"2023-06-12T13:38:57.173628Z"},"trusted":true},"execution_count":null,"outputs":[]}]}