{"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":"# hand written digits recognition","metadata":{}},{"cell_type":"markdown","source":"accuracy 94%","metadata":{}},{"cell_type":"markdown","source":"The **MNIST** dataset is a well-known dataset in the field of machine learning, it's used for testing & training algorithms and models. It consists of 70,000 images of handwritten digits (0 to 9) that are 28 pixels wide and 28 pixels height. Each image is represented as a grayscale array of 784 pixels (28x28).\n\nThe MNIST dataset is frequently used for image classification tasks, where the goal is to train a machine learning model to correctly identify the digit represented in each image.\n\nDigit recognition is used in a lot of activities like identify the digits on cheques or zip postal codes ... \n\nand in this notebook we will build a model that will identify the hand written digits based on the mnist data set.","metadata":{}},{"cell_type":"markdown","source":"# libraries \n<p> starting by importing the required libraries, we are going to use the following  :</p>\n<b>numpy</b> : for manupilation vector and matrix  </br>\n<b>pandas </b>: for importing and working with the dataset </br>\n<b>matplotlib </b> : for visulazng the dataset </br>\n<b> tensorflow </b> : for building the model","metadata":{}},{"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)\nimport matplotlib.pyplot as plt\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":"2023-04-15T23:55:13.554842Z","iopub.execute_input":"2023-04-15T23:55:13.555238Z","iopub.status.idle":"2023-04-15T23:55:13.580701Z","shell.execute_reply.started":"2023-04-15T23:55:13.555211Z","shell.execute_reply":"2023-04-15T23:55:13.579147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## importing the data set","metadata":{}},{"cell_type":"markdown","source":"lets read our data frame using pandas ","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:13.582815Z","iopub.execute_input":"2023-04-15T23:55:13.583172Z","iopub.status.idle":"2023-04-15T23:55:16.529295Z","shell.execute_reply.started":"2023-04-15T23:55:13.583137Z","shell.execute_reply":"2023-04-15T23:55:16.527846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## inspect the dataset","metadata":{}},{"cell_type":"markdown","source":"After readning the data set lets see the first 5 rows on it. <br>\nWe have the first column that contains the label for each digit and the rest contains the values(value of each pixel) of the image vector(784 pixel => (28,28))","metadata":{}},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.530675Z","iopub.execute_input":"2023-04-15T23:55:16.530960Z","iopub.status.idle":"2023-04-15T23:55:16.551432Z","shell.execute_reply.started":"2023-04-15T23:55:16.530934Z","shell.execute_reply":"2023-04-15T23:55:16.549525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#the shape of the data set \n#it contains 42000 image with shape of (28,28) \ndf.shape\n#train has a label","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.555162Z","iopub.execute_input":"2023-04-15T23:55:16.555689Z","iopub.status.idle":"2023-04-15T23:55:16.566388Z","shell.execute_reply.started":"2023-04-15T23:55:16.555646Z","shell.execute_reply":"2023-04-15T23:55:16.564255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## split the dataset","metadata":{}},{"cell_type":"markdown","source":"Now after we read and seen the data set lets split it to a training set and testing set.<br>\nwe will take 10000 element in the trainig set and 1000 element in the testing set","metadata":{}},{"cell_type":"code","source":"test_slice = df_test[0:100]\n# y_test = np.array(test_slice['label'])\n# test_slice = test_slice.drop('label',axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.568731Z","iopub.execute_input":"2023-04-15T23:55:16.569623Z","iopub.status.idle":"2023-04-15T23:55:16.583702Z","shell.execute_reply.started":"2023-04-15T23:55:16.569580Z","shell.execute_reply":"2023-04-15T23:55:16.581292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the training set\ntrain_slice=df[0:10000]\ny_train = np.array(train_slice['label']) # label\nX_train = np.array(train_slice.drop('label',axis=1))\n","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.585853Z","iopub.execute_input":"2023-04-15T23:55:16.587635Z","iopub.status.idle":"2023-04-15T23:55:16.608913Z","shell.execute_reply.started":"2023-04-15T23:55:16.587586Z","shell.execute_reply":"2023-04-15T23:55:16.607409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#the testing set\ntest_slice= df[10001:11001]\ny_test = np.array(test_slice['label']) # label\nx_test = test_slice.drop('label',axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.610422Z","iopub.execute_input":"2023-04-15T23:55:16.610866Z","iopub.status.idle":"2023-04-15T23:55:16.622098Z","shell.execute_reply.started":"2023-04-15T23:55:16.610824Z","shell.execute_reply":"2023-04-15T23:55:16.620559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the shape of each shape\nprint(f\"the shape of the testing set {test_slice.shape}\")\nprint(f\"the shape of the training set {train_slice.shape}\")\nprint(f\"the shape of x_train set and y_train set {y_train.shape,X_train.shape}\")\nprint(f\"the shape of x_test set and y_test set {y_test.shape,x_test.shape}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.623918Z","iopub.execute_input":"2023-04-15T23:55:16.624596Z","iopub.status.idle":"2023-04-15T23:55:16.632281Z","shell.execute_reply.started":"2023-04-15T23:55:16.624537Z","shell.execute_reply":"2023-04-15T23:55:16.631089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_slice.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.637022Z","iopub.execute_input":"2023-04-15T23:55:16.637760Z","iopub.status.idle":"2023-04-15T23:55:16.657282Z","shell.execute_reply.started":"2023-04-15T23:55:16.637710Z","shell.execute_reply":"2023-04-15T23:55:16.656133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## visualizing the data","metadata":{}},{"cell_type":"code","source":"fig ,ax = plt.subplots(1,10,figsize=(20,3))\nfor i in range(10):\n    testReshaped=X_train[i].reshape(28,28)\n    ax[i].imshow(testReshaped.astype('uint8'),interpolation=\"none\",cmap=\"gray\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:16.659257Z","iopub.execute_input":"2023-04-15T23:55:16.659651Z","iopub.status.idle":"2023-04-15T23:55:17.367221Z","shell.execute_reply.started":"2023-04-15T23:55:16.659612Z","shell.execute_reply":"2023-04-15T23:55:17.365912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"markdown","source":"### import required library for building the model","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:55:17.368462Z","iopub.execute_input":"2023-04-15T23:55:17.369311Z","iopub.status.idle":"2023-04-15T23:55:17.374817Z","shell.execute_reply.started":"2023-04-15T23:55:17.369275Z","shell.execute_reply":"2023-04-15T23:55:17.373089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we will build a neural network of 3 layers, the first layer contains 35 units with relu activation, the second layer contains 30 units with relu activation and the last layer contains 10 units to predict each number with linear activation","metadata":{}},{"cell_type":"code","source":"model= Sequential([\n    tf.keras.Input(shape=(784,)),  \n    Dense(units=512,activation='relu'),\n    #Dense(units=10,activation='relu'),\n    Dense(units=10,activation='linear'),\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:43:42.109870Z","iopub.execute_input":"2023-04-16T00:43:42.110323Z","iopub.status.idle":"2023-04-16T00:43:42.142929Z","shell.execute_reply.started":"2023-04-16T00:43:42.110276Z","shell.execute_reply":"2023-04-16T00:43:42.141652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"for the cost/loss function we will use Sparse Categorical Cross entropy and the adam algorithm. <br>","metadata":{}},{"cell_type":"code","source":"# train and fit the model\nmodel.compile(\n    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n     metrics=['accuracy'],\n)\n\nhistory=model.fit(X_train,y_train,epochs=20,validation_data=(x_test, y_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:43:43.313270Z","iopub.execute_input":"2023-04-16T00:43:43.313668Z","iopub.status.idle":"2023-04-16T00:44:21.699290Z","shell.execute_reply.started":"2023-04-16T00:43:43.313633Z","shell.execute_reply":"2023-04-16T00:44:21.698256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## predict the digit","metadata":{}},{"cell_type":"markdown","source":"after training the model lets use it to predict the digit in the images","metadata":{}},{"cell_type":"code","source":"prr=np.array(test_slice.iloc[999][1:]) # there is 1000 element you can change between 0 999\ntestReshaped=prr.reshape(28,28)\nplt.imshow(testReshaped.astype('uint8'),interpolation=\"none\",cmap=\"gray\")\nplt.title('the digit we will predict')","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:41:06.396699Z","iopub.execute_input":"2023-04-16T00:41:06.397078Z","iopub.status.idle":"2023-04-16T00:41:06.575019Z","shell.execute_reply.started":"2023-04-16T00:41:06.397043Z","shell.execute_reply":"2023-04-16T00:41:06.573953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result=model.predict(prr.reshape(1,784))","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:44:27.353146Z","iopub.execute_input":"2023-04-16T00:44:27.353580Z","iopub.status.idle":"2023-04-16T00:44:27.453224Z","shell.execute_reply.started":"2023-04-16T00:44:27.353541Z","shell.execute_reply":"2023-04-16T00:44:27.451561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"the digit on the image is {np.argmax(result)}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:44:27.616750Z","iopub.execute_input":"2023-04-16T00:44:27.617770Z","iopub.status.idle":"2023-04-16T00:44:27.625437Z","shell.execute_reply.started":"2023-04-16T00:44:27.617719Z","shell.execute_reply":"2023-04-16T00:44:27.623851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## the accuracy of the model","metadata":{}},{"cell_type":"code","source":"loss,accuracy = model.evaluate(x_test, y_test, verbose=0)","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:24:51.625030Z","iopub.execute_input":"2023-04-16T00:24:51.625423Z","iopub.status.idle":"2023-04-16T00:24:51.767626Z","shell.execute_reply.started":"2023-04-16T00:24:51.625386Z","shell.execute_reply":"2023-04-16T00:24:51.765754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"the accuracy is {accuracy*100:.2f}%\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:24:52.256265Z","iopub.execute_input":"2023-04-16T00:24:52.257873Z","iopub.status.idle":"2023-04-16T00:24:52.266119Z","shell.execute_reply.started":"2023-04-16T00:24:52.257810Z","shell.execute_reply":"2023-04-16T00:24:52.264071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"convulution matrix and accuracy","metadata":{}},{"cell_type":"code","source":"test = [np.array(test_slice.iloc[i][1:]) for i in range(1000)]\ntest=np.array(test)\ny_label=[test_slice.iloc[i][0] for i in range(1000)]\ny_pred=[np.argmax(model.predict(test[i].reshape(1,784))) for i in range(1000)]","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:50:23.829083Z","iopub.execute_input":"2023-04-16T00:50:23.829446Z","iopub.status.idle":"2023-04-16T00:51:26.068093Z","shell.execute_reply.started":"2023-04-16T00:50:23.829418Z","shell.execute_reply":"2023-04-16T00:51:26.066157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\ncm=confusion_matrix(y_pred,y_label)\nsns.heatmap(cm,annot=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"here on the horizantal axis we have the digit we wanted to predict and on the vertical axis we have the the predicted digit and how many its predicted","metadata":{}},{"cell_type":"code","source":"#model.metrics_names","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:56:41.601725Z","iopub.execute_input":"2023-04-15T23:56:41.602646Z","iopub.status.idle":"2023-04-15T23:56:41.608089Z","shell.execute_reply.started":"2023-04-15T23:56:41.602607Z","shell.execute_reply":"2023-04-15T23:56:41.606635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"i have imported a written digit i wrote it to predec it","metadata":{}},{"cell_type":"code","source":"from PIL import Image # fro manupilation the images","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:56:41.609695Z","iopub.execute_input":"2023-04-15T23:56:41.610627Z","iopub.status.idle":"2023-04-15T23:56:41.621776Z","shell.execute_reply.started":"2023-04-15T23:56:41.610560Z","shell.execute_reply":"2023-04-15T23:56:41.620068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=Image.open('/kaggle/input/itsnotadataset/2.jpeg')\n# read the image","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:56:41.626048Z","iopub.execute_input":"2023-04-15T23:56:41.626517Z","iopub.status.idle":"2023-04-15T23:56:41.637444Z","shell.execute_reply.started":"2023-04-15T23:56:41.626447Z","shell.execute_reply":"2023-04-15T23:56:41.635792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image # visualize the digit","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:56:41.642600Z","iopub.execute_input":"2023-04-15T23:56:41.643048Z","iopub.status.idle":"2023-04-15T23:56:42.185673Z","shell.execute_reply.started":"2023-04-15T23:56:41.643008Z","shell.execute_reply":"2023-04-15T23:56:42.184285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#preprocecing\nimage=image.resize((28,28))\nimage=image.convert('L')\nimArray=np.array(image)","metadata":{"execution":{"iopub.status.busy":"2023-04-15T23:56:42.187403Z","iopub.execute_input":"2023-04-15T23:56:42.187822Z","iopub.status.idle":"2023-04-15T23:56:42.212371Z","shell.execute_reply.started":"2023-04-15T23:56:42.187784Z","shell.execute_reply":"2023-04-15T23:56:42.210905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict the digit\nresult=model.predict(imArray.reshape(1,784))\nprint(f\"the number is : {np.argmax(result)}\")","metadata":{"execution":{"iopub.status.busy":"2023-04-16T00:44:35.293399Z","iopub.execute_input":"2023-04-16T00:44:35.294027Z","iopub.status.idle":"2023-04-16T00:44:35.397396Z","shell.execute_reply.started":"2023-04-16T00:44:35.293972Z","shell.execute_reply":"2023-04-16T00:44:35.396263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}