{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport random\nimport seaborn as sns\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport keras\nfrom keras.optimizers import Adam\nfrom keras.layers import MaxPooling2D, Conv2D, Flatten, Dense\nfrom keras.callbacks import TensorBoard\nfrom keras.models import Sequential\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"#Importing dataset\ntrain = pd.read_csv('../input/train.csv')\ntest = pd.read_csv('../input/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Viewing the data\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Lets try to visualize any random number\ntraining = np.array(train, dtype = 'float32')\ntesting = np.array(test, dtype = 'float32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"i = random.randint(0, len(training))\nplt.imshow(training[i, 1:].reshape(28, 28))\nlabel = training[i, 0]\nlabel","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So yeah we got our first visualization.\nNow lets try to see all the classes that we have.\nLets visualize them all"},{"metadata":{"trusted":true},"cell_type":"code","source":"train['label'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = []\nw_grid = 5\nl_grid = 2\nfig, axes = plt.subplots(l_grid, w_grid, figsize = (20, 8))\naxes = axes.ravel()\ni = 0\nwhile len(x) < 10:\n    index = np.random.randint(0, len(training))\n    if training[index, 0] not in x:\n        axes[i].imshow(training[index, 1:].reshape((28, 28))) \n        x.append(training[index, 0])\n        i += 1\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So we got an idea of all the digits that we would be training our model."},{"metadata":{},"cell_type":"markdown","source":"Lets make our training and validating sets"},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = training[:, 1:]\ny_train = training[:, 0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_validate, y_train, y_validate = train_test_split(X_train, y_train, test_size = 0.2, random_state = 0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Testing is the testdata set that we will use to test at the end\n#Reshaping the datasets\nX_train = X_train.reshape(X_train.shape[0], *(28, 28, 1))\nX_validate = X_validate.reshape(X_validate.shape[0], *(28, 28, 1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_validate.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"****We are using the cnn algorithm to make the best predictions possible and also cnn provides best ways to deal with images and classifying them****"},{"metadata":{"trusted":true},"cell_type":"code","source":"#Initializing cnn\ncnn = Sequential()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Adding the convolutional layer\ncnn.add(Conv2D(128, 3, 3, input_shape = (28, 28, 1), activation = 'relu'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Adding the max pool layer\ncnn.add(MaxPooling2D(pool_size = (2, 2)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Adding the flattening layer\ncnn.add(Flatten())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Adding the ann layer \ncnn.add(Dense(output_dim = 32, activation = 'relu'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Adding the output layer\ncnn.add(Dense(output_dim = 10, activation = 'sigmoid'))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Compiling the cnn model"},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn.compile(optimizer= Adam(lr = 0.001), loss = 'sparse_categorical_crossentropy', metrics= ['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now the model is ready lets train it on the training set"},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs = 50\ncnn.fit(X_train, y_train, batch_size = 512, epochs = epochs, validation_data = (X_validate, y_validate), verbose = 1,)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Lets see the report "},{"metadata":{"trusted":true},"cell_type":"code","source":"cnn.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prediction = cnn.predict_classes(X_validate)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cm = confusion_matrix(y_validate, prediction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize = (15,10))\nsns.heatmap(cm, annot = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_classes = 10\ntarget_names = ['Class {}'.format(i) for i in range(num_classes)]\n\nprint(classification_report(y_validate, prediction, target_names = target_names))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**So we can see that Class 7 and Class 4 are the classes which are little hard to predict. We can even make this model to work better by applying some general feature scaling or by adding one more hidden layer in the network, but that can result to overfitting so we will stick with 97% accuracy**"},{"metadata":{},"cell_type":"markdown","source":"Now lets predict our test set too"},{"metadata":{"trusted":true},"cell_type":"code","source":"test = np.array(test, 'int')\nTest = test.reshape(test.shape[0], *(28, 28, 1))\nresult = cnn.predict(Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_prediction = cnn.predict_classes(Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_prediction","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result = np.argmax(result,axis = 1)\nresult = pd.Series(result,name=\"Label\")\n\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),result],axis = 1)\n\nsubmission.to_csv(\"Prediction_test_output.csv\",index=False)\n\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}