{"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-09-05T14:39:56.540583Z","iopub.execute_input":"2021-09-05T14:39:56.540965Z","iopub.status.idle":"2021-09-05T14:39:56.556296Z","shell.execute_reply.started":"2021-09-05T14:39:56.540878Z","shell.execute_reply":"2021-09-05T14:39:56.555270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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 seaborn as sns\nfrom sklearn.model_selection import train_test_split\n\nnp.random.seed(2)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:39:56.560426Z","iopub.execute_input":"2021-09-05T14:39:56.560686Z","iopub.status.idle":"2021-09-05T14:40:00.857874Z","shell.execute_reply.started":"2021-09-05T14:39:56.560662Z","shell.execute_reply":"2021-09-05T14:40:00.856976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/digit-recognizer/train.csv')\ndf\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:00.859147Z","iopub.execute_input":"2021-09-05T14:40:00.859462Z","iopub.status.idle":"2021-09-05T14:40:06.124755Z","shell.execute_reply.started":"2021-09-05T14:40:00.859428Z","shell.execute_reply":"2021-09-05T14:40:06.123931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.127063Z","iopub.execute_input":"2021-09-05T14:40:06.127650Z","iopub.status.idle":"2021-09-05T14:40:06.134854Z","shell.execute_reply.started":"2021-09-05T14:40:06.127565Z","shell.execute_reply":"2021-09-05T14:40:06.133872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = df[\"label\"]\n\n# Drop 'label' column\nX_train = df.drop(labels = [\"label\"],axis = 1) \n\n# free some space\ndel df \n\ng = sns.countplot(y_train)\n\ny_train.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.136675Z","iopub.execute_input":"2021-09-05T14:40:06.137157Z","iopub.status.idle":"2021-09-05T14:40:06.413553Z","shell.execute_reply.started":"2021-09-05T14:40:06.137122Z","shell.execute_reply":"2021-09-05T14:40:06.412587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.414996Z","iopub.execute_input":"2021-09-05T14:40:06.415333Z","iopub.status.idle":"2021-09-05T14:40:06.448183Z","shell.execute_reply.started":"2021-09-05T14:40:06.415297Z","shell.execute_reply":"2021-09-05T14:40:06.447348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.isnull().any().describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.449368Z","iopub.execute_input":"2021-09-05T14:40:06.449806Z","iopub.status.idle":"2021-09-05T14:40:06.467128Z","shell.execute_reply.started":"2021-09-05T14:40:06.449764Z","shell.execute_reply":"2021-09-05T14:40:06.466123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255.0\ntest = test / 255.0","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.468587Z","iopub.execute_input":"2021-09-05T14:40:06.468929Z","iopub.status.idle":"2021-09-05T14:40:06.638773Z","shell.execute_reply.started":"2021-09-05T14:40:06.468883Z","shell.execute_reply":"2021-09-05T14:40:06.637857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.643470Z","iopub.execute_input":"2021-09-05T14:40:06.643778Z","iopub.status.idle":"2021-09-05T14:40:06.649243Z","shell.execute_reply.started":"2021-09-05T14:40:06.643748Z","shell.execute_reply":"2021-09-05T14:40:06.648382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[1].shape","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.651461Z","iopub.execute_input":"2021-09-05T14:40:06.652051Z","iopub.status.idle":"2021-09-05T14:40:06.660439Z","shell.execute_reply.started":"2021-09-05T14:40:06.652012Z","shell.execute_reply":"2021-09-05T14:40:06.659485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.matshow(X_train[0])","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.661720Z","iopub.execute_input":"2021-09-05T14:40:06.662093Z","iopub.status.idle":"2021-09-05T14:40:06.930183Z","shell.execute_reply.started":"2021-09-05T14:40:06.662057Z","shell.execute_reply":"2021-09-05T14:40:06.929099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.934627Z","iopub.execute_input":"2021-09-05T14:40:06.935022Z","iopub.status.idle":"2021-09-05T14:40:06.946065Z","shell.execute_reply.started":"2021-09-05T14:40:06.934985Z","shell.execute_reply":"2021-09-05T14:40:06.945020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.949206Z","iopub.execute_input":"2021-09-05T14:40:06.952146Z","iopub.status.idle":"2021-09-05T14:40:06.961414Z","shell.execute_reply.started":"2021-09-05T14:40:06.952101Z","shell.execute_reply":"2021-09-05T14:40:06.960551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_flattern=X_train.reshape(len(X_train),28*28)\ntest_flattern=test.reshape(len(test),28*28)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.967855Z","iopub.execute_input":"2021-09-05T14:40:06.968520Z","iopub.status.idle":"2021-09-05T14:40:06.976568Z","shell.execute_reply.started":"2021-09-05T14:40:06.968481Z","shell.execute_reply":"2021-09-05T14:40:06.975745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.978724Z","iopub.execute_input":"2021-09-05T14:40:06.980018Z","iopub.status.idle":"2021-09-05T14:40:06.990154Z","shell.execute_reply.started":"2021-09-05T14:40:06.979978Z","shell.execute_reply":"2021-09-05T14:40:06.989196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_flattern.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:06.993465Z","iopub.execute_input":"2021-09-05T14:40:06.996662Z","iopub.status.idle":"2021-09-05T14:40:07.002397Z","shell.execute_reply.started":"2021-09-05T14:40:06.996625Z","shell.execute_reply":"2021-09-05T14:40:07.001499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_flattern.shape\ntest_flattern[0]\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.003548Z","iopub.execute_input":"2021-09-05T14:40:07.004471Z","iopub.status.idle":"2021-09-05T14:40:07.173144Z","shell.execute_reply.started":"2021-09-05T14:40:07.004433Z","shell.execute_reply":"2021-09-05T14:40:07.172271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_seed=2\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.174437Z","iopub.execute_input":"2021-09-05T14:40:07.174910Z","iopub.status.idle":"2021-09-05T14:40:07.178604Z","shell.execute_reply.started":"2021-09-05T14:40:07.174870Z","shell.execute_reply":"2021-09-05T14:40:07.177790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X_train_flattern, y_train, test_size = 0.1, random_state=random_seed)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.180069Z","iopub.execute_input":"2021-09-05T14:40:07.180658Z","iopub.status.idle":"2021-09-05T14:40:07.556093Z","shell.execute_reply.started":"2021-09-05T14:40:07.180624Z","shell.execute_reply":"2021-09-05T14:40:07.555236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model=keras.Sequential([\n#    keras.layers.Dense(10,input_shape=(784,),activation='sigmoid')\n#])\n#model.compile(optimizer='adam',\n#              loss='sparse_categorical_crossentropy',\n#              metrics=['accuracy'])\n#\n#model.fit(X_train, Y_train, epochs=50)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.557383Z","iopub.execute_input":"2021-09-05T14:40:07.557745Z","iopub.status.idle":"2021-09-05T14:40:07.561987Z","shell.execute_reply.started":"2021-09-05T14:40:07.557710Z","shell.execute_reply":"2021-09-05T14:40:07.560905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#model.evaluate(X_val, Y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.563377Z","iopub.execute_input":"2021-09-05T14:40:07.563832Z","iopub.status.idle":"2021-09-05T14:40:07.571166Z","shell.execute_reply.started":"2021-09-05T14:40:07.563795Z","shell.execute_reply":"2021-09-05T14:40:07.570411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#y_predicted = model.predict(X_val)\n#y_predicted[0]","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.572402Z","iopub.execute_input":"2021-09-05T14:40:07.572861Z","iopub.status.idle":"2021-09-05T14:40:07.579969Z","shell.execute_reply.started":"2021-09-05T14:40:07.572829Z","shell.execute_reply":"2021-09-05T14:40:07.579111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#plt.matshow(X_val[0].reshape(28, 28))","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.581261Z","iopub.execute_input":"2021-09-05T14:40:07.581590Z","iopub.status.idle":"2021-09-05T14:40:07.588639Z","shell.execute_reply.started":"2021-09-05T14:40:07.581556Z","shell.execute_reply":"2021-09-05T14:40:07.587876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#np.argmax(y_predicted[0])\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.592671Z","iopub.execute_input":"2021-09-05T14:40:07.593044Z","iopub.status.idle":"2021-09-05T14:40:07.597321Z","shell.execute_reply.started":"2021-09-05T14:40:07.593017Z","shell.execute_reply":"2021-09-05T14:40:07.596324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#y_predicted_labels = [np.argmax(i) for i in y_predicted]","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.599445Z","iopub.execute_input":"2021-09-05T14:40:07.599943Z","iopub.status.idle":"2021-09-05T14:40:07.604875Z","shell.execute_reply.started":"2021-09-05T14:40:07.599908Z","shell.execute_reply":"2021-09-05T14:40:07.603970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#y_predicted_labels[:5]","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.606411Z","iopub.execute_input":"2021-09-05T14:40:07.606756Z","iopub.status.idle":"2021-09-05T14:40:07.612953Z","shell.execute_reply.started":"2021-09-05T14:40:07.606724Z","shell.execute_reply":"2021-09-05T14:40:07.612200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#cm = tf.math.confusion_matrix(labels=Y_val,predictions=y_predicted_labels)\n#cm","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:40:07.614343Z","iopub.execute_input":"2021-09-05T14:40:07.614712Z","iopub.status.idle":"2021-09-05T14:40:07.621046Z","shell.execute_reply.started":"2021-09-05T14:40:07.614678Z","shell.execute_reply":"2021-09-05T14:40:07.620184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sn\n#plt.figure(figsize = (10,7))\n#sn.heatmap(cm, annot=True, fmt='d')\n#plt.xlabel('Predicted')\n#plt.ylabel('Truth')","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:53:50.745331Z","iopub.execute_input":"2021-09-05T14:53:50.745683Z","iopub.status.idle":"2021-09-05T14:53:50.750272Z","shell.execute_reply.started":"2021-09-05T14:53:50.745651Z","shell.execute_reply":"2021-09-05T14:53:50.749463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=60)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:41:48.536065Z","iopub.execute_input":"2021-09-05T14:41:48.536378Z","iopub.status.idle":"2021-09-05T14:43:53.731260Z","shell.execute_reply.started":"2021-09-05T14:41:48.536348Z","shell.execute_reply":"2021-09-05T14:43:53.730462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel1.evaluate(X_val,Y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:41:34.606468Z","iopub.execute_input":"2021-09-05T14:41:34.606816Z","iopub.status.idle":"2021-09-05T14:41:34.959052Z","shell.execute_reply.started":"2021-09-05T14:41:34.606779Z","shell.execute_reply":"2021-09-05T14:41:34.958293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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))\nsn.heatmap(cm, annot=True, fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('Truth')","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:53:55.150192Z","iopub.execute_input":"2021-09-05T14:53:55.150521Z","iopub.status.idle":"2021-09-05T14:53:55.928065Z","shell.execute_reply.started":"2021-09-05T14:53:55.150489Z","shell.execute_reply":"2021-09-05T14:53:55.927278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_val","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:54:01.130148Z","iopub.execute_input":"2021-09-05T14:54:01.130462Z","iopub.status.idle":"2021-09-05T14:54:01.136823Z","shell.execute_reply.started":"2021-09-05T14:54:01.130432Z","shell.execute_reply":"2021-09-05T14:54:01.136021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = 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=60)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:54:12.845920Z","iopub.execute_input":"2021-09-05T14:54:12.846238Z","iopub.status.idle":"2021-09-05T14:56:18.341616Z","shell.execute_reply.started":"2021-09-05T14:54:12.846210Z","shell.execute_reply":"2021-09-05T14:56:18.340850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(X_val,Y_val)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:56:21.735132Z","iopub.execute_input":"2021-09-05T14:56:21.735449Z","iopub.status.idle":"2021-09-05T14:56:22.065950Z","shell.execute_reply.started":"2021-09-05T14:56:21.735420Z","shell.execute_reply":"2021-09-05T14:56:22.065154Z"},"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-05T14:56:29.280296Z","iopub.execute_input":"2021-09-05T14:56:29.280644Z","iopub.status.idle":"2021-09-05T14:56:30.084526Z","shell.execute_reply.started":"2021-09-05T14:56:29.280613Z","shell.execute_reply":"2021-09-05T14:56:30.083628Z"},"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(\"digit.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-05T14:56:34.715180Z","iopub.execute_input":"2021-09-05T14:56:34.715496Z","iopub.status.idle":"2021-09-05T14:56:34.768244Z","shell.execute_reply.started":"2021-09-05T14:56:34.715465Z","shell.execute_reply":"2021-09-05T14:56:34.767528Z"},"trusted":true},"execution_count":null,"outputs":[]}]}