{"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":"markdown","source":"importing package","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-09T09:01:06.652821Z","iopub.execute_input":"2021-09-09T09:01:06.653258Z","iopub.status.idle":"2021-09-09T09:01:13.008212Z","shell.execute_reply.started":"2021-09-09T09:01:06.653157Z","shell.execute_reply":"2021-09-09T09:01:13.006942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**loading the train and test data**","metadata":{}},{"cell_type":"code","source":"df=pd.read_csv('../input/digit-recognizer/train.csv')\ntest=pd.read_csv('../input/digit-recognizer/test.csv')\ndf.head(1)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:13.010178Z","iopub.execute_input":"2021-09-09T09:01:13.010741Z","iopub.status.idle":"2021-09-09T09:01:20.007445Z","shell.execute_reply.started":"2021-09-09T09:01:13.010649Z","shell.execute_reply":"2021-09-09T09:01:20.006407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking thetrain and test data value is null or not**","metadata":{}},{"cell_type":"code","source":"df.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.011468Z","iopub.execute_input":"2021-09-09T09:01:20.011791Z","iopub.status.idle":"2021-09-09T09:01:20.054848Z","shell.execute_reply.started":"2021-09-09T09:01:20.011760Z","shell.execute_reply":"2021-09-09T09:01:20.053736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.057013Z","iopub.execute_input":"2021-09-09T09:01:20.057469Z","iopub.status.idle":"2021-09-09T09:01:20.079577Z","shell.execute_reply.started":"2021-09-09T09:01:20.057425Z","shell.execute_reply":"2021-09-09T09:01:20.078192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**removeing the label or output and stored to y variable**","metadata":{}},{"cell_type":"code","source":"y=df['label']\ndf1= df.drop(labels = [\"label\"],axis = 1) \ndf1.head(1)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.081404Z","iopub.execute_input":"2021-09-09T09:01:20.081883Z","iopub.status.idle":"2021-09-09T09:01:20.206503Z","shell.execute_reply.started":"2021-09-09T09:01:20.081839Z","shell.execute_reply":"2021-09-09T09:01:20.204974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2 = df1 / 255.0\ntest1= test / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.208415Z","iopub.execute_input":"2021-09-09T09:01:20.209388Z","iopub.status.idle":"2021-09-09T09:01:20.415861Z","shell.execute_reply.started":"2021-09-09T09:01:20.209248Z","shell.execute_reply":"2021-09-09T09:01:20.414693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"reshapeing train and test data","metadata":{}},{"cell_type":"code","source":"X_train = df2.values.reshape(-1,28,28,1)\ntest2 = test1.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.417511Z","iopub.execute_input":"2021-09-09T09:01:20.417930Z","iopub.status.idle":"2021-09-09T09:01:20.428032Z","shell.execute_reply.started":"2021-09-09T09:01:20.417887Z","shell.execute_reply":"2021-09-09T09:01:20.426827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[1].shape","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.432727Z","iopub.execute_input":"2021-09-09T09:01:20.433183Z","iopub.status.idle":"2021-09-09T09:01:20.443190Z","shell.execute_reply.started":"2021-09-09T09:01:20.433150Z","shell.execute_reply":"2021-09-09T09:01:20.441635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ploting the value**","metadata":{}},{"cell_type":"code","source":"plt.matshow(X_train[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.446251Z","iopub.execute_input":"2021-09-09T09:01:20.446999Z","iopub.status.idle":"2021-09-09T09:01:20.829839Z","shell.execute_reply.started":"2021-09-09T09:01:20.446954Z","shell.execute_reply":"2021-09-09T09:01:20.828557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.831649Z","iopub.execute_input":"2021-09-09T09:01:20.837821Z","iopub.status.idle":"2021-09-09T09:01:20.849258Z","shell.execute_reply.started":"2021-09-09T09:01:20.837769Z","shell.execute_reply":"2021-09-09T09:01:20.847930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"[https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.edureka.co%2Fblog%2Ftensorflow-tutorial%2F&psig=AOvVaw3UvYuQ7iKRvFtab_jqtJi6&ust=1631265549051000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCNCCoPbH8fICFQAAAAAdAAAAABAD](http://)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**flattern the data**","metadata":{}},{"cell_type":"code","source":"X_train_flattern=X_train.reshape(len(X_train),28*28)\ntest_flattern=test2.reshape(len(test),28*28)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.853041Z","iopub.execute_input":"2021-09-09T09:01:20.853935Z","iopub.status.idle":"2021-09-09T09:01:20.866793Z","shell.execute_reply.started":"2021-09-09T09:01:20.853828Z","shell.execute_reply":"2021-09-09T09:01:20.865604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_seed=2","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.868187Z","iopub.execute_input":"2021-09-09T09:01:20.868859Z","iopub.status.idle":"2021-09-09T09:01:20.880192Z","shell.execute_reply.started":"2021-09-09T09:01:20.868814Z","shell.execute_reply":"2021-09-09T09:01:20.878915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"split the data into train and test data","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, Y_train, Y_val = train_test_split(X_train_flattern, y, test_size = 0.1, random_state=random_seed)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:20.882001Z","iopub.execute_input":"2021-09-09T09:01:20.882621Z","iopub.status.idle":"2021-09-09T09:01:21.515478Z","shell.execute_reply.started":"2021-09-09T09:01:20.882563Z","shell.execute_reply":"2021-09-09T09:01:21.514327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras import layers, models\nmodel = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='sigmoid'))","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:25:42.380635Z","iopub.execute_input":"2021-09-09T09:25:42.381091Z","iopub.status.idle":"2021-09-09T09:25:42.466750Z","shell.execute_reply.started":"2021-09-09T09:25:42.381045Z","shell.execute_reply":"2021-09-09T09:25:42.465626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:25:44.679020Z","iopub.execute_input":"2021-09-09T09:25:44.679452Z","iopub.status.idle":"2021-09-09T09:25:44.693138Z","shell.execute_reply.started":"2021-09-09T09:25:44.679420Z","shell.execute_reply":"2021-09-09T09:25:44.691838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(layers.Flatten())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10))","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:25:48.909730Z","iopub.execute_input":"2021-09-09T09:25:48.910159Z","iopub.status.idle":"2021-09-09T09:25:48.942150Z","shell.execute_reply.started":"2021-09-09T09:25:48.910117Z","shell.execute_reply":"2021-09-09T09:25:48.941052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:26:01.374886Z","iopub.execute_input":"2021-09-09T09:26:01.375290Z","iopub.status.idle":"2021-09-09T09:26:01.392202Z","shell.execute_reply.started":"2021-09-09T09:26:01.375243Z","shell.execute_reply":"2021-09-09T09:26:01.390853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:26:05.096864Z","iopub.execute_input":"2021-09-09T09:26:05.097381Z","iopub.status.idle":"2021-09-09T09:26:05.125460Z","shell.execute_reply.started":"2021-09-09T09:26:05.097347Z","shell.execute_reply":"2021-09-09T09:26:05.123460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_val.shape)\nprint(Y_train.shape)\nprint(Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:24.245091Z","iopub.execute_input":"2021-09-09T09:01:24.245631Z","iopub.status.idle":"2021-09-09T09:01:24.253950Z","shell.execute_reply.started":"2021-09-09T09:01:24.245573Z","shell.execute_reply":"2021-09-09T09:01:24.252262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**train the model using tensorflow**","metadata":{}},{"cell_type":"code","source":"model1 = keras.Sequential([\n    keras.layers.Dense(100, input_shape=(784,), activation='relu'),\n    keras.layers.Dense(10, activation='sigmoid')\n])\n\nmodel1.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel1.fit(X_train, Y_train, epochs=100)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:01:24.255788Z","iopub.execute_input":"2021-09-09T09:01:24.256412Z","iopub.status.idle":"2021-09-09T09:04:23.028122Z","shell.execute_reply.started":"2021-09-09T09:01:24.256374Z","shell.execute_reply":"2021-09-09T09:04:23.027023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**evaluate accuracy and loss**","metadata":{}},{"cell_type":"code","source":"loss,acc=model1.evaluate(X_val,Y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:23.030004Z","iopub.execute_input":"2021-09-09T09:04:23.030477Z","iopub.status.idle":"2021-09-09T09:04:23.519062Z","shell.execute_reply.started":"2021-09-09T09:04:23.030435Z","shell.execute_reply":"2021-09-09T09:04:23.517893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**ploting accurcy using confusion matrix**","metadata":{}},{"cell_type":"code","source":"y_predicted = model1.predict(X_val)\ny_predicted_labels = [np.argmax(i) for i in y_predicted]\ncm = tf.math.confusion_matrix(labels=Y_val,predictions=y_predicted_labels)\n\nplt.figure(figsize = (10,7))\nsns.heatmap(cm, annot=True, fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('Truth')","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:23.520697Z","iopub.execute_input":"2021-09-09T09:04:23.521174Z","iopub.status.idle":"2021-09-09T09:04:24.658455Z","shell.execute_reply.started":"2021-09-09T09:04:23.521096Z","shell.execute_reply":"2021-09-09T09:04:24.657218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_model = tf.keras.Sequential([model1, \n                                         tf.keras.layers.Softmax()])","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:24.660264Z","iopub.execute_input":"2021-09-09T09:04:24.660680Z","iopub.status.idle":"2021-09-09T09:04:24.688118Z","shell.execute_reply.started":"2021-09-09T09:04:24.660639Z","shell.execute_reply":"2021-09-09T09:04:24.687157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predition=probability_model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:24.692949Z","iopub.execute_input":"2021-09-09T09:04:24.693279Z","iopub.status.idle":"2021-09-09T09:04:25.758473Z","shell.execute_reply.started":"2021-09-09T09:04:24.693249Z","shell.execute_reply":"2021-09-09T09:04:25.757386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"check the value","metadata":{}},{"cell_type":"code","source":"predition[1]","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:25.760741Z","iopub.execute_input":"2021-09-09T09:04:25.761186Z","iopub.status.idle":"2021-09-09T09:04:25.769443Z","shell.execute_reply.started":"2021-09-09T09:04:25.761141Z","shell.execute_reply":"2021-09-09T09:04:25.767962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"select maximum value from the array","metadata":{}},{"cell_type":"code","source":"np.argmax(predition[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:25.771444Z","iopub.execute_input":"2021-09-09T09:04:25.772386Z","iopub.status.idle":"2021-09-09T09:04:25.784688Z","shell.execute_reply.started":"2021-09-09T09:04:25.772342Z","shell.execute_reply":"2021-09-09T09:04:25.783177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[1]","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:25.788754Z","iopub.execute_input":"2021-09-09T09:04:25.789703Z","iopub.status.idle":"2021-09-09T09:04:25.801989Z","shell.execute_reply.started":"2021-09-09T09:04:25.789595Z","shell.execute_reply":"2021-09-09T09:04:25.800434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"**\n Using hidden layer**","metadata":{}},{"cell_type":"code","source":"model = keras.Sequential([\n    keras.layers.Flatten(input_shape=(28, 28)),\n    keras.layers.Dense(100, activation='relu'),\n    keras.layers.Dense(10, activation='sigmoid')\n])\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.fit(X_train, Y_train, epochs=100)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:04:25.804315Z","iopub.execute_input":"2021-09-09T09:04:25.804885Z","iopub.status.idle":"2021-09-09T09:07:25.566040Z","shell.execute_reply.started":"2021-09-09T09:04:25.804839Z","shell.execute_reply":"2021-09-09T09:07:25.564832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_val,Y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:07:25.567765Z","iopub.execute_input":"2021-09-09T09:07:25.568246Z","iopub.status.idle":"2021-09-09T09:07:26.030545Z","shell.execute_reply.started":"2021-09-09T09:07:25.568190Z","shell.execute_reply":"2021-09-09T09:07:26.029424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict results\nresults = model.predict(test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:07:26.032259Z","iopub.execute_input":"2021-09-09T09:07:26.032678Z","iopub.status.idle":"2021-09-09T09:07:27.611266Z","shell.execute_reply.started":"2021-09-09T09:07:26.032634Z","shell.execute_reply":"2021-09-09T09:07:27.610111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"digit1.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-09T09:07:27.616596Z","iopub.execute_input":"2021-09-09T09:07:27.619064Z","iopub.status.idle":"2021-09-09T09:07:27.720103Z","shell.execute_reply.started":"2021-09-09T09:07:27.619018Z","shell.execute_reply":"2021-09-09T09:07:27.719077Z"},"trusted":true},"execution_count":null,"outputs":[]}]}