{"cells":[{"metadata":{},"cell_type":"markdown","source":"> # Recognising Handwritten Digits on MNIST DataSet using KNN"},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mnist =pd.read_csv(\"../input/digit-recognizer/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mnist\n#(rows,cols)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mnist.columns\n#so it represent that first column is label and rest are pixel of label\n#in this data image of 28x28 is stored in format of 784 array of pixels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mnist.head()\n#label isalways y\n#and we have 784 pixels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X= mnist.iloc[: ,1:].values #bcz we are accessing integer val # #we can also do like mnist[:,1:]\n# accesing number instead of categorical data\ny=mnist[\"label\"].values #we can also do like mnist[:,0]\n#here we have used .numpy to convert it into numpy array","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=12)\n#this just for splitting train and test","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nX_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#we can apply kNN on this ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_image_color(img):\n    img=img.reshape((28,28))\n    plt.imshow(img) #this function gives u acolor map \n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image_color(X_train[60])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def plot_image(img):\n    plt.imshow(img.reshape(28,28) ,cmap=\"gray\") #this function gives u acolor map \n    #imshow just plot img and in img u will get (1,784)\n    \n    #so converting 784 into 28x28  \n    plt.show()\n    #as the img is graay scale so if we dont put cmap it \n    #automatically put so some color imshow ()\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot_image(X_train[60]) #you can try this out on different image \nprint(y_train[60])\n#here training timeis o(1) bcz everything is going to happen in prediction time\n# and their is no training time","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# kNN"},{"metadata":{"trusted":true},"cell_type":"code","source":"def distance(pA, pB):\n    return np.sum((pB - pA)**2)**0.5","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def kNN(X, y, x_query, k = 5):\n    \"\"\"\n    X - > (m, 784)  np array (m is just number of eg. images)\n    y - > (m,) np array\n    x_query -> (1,874) np array\n    k -> scaler  int\n    \n    do knn for classification\n    \"\"\"\n    \n    m = X.shape[0]\n    distances = []\n    for i in range(m):\n        dis = distance(x_query, X[i])\n        distances.append((dis, y[i]))\n        \n    distances = sorted(distances)\n    distances = distances[:k]\n    \n    distances = np.array(distances)\n    labels = distances[:, 1]\n    \n    \n    uniq_label, counts = np.unique(labels, return_counts=True)\n\n    pred = uniq_label[counts.argmax()]\n    \n    \n    return int(pred)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# What algorithm is predicting"},{"metadata":{"trusted":true},"cell_type":"code","source":"# prediciting label for test\nkNN(X_train , y_train, X_test[235], k=7)\n#X_test if for testing","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# what we are getting image as output"},{"metadata":{"trusted":true},"cell_type":"code","source":"# displaying test image\nplot_image(X_test[235])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# actual label\ny_test[235]\n#this for checking that is X_train ==y_train","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# accuracy of Knn over test set"},{"metadata":{},"cell_type":"markdown","source":"# Prediciting 100 Images"},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = []\nfor i in range(100):\n    p = kNN(X_train , y_train, X_test[i], k=7)\n    predictions.append(p)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions = np.array(predictions)\npredictions.dtype","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Accuracy on 100 Images"},{"metadata":{"trusted":true},"cell_type":"code","source":"p=(y_test[:100] == predictions)\ntype(p)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(y_test[:100] == predictions).sum()/len(predictions) #or 100","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictions","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# KNN From sklearn"},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.neighbors import KNeighborsClassifier","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"knn=KNeighborsClassifier(n_neighbors=7)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"knn.fit(X_train,y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"yp=[]\nfor i in range(100):\n    p=knn.predict([X_test[i]])#here we have used it to convert it in 2D array\n    p1=np.array(p)\n    yp.append(p1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"yp=np.array(yp)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"yp=yp.reshape((100,)) #this we have done bcz we have earlier converted\n#our X_test into 2D array\n\nprint(yp.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(y_test[:100] == yp).sum()/len(yp) #or 100","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Applying Another Approach "},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow.keras as tf \nmnist_train =pd.read_csv(\"../input/digit-recognizer/train.csv\")\nmnist_test =pd.read_csv(\"../input/digit-recognizer/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(mnist_train)#.head(32000)\nimg= df.drop([\"label\"], axis=1).values\ny= df[\"label\"].values\n\nimg = img.reshape(42000, 28, 28, 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = tf.models.Sequential()\nmodel.add(tf.layers.Conv2D(128, (3, 3), activation='relu', input_shape=(28, 28, 1)))\nmodel.add(tf.layers.MaxPooling2D((2, 2)))\nmodel.add(tf.layers.Conv2D(256, (3, 3), activation='relu'))\nmodel.add(tf.layers.MaxPooling2D((2, 2)))\n\nmodel.add(tf.layers.Flatten())\nmodel.add(tf.layers.Dense(64, activation='relu'))\nmodel.add(tf.layers.Dense(10, activation='softmax'))\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n\nres = model.fit(img, y, epochs=12, validation_split=0.3)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_t=pd.DataFrame(mnist_test)\nimg= df_t.values\n\nimg= img.reshape(28000, 28, 28, 1)\nprediction = np.argmax(model.predict(img), axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# dfinal=pd.DataFrame(data=prediction,columns=[\"Label\"])\n#dfinal\nsample_submission = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\ndfinal= sample_submission.drop([\"Label\"], axis=1) \ndfinal.insert(1,'Label',prediction)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dfinal.to_csv(\"./Recog_MNIST.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}