{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom keras.layers import Dense\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.preprocessing import OneHotEncoder\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_set = pd.read_csv('../input/digit-recognizer/train.csv')\nsample_set = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\ntest_set = pd.read_csv('../input/digit-recognizer/test.csv')\ntest = test_set.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_set.head()\ntrain_set.shape\nX = train_set.drop(['label'], axis=1)\ny = train_set.iloc[:,0]\ntrain_set.dtypes\nplt.bar(y.value_counts().index,y.value_counts())\nprint(X.describe())\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Train Test Split\nX_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.1, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#check for the null values\nprint(X_train.isnull().any().describe())\nprint(X_test.isnull().any().describe())\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Normalization\nscaler = MinMaxScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.fit_transform(X_test)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#One Hot Encoding\nfrom keras.utils import np_utils\ny_train = np_utils.to_categorical(y_train)\ny_test = np_utils.to_categorical(y_test)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Model\nmodel = tf.keras.Sequential()\nmodel.add(Dense(units = 784/2, activation = 'relu', input_dim=784))\nmodel.add(Dense(units = 784/4, activation = 'relu'))\nmodel.add(Dense(units = 784/8, activation = 'relu'))\nmodel.add(Dense(units = 784/16, activation = 'relu'))\nmodel.add(Dense(units = 784/32, activation = 'relu'))\nmodel.add(Dense(units = 10, activation = 'softmax'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Compiling the model and fitting\nmodel.compile(loss=\"categorical_crossentropy\",\n              optimizer=\"sgd\",\n              metrics = ['accuracy'])\nmodel.fit(X_train, y_train, batch_size=100, epochs=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#prediction\npredicted = model.predict(X_test)\ny_head = predicted.argmax(axis=1).reshape(-1,1)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Evaluation\nprint(accuracy_score(y_test.argmax(axis=1), y_head))\ncm = confusion_matrix(y_test.argmax(axis=1),y_head)\nsns.heatmap(cm, annot=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Making Predictions on Test Data\ny_head_test = model.predict(test)\nresult_test = y_head_test.argmax(axis=1)\n#Visualising predictions\nfor i in range(1,5):\n    index = np.random.randint(1,28001)\n    plt.subplot(3,3,i)\n    plt.imshow(test[index].reshape(28,28))\n    plt.title(\"Predicted Label : {} \".format(result_test[index]))\nplt.subplots_adjust(hspace = 1.2, wspace = 1.2)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result_test = pd.Series(result_test,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),result_test],axis = 1)\nsubmission.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}