{"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":"# importing necessary libraries :\n\nimport torch\nimport torch.nn as nn\nimport torchvision\nfrom torchvision import transforms\nfrom torch import optim\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom torch.utils.data import DataLoader\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:00.644216Z","iopub.execute_input":"2023-02-16T21:51:00.645170Z","iopub.status.idle":"2023-02-16T21:51:03.566333Z","shell.execute_reply.started":"2023-02-16T21:51:00.645046Z","shell.execute_reply":"2023-02-16T21:51:03.564241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loading data :\ntrain = pd.read_csv('../input/digit-recognizer/train.csv', dtype = 'float32')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:09.968069Z","iopub.execute_input":"2023-02-16T21:51:09.968720Z","iopub.status.idle":"2023-02-16T21:51:13.918356Z","shell.execute_reply.started":"2023-02-16T21:51:09.968684Z","shell.execute_reply":"2023-02-16T21:51:13.917213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data size :\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:13.920687Z","iopub.execute_input":"2023-02-16T21:51:13.921162Z","iopub.status.idle":"2023-02-16T21:51:13.928266Z","shell.execute_reply.started":"2023-02-16T21:51:13.921112Z","shell.execute_reply":"2023-02-16T21:51:13.927035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:13.930063Z","iopub.execute_input":"2023-02-16T21:51:13.930524Z","iopub.status.idle":"2023-02-16T21:51:14.134806Z","shell.execute_reply.started":"2023-02-16T21:51:13.930480Z","shell.execute_reply":"2023-02-16T21:51:14.133879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare Dataset for training :-\n\n# split data into features (pixels) and labels (numbers from 0 to 9)\nlabels = train['label'].values\nfeatures = train.loc[:,train.columns != \"label\"].values/255 # normalization\n\n# split data into two parts, training and testing data :- \ntrain_features, test_features, train_targets, test_targets = train_test_split(features,labels,test_size = 0.2,\n                                                                              random_state = 42) \n# create feature and targets tensor for training set.\nfeaturesTrain = torch.from_numpy(train_features)\ntargetsTrain = torch.from_numpy(train_targets).type(torch.LongTensor) # data type is long\n\n# create feature and targets tensor for testing set.\nfeaturesTest = torch.from_numpy(test_features)\ntargetsTest = torch.from_numpy(test_targets).type(torch.LongTensor) # data type is long\n\n# Pytorch train and test sets\ntrain = torch.utils.data.TensorDataset(featuresTrain,targetsTrain)\ntest = torch.utils.data.TensorDataset(featuresTest,targetsTest)\n\n# data loader\ntrain_loader = DataLoader(train, batch_size = 100, shuffle = True)\ntest_loader = DataLoader(test, batch_size = 100, shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:14.137804Z","iopub.execute_input":"2023-02-16T21:51:14.138352Z","iopub.status.idle":"2023-02-16T21:51:14.597234Z","shell.execute_reply.started":"2023-02-16T21:51:14.138304Z","shell.execute_reply":"2023-02-16T21:51:14.596210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# visualize one of the images in data set\nplt.imshow(features[5].reshape(28,28))\nplt.title(str(labels[5]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:14.598787Z","iopub.execute_input":"2023-02-16T21:51:14.599139Z","iopub.status.idle":"2023-02-16T21:51:14.832465Z","shell.execute_reply.started":"2023-02-16T21:51:14.599107Z","shell.execute_reply":"2023-02-16T21:51:14.830974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"iterdata = iter(train_loader)\nimage, label = next(iterdata)\nimage[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:14.833999Z","iopub.execute_input":"2023-02-16T21:51:14.834363Z","iopub.status.idle":"2023-02-16T21:51:14.852431Z","shell.execute_reply.started":"2023-02-16T21:51:14.834323Z","shell.execute_reply":"2023-02-16T21:51:14.851262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building Classifier model:\nmodel = nn.Sequential(nn.Linear(784,392),\n                      nn.ReLU(),\n                      nn.Dropout(0.4),\n                      nn.Linear(392,98),\n                      nn.ReLU(),\n                      nn.Dropout(0.4),\n                      nn.Linear(98,49),\n                      nn.ReLU(),\n                      nn.Dropout(0.4),\n                      nn.Linear(49,10),\n                      nn.LogSoftmax(dim = 1))\n\ncriterion = nn.NLLLoss()\noptimizer = optim.SGD(model.parameters(), lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:14.853900Z","iopub.execute_input":"2023-02-16T21:51:14.854796Z","iopub.status.idle":"2023-02-16T21:51:14.866765Z","shell.execute_reply.started":"2023-02-16T21:51:14.854760Z","shell.execute_reply":"2023-02-16T21:51:14.865695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 100\ntrain_losses = []\ntest_losses = []\ntest_accuracy = []\n\nfor e in range(epochs):\n    tot_train_loss = 0\n    for image, label in train_loader:\n        image = image.view(image.shape[0], -1)\n        optimizer.zero_grad()\n        \n        output = model(image)\n        loss = criterion(output, label)\n        tot_train_loss += loss.item()\n        \n        loss.backward()\n        optimizer.step()\n        \n    else:\n        tot_test_loss = 0\n        accuracy = 0\n        model.eval()\n        \n        with torch.no_grad():\n            for image, label in test_loader:\n                image = image.view(image.shape[0], -1)\n                output = model(image)\n                t_loss = criterion(output, label)\n                tot_test_loss += t_loss.item()\n                \n                ps = torch.exp(output)\n                top_p, top_class = ps.topk(1, dim = 1)\n                equalls = top_class == label.view(*top_class.shape)\n                accuracy += torch.mean(equalls.type(torch.FloatTensor)).item()\n         \n        train_loss = tot_train_loss / len(train_loader)\n        test_loss = tot_test_loss / len(test_loader)\n        train_losses.append(train_loss)\n        test_losses.append(test_loss)\n        test_accuracy.append(accuracy)\n        \n        if (e+1)%10 == 0:\n            print(f\"Epoch: {e+1}/{epochs} .. \"\n                    f\"Training loss: {train_loss:.3f} \"\n                    f\"Testing loss: {test_loss:.3f} \"\n                    f\"Test accuracy: {100*accuracy/len(test_loader):.2f}%\")\n            \n        model.train()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:51:14.868564Z","iopub.execute_input":"2023-02-16T21:51:14.869647Z","iopub.status.idle":"2023-02-16T21:53:43.447322Z","shell.execute_reply.started":"2023-02-16T21:51:14.869607Z","shell.execute_reply":"2023-02-16T21:53:43.445965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [8,5]);\nplt.plot(train_losses, label='Training loss');\nplt.plot(test_losses, label='Testing loss');\nplt.legend(frameon=False);","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:43.449330Z","iopub.execute_input":"2023-02-16T21:53:43.450174Z","iopub.status.idle":"2023-02-16T21:53:43.603409Z","shell.execute_reply.started":"2023-02-16T21:53:43.450123Z","shell.execute_reply":"2023-02-16T21:53:43.601907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = [8,5]);\nplt.plot(test_accuracy, c = 'g');\nplt.xlabel('Number of Epochs');\nplt.ylabel('Accuracy \"%\"');","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-02-16T21:53:43.608564Z","iopub.execute_input":"2023-02-16T21:53:43.608927Z","iopub.status.idle":"2023-02-16T21:53:43.757758Z","shell.execute_reply.started":"2023-02-16T21:53:43.608897Z","shell.execute_reply":"2023-02-16T21:53:43.756434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Accuracy = 97.3%, is it good for prediction, now we want to predict the unseen data through model and get the desired output :","metadata":{"execution":{"iopub.status.busy":"2023-02-02T21:51:19.483228Z","iopub.execute_input":"2023-02-02T21:51:19.483613Z","iopub.status.idle":"2023-02-02T21:51:19.488379Z","shell.execute_reply.started":"2023-02-02T21:51:19.483585Z","shell.execute_reply":"2023-02-02T21:51:19.487211Z"}}},{"cell_type":"code","source":"test = pd.read_csv('../input/digit-recognizer/test.csv', dtype = 'float32')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:43.759574Z","iopub.execute_input":"2023-02-16T21:53:43.760053Z","iopub.status.idle":"2023-02-16T21:53:46.261243Z","shell.execute_reply.started":"2023-02-16T21:53:43.760007Z","shell.execute_reply":"2023-02-16T21:53:46.259964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_arr = test.to_numpy().reshape(28000,28*28)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:46.263000Z","iopub.execute_input":"2023-02-16T21:53:46.263813Z","iopub.status.idle":"2023-02-16T21:53:46.270166Z","shell.execute_reply.started":"2023-02-16T21:53:46.263763Z","shell.execute_reply":"2023-02-16T21:53:46.268914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_arr[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:46.272221Z","iopub.execute_input":"2023-02-16T21:53:46.272734Z","iopub.status.idle":"2023-02-16T21:53:46.285603Z","shell.execute_reply.started":"2023-02-16T21:53:46.272684Z","shell.execute_reply":"2023-02-16T21:53:46.284293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pass testing data through model and predict the output :\npred = []\nfor i in test_arr:\n    i = i.reshape(1,784)                       # to suitable the input size of classifer model (784)\n    i = torch.from_numpy(i).float()            # convert numpy to tensor/float\n    \n    output = model(i)                          # pass image through model\n    \n    ps = torch.exp(output) \n    top_p, top_class = ps.topk(1, dim = 1)     # return top k value for propabilities and it's index\n    pred.append(top_class)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:46.287085Z","iopub.execute_input":"2023-02-16T21:53:46.287611Z","iopub.status.idle":"2023-02-16T21:53:55.045907Z","shell.execute_reply.started":"2023-02-16T21:53:46.287577Z","shell.execute_reply":"2023-02-16T21:53:55.044677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred ","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.047735Z","iopub.execute_input":"2023-02-16T21:53:55.048466Z","iopub.status.idle":"2023-02-16T21:53:55.138143Z","shell.execute_reply.started":"2023-02-16T21:53:55.048415Z","shell.execute_reply":"2023-02-16T21:53:55.136951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# it is a list with only one index but inside it we have 28000 indces (pred[0][0~27999]), \n# we want to convert it to tensor :-\n\n# converting list to tensor :\npred = torch.as_tensor(pred)\npred.type","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.140059Z","iopub.execute_input":"2023-02-16T21:53:55.140536Z","iopub.status.idle":"2023-02-16T21:53:55.170707Z","shell.execute_reply.started":"2023-02-16T21:53:55.140501Z","shell.execute_reply":"2023-02-16T21:53:55.169615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.T","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.172220Z","iopub.execute_input":"2023-02-16T21:53:55.173041Z","iopub.status.idle":"2023-02-16T21:53:55.181067Z","shell.execute_reply.started":"2023-02-16T21:53:55.172993Z","shell.execute_reply":"2023-02-16T21:53:55.179979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_classify(ind):\n    ''' Function for viewing an image and it's predicted classes.\n    '''\n    img = test_arr[ind].reshape(1,28,28).squeeze() # shape (28,28) for display image\n    img_pred = torch.from_numpy(img).reshape(1,784) # shape (1,784) to enter as input inside classifier [n_input = 784, n_output = 10]\n    \n    with torch.no_grad():\n        logps = model(img_pred)\n        \n    ps = torch.exp(logps)\n    \n    ps_np = ps.data.numpy().squeeze() # converting ps values to array \n    \n    fig, (ax1, ax2) = plt.subplots(figsize=(8,14), ncols=2)\n    ax1.imshow(img)\n    ax2.barh(np.arange(10), ps_np)\n    ax2.set_aspect(0.1)\n    ax2.set_yticks(np.arange(10))\n    ax2.set_yticklabels(np.arange(10))\n    \n    ax2.set_title('Class Probability')\n    ax2.set_xlim(0, 1.1)\n    plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.182410Z","iopub.execute_input":"2023-02-16T21:53:55.182729Z","iopub.status.idle":"2023-02-16T21:53:55.192558Z","shell.execute_reply.started":"2023-02-16T21:53:55.182700Z","shell.execute_reply":"2023-02-16T21:53:55.191333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_classify(10)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-02-16T21:53:55.194138Z","iopub.execute_input":"2023-02-16T21:53:55.194479Z","iopub.status.idle":"2023-02-16T21:53:55.483786Z","shell.execute_reply.started":"2023-02-16T21:53:55.194450Z","shell.execute_reply":"2023-02-16T21:53:55.482424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from our prediction list :\npred[10]","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.485065Z","iopub.execute_input":"2023-02-16T21:53:55.485437Z","iopub.status.idle":"2023-02-16T21:53:55.491864Z","shell.execute_reply.started":"2023-02-16T21:53:55.485404Z","shell.execute_reply":"2023-02-16T21:53:55.491101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# saving predicted values :\ndf = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.493269Z","iopub.execute_input":"2023-02-16T21:53:55.493845Z","iopub.status.idle":"2023-02-16T21:53:55.523843Z","shell.execute_reply.started":"2023-02-16T21:53:55.493812Z","shell.execute_reply":"2023-02-16T21:53:55.523003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# change the values inside (Label) column to the predicted value from our model :\ndf['Label'] = pred\n\n# saving the dataframe\n\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.525365Z","iopub.execute_input":"2023-02-16T21:53:55.525965Z","iopub.status.idle":"2023-02-16T21:53:55.562492Z","shell.execute_reply.started":"2023-02-16T21:53:55.525931Z","shell.execute_reply":"2023-02-16T21:53:55.561437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# reading the new file :\ndf = pd.read_csv('submission.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T21:53:55.566082Z","iopub.execute_input":"2023-02-16T21:53:55.566983Z","iopub.status.idle":"2023-02-16T21:53:55.584996Z","shell.execute_reply.started":"2023-02-16T21:53:55.566940Z","shell.execute_reply":"2023-02-16T21:53:55.583744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}