{"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":" Description :\n In this competition, the goal is to correctly identify digits from a dataset of tens of thousands of handwritten images. ","metadata":{}},{"cell_type":"markdown","source":"Data Source: https://www.kaggle.com/competitions/digit-recognizer/overview","metadata":{}},{"cell_type":"markdown","source":"Starting by importing the required libraries :)","metadata":{}},{"cell_type":"code","source":"import matplotlib as matplotlib\nimport matplotlib as mpl\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.metrics import roc_curve, plot_roc_curve, roc_auc_score, f1_score\nfrom sklearn.model_selection import cross_val_predict\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:12.399475Z","iopub.execute_input":"2022-08-11T19:30:12.400170Z","iopub.status.idle":"2022-08-11T19:30:13.209844Z","shell.execute_reply.started":"2022-08-11T19:30:12.400131Z","shell.execute_reply":"2022-08-11T19:30:13.208784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Download the Data  : ","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntest=pd.read_csv('../input/digit-recognizer/test.csv')\nsample=pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:13.212014Z","iopub.execute_input":"2022-08-11T19:30:13.212490Z","iopub.status.idle":"2022-08-11T19:30:18.876598Z","shell.execute_reply.started":"2022-08-11T19:30:13.212432Z","shell.execute_reply":"2022-08-11T19:30:18.875361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:18.878225Z","iopub.execute_input":"2022-08-11T19:30:18.879136Z","iopub.status.idle":"2022-08-11T19:30:18.910534Z","shell.execute_reply.started":"2022-08-11T19:30:18.879086Z","shell.execute_reply":"2022-08-11T19:30:18.909398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:18.913026Z","iopub.execute_input":"2022-08-11T19:30:18.913388Z","iopub.status.idle":"2022-08-11T19:30:18.933052Z","shell.execute_reply.started":"2022-08-11T19:30:18.913345Z","shell.execute_reply":"2022-08-11T19:30:18.931394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:18.934223Z","iopub.execute_input":"2022-08-11T19:30:18.934584Z","iopub.status.idle":"2022-08-11T19:30:18.952872Z","shell.execute_reply.started":"2022-08-11T19:30:18.934532Z","shell.execute_reply":"2022-08-11T19:30:18.951617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Data is ready\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:18.954297Z","iopub.execute_input":"2022-08-11T19:30:18.955176Z","iopub.status.idle":"2022-08-11T19:30:18.963002Z","shell.execute_reply.started":"2022-08-11T19:30:18.955140Z","shell.execute_reply":"2022-08-11T19:30:18.961960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:18.964385Z","iopub.execute_input":"2022-08-11T19:30:18.964728Z","iopub.status.idle":"2022-08-11T19:30:21.380829Z","shell.execute_reply.started":"2022-08-11T19:30:18.964697Z","shell.execute_reply":"2022-08-11T19:30:21.379630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"train data size is :\"+ str(train.shape))\nprint(\"test data size is :\"+ str(test.shape))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:21.382233Z","iopub.execute_input":"2022-08-11T19:30:21.383215Z","iopub.status.idle":"2022-08-11T19:30:21.390108Z","shell.execute_reply.started":"2022-08-11T19:30:21.383177Z","shell.execute_reply":"2022-08-11T19:30:21.388614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Data Preparation :","metadata":{}},{"cell_type":"markdown","source":"# splitting train data set into y that represents the target label , and x that represents the training examples.","metadata":{}},{"cell_type":"code","source":"y=train['label']\nx=train.drop('label',axis='columns')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:21.392099Z","iopub.execute_input":"2022-08-11T19:30:21.392923Z","iopub.status.idle":"2022-08-11T19:30:21.481395Z","shell.execute_reply.started":"2022-08-11T19:30:21.392875Z","shell.execute_reply":"2022-08-11T19:30:21.480061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:21.486410Z","iopub.execute_input":"2022-08-11T19:30:21.486792Z","iopub.status.idle":"2022-08-11T19:30:21.508097Z","shell.execute_reply.started":"2022-08-11T19:30:21.486756Z","shell.execute_reply":"2022-08-11T19:30:21.506960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lets display some digit from the data set \n\nplt.figure(figsize=(5,5))\nsome_digit=140\nsome_digit_image = x.iloc[some_digit].to_numpy().reshape(28, 28)\nplt.imshow(np.reshape(some_digit_image, (28,28)), cmap=plt.cm.gray)\nprint(y[some_digit])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:21.510031Z","iopub.execute_input":"2022-08-11T19:30:21.510498Z","iopub.status.idle":"2022-08-11T19:30:21.735373Z","shell.execute_reply.started":"2022-08-11T19:30:21.510454Z","shell.execute_reply":"2022-08-11T19:30:21.734155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Display a group of digits\n\ndef print_image(row, df):\n    temp=df.iloc[row,:].values\n    temp = temp.reshape(28,28).astype('uint8')\n    plt.imshow(temp)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:21.737116Z","iopub.execute_input":"2022-08-11T19:30:21.737878Z","iopub.status.idle":"2022-08-11T19:30:21.744800Z","shell.execute_reply.started":"2022-08-11T19:30:21.737829Z","shell.execute_reply":"2022-08-11T19:30:21.743635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(30):\n    plt.subplot(5, 6, i+1)\n    print_image(i, test)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:21.746195Z","iopub.execute_input":"2022-08-11T19:30:21.747293Z","iopub.status.idle":"2022-08-11T19:30:24.150505Z","shell.execute_reply.started":"2022-08-11T19:30:21.747239Z","shell.execute_reply":"2022-08-11T19:30:24.149291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check that the data is balanced :\nsns.countplot(train['label'])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:24.151814Z","iopub.execute_input":"2022-08-11T19:30:24.152315Z","iopub.status.idle":"2022-08-11T19:30:24.378083Z","shell.execute_reply.started":"2022-08-11T19:30:24.152282Z","shell.execute_reply":"2022-08-11T19:30:24.376851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:24.379785Z","iopub.execute_input":"2022-08-11T19:30:24.380147Z","iopub.status.idle":"2022-08-11T19:30:24.389596Z","shell.execute_reply.started":"2022-08-11T19:30:24.380114Z","shell.execute_reply":"2022-08-11T19:30:24.388389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train test split :\nfrom sklearn.model_selection import  train_test_split\nx_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.30, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:24.391088Z","iopub.execute_input":"2022-08-11T19:30:24.391452Z","iopub.status.idle":"2022-08-11T19:30:24.747928Z","shell.execute_reply.started":"2022-08-11T19:30:24.391420Z","shell.execute_reply":"2022-08-11T19:30:24.746707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Models Building","metadata":{}},{"cell_type":"markdown","source":"# 1 - KNN ","metadata":{}},{"cell_type":"code","source":"# Model fitting\nfrom sklearn.neighbors import KNeighborsClassifier\nKNN_classifier = KNeighborsClassifier(n_neighbors = 5)\nKNN_classifier.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:24.749587Z","iopub.execute_input":"2022-08-11T19:30:24.749937Z","iopub.status.idle":"2022-08-11T19:30:24.908028Z","shell.execute_reply.started":"2022-08-11T19:30:24.749902Z","shell.execute_reply":"2022-08-11T19:30:24.907089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model prediction\nKNN_y_pred = KNN_classifier.predict(x_test)\nprint(KNN_y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:13:34.397182Z","iopub.execute_input":"2022-08-11T20:13:34.397606Z","iopub.status.idle":"2022-08-11T20:13:51.992568Z","shell.execute_reply.started":"2022-08-11T20:13:34.397571Z","shell.execute_reply":"2022-08-11T20:13:51.991149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,classification_report,confusion_matrix\nprint(accuracy_score(y_test, KNN_y_pred))\nprint(classification_report(y_test, KNN_y_pred))\nprint(confusion_matrix(y_test, KNN_y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:44.555174Z","iopub.execute_input":"2022-08-11T19:30:44.555627Z","iopub.status.idle":"2022-08-11T19:30:44.609400Z","shell.execute_reply.started":"2022-08-11T19:30:44.555572Z","shell.execute_reply":"2022-08-11T19:30:44.607798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We can say that 5NN Model gets accuracy of 97% and it's performance is very good :) ,But let us look at the confusion matrix ","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:44.611261Z","iopub.execute_input":"2022-08-11T19:30:44.612318Z","iopub.status.idle":"2022-08-11T19:30:44.618173Z","shell.execute_reply.started":"2022-08-11T19:30:44.612263Z","shell.execute_reply":"2022-08-11T19:30:44.616798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion Matrix Heatmap for more visulaization ","metadata":{}},{"cell_type":"code","source":"S=sns.heatmap(confusion_matrix(y_test, KNN_y_pred), annot=True ,cmap=\"nipy_spectral_r\").set_title(\"MNIST_Confusion_Matrix_Heatmap\")\n","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:44.620581Z","iopub.execute_input":"2022-08-11T19:30:44.621613Z","iopub.status.idle":"2022-08-11T19:30:45.259876Z","shell.execute_reply.started":"2022-08-11T19:30:44.621543Z","shell.execute_reply":"2022-08-11T19:30:45.258632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's try K=7 instead of K=5 and see the performance:\n\nfrom sklearn.neighbors import KNeighborsClassifier\nKNN_classifier = KNeighborsClassifier(n_neighbors = 7)\nKNN_classifier.fit(x_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:45.261185Z","iopub.execute_input":"2022-08-11T19:30:45.261531Z","iopub.status.idle":"2022-08-11T19:30:45.280159Z","shell.execute_reply.started":"2022-08-11T19:30:45.261501Z","shell.execute_reply":"2022-08-11T19:30:45.278916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"KNN_y_pred = KNN_classifier.predict(x_test)\nprint(KNN_y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:30:45.281458Z","iopub.execute_input":"2022-08-11T19:30:45.282402Z","iopub.status.idle":"2022-08-11T19:31:02.425833Z","shell.execute_reply.started":"2022-08-11T19:30:45.282365Z","shell.execute_reply":"2022-08-11T19:31:02.424352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score,classification_report,confusion_matrix\nprint(accuracy_score(y_test, KNN_y_pred))\nprint(classification_report(y_test, KNN_y_pred))\nprint(confusion_matrix(y_test, KNN_y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:02.427266Z","iopub.execute_input":"2022-08-11T19:31:02.428133Z","iopub.status.idle":"2022-08-11T19:31:02.476509Z","shell.execute_reply.started":"2022-08-11T19:31:02.428089Z","shell.execute_reply":"2022-08-11T19:31:02.474582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 5NN is better than 7NN but both performance are close together \n# 5NN accuracy is 97% \n# 7NN accuracy is 96%","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:02.478523Z","iopub.execute_input":"2022-08-11T19:31:02.479594Z","iopub.status.idle":"2022-08-11T19:31:02.484140Z","shell.execute_reply.started":"2022-08-11T19:31:02.479525Z","shell.execute_reply":"2022-08-11T19:31:02.482781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's Try Another Model","metadata":{}},{"cell_type":"markdown","source":"# 2-Training a Binary Classifier (SGD Classifier)\n","metadata":{}},{"cell_type":"code","source":"y_train_5 = (y_train == 5) # True for all 5s, False for all other digits.\ny_test_5 = (y_test == 5)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:02.485551Z","iopub.execute_input":"2022-08-11T19:31:02.486780Z","iopub.status.idle":"2022-08-11T19:31:02.501306Z","shell.execute_reply.started":"2022-08-11T19:31:02.486736Z","shell.execute_reply":"2022-08-11T19:31:02.499638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Okay, now let’s pick a classifier and train it. A good place to start is with a Stochastic\n# Gradient Descent (SGD) classifier, using Scikit-Learn’s SGDClassifier class.\n\nfrom sklearn.linear_model import SGDClassifier\nsgd_clf = SGDClassifier(random_state=42)\nsgd_clf.fit(x_train, y_train_5)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:02.503144Z","iopub.execute_input":"2022-08-11T19:31:02.504897Z","iopub.status.idle":"2022-08-11T19:31:07.963289Z","shell.execute_reply.started":"2022-08-11T19:31:02.504829Z","shell.execute_reply":"2022-08-11T19:31:07.961906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"# Let's print some_digit that is labeled 5:","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(5,5))\nsome_digit=19\nsome_digit_image = x.iloc[some_digit].to_numpy().reshape(28, 28)\nplt.imshow(np.reshape(some_digit_image, (28,28)), cmap=plt.cm.gray)\nprint(y[some_digit])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:07.970073Z","iopub.execute_input":"2022-08-11T19:31:07.971140Z","iopub.status.idle":"2022-08-11T19:31:08.428104Z","shell.execute_reply.started":"2022-08-11T19:31:07.971098Z","shell.execute_reply":"2022-08-11T19:31:08.426390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"some_digit = x_train.iloc[44]\nsome_digit_image = some_digit.to_numpy().reshape(28, 28)\nplt.imshow(some_digit_image, cmap = mpl.cm.binary, interpolation=\"nearest\")\nplt.axis(\"off\")\nplt.show()\nprint(y_train.iloc[44])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:08.430531Z","iopub.execute_input":"2022-08-11T19:31:08.430966Z","iopub.status.idle":"2022-08-11T19:31:08.539287Z","shell.execute_reply.started":"2022-08-11T19:31:08.430926Z","shell.execute_reply":"2022-08-11T19:31:08.537666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now, detect images of the number 5:\nsgd_clf.predict([some_digit])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:08.542136Z","iopub.execute_input":"2022-08-11T19:31:08.543439Z","iopub.status.idle":"2022-08-11T19:31:08.559108Z","shell.execute_reply.started":"2022-08-11T19:31:08.543370Z","shell.execute_reply":"2022-08-11T19:31:08.557206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# If the number was any number but not 5 :\nsome_digit = x_train.iloc[4]\nprint(y_train.iloc[4])\nsgd_clf.predict([some_digit])","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:08.562377Z","iopub.execute_input":"2022-08-11T19:31:08.563655Z","iopub.status.idle":"2022-08-11T19:31:08.581240Z","shell.execute_reply.started":"2022-08-11T19:31:08.563581Z","shell.execute_reply":"2022-08-11T19:31:08.579471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SGD Confusion Matrix\n\n#Each row in a confusion matrix represents an actual class, while each column represents\n#a predicted class. -->","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import cross_val_predict\ny_train_pred = cross_val_predict(sgd_clf, x_train, y_train_5, cv=3)\ny_train_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:08.583071Z","iopub.execute_input":"2022-08-11T19:31:08.583726Z","iopub.status.idle":"2022-08-11T19:31:21.827938Z","shell.execute_reply.started":"2022-08-11T19:31:08.583688Z","shell.execute_reply":"2022-08-11T19:31:21.824395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nconfusion_matrix(y_train_5, y_train_pred)  ","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:21.834394Z","iopub.execute_input":"2022-08-11T19:31:21.835403Z","iopub.status.idle":"2022-08-11T19:31:21.857586Z","shell.execute_reply.started":"2022-08-11T19:31:21.835326Z","shell.execute_reply":"2022-08-11T19:31:21.855645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_perfect_predictions = y_train_5 # pretend we reached perfection\nconfusion_matrix(y_train_5, y_train_perfect_predictions)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:21.864502Z","iopub.execute_input":"2022-08-11T19:31:21.867107Z","iopub.status.idle":"2022-08-11T19:31:21.903367Z","shell.execute_reply.started":"2022-08-11T19:31:21.867028Z","shell.execute_reply":"2022-08-11T19:31:21.901400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's check for the performance score:\nfrom sklearn.metrics import precision_score, recall_score ,f1_score\nprint(\"precision score is: \"+ str(precision_score(y_train_5, y_train_pred)))\nprint(\"recall score is: \"+str(recall_score(y_train_5, y_train_pred)))\nprint(\"f1 score is: \"+str(f1_score(y_train_5, y_train_pred)))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:21.906581Z","iopub.execute_input":"2022-08-11T19:31:21.908082Z","iopub.status.idle":"2022-08-11T19:31:21.965091Z","shell.execute_reply.started":"2022-08-11T19:31:21.907961Z","shell.execute_reply":"2022-08-11T19:31:21.963714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Precision/Recall Tradeoff","metadata":{}},{"cell_type":"code","source":"# lowering the threshold increases recall and reduces precision.\n# Instead of calling the classifier’s predict() method, you can call its decision_function() method\n# which returns a score for each instance, and then make predictions based on those scores using any threshold you want:)\n# Remember that: A high-precision classifier is not very useful if its recall is too low!","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:21.967181Z","iopub.execute_input":"2022-08-11T19:31:21.967759Z","iopub.status.idle":"2022-08-11T19:31:21.972962Z","shell.execute_reply.started":"2022-08-11T19:31:21.967709Z","shell.execute_reply":"2022-08-11T19:31:21.971905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_scores = sgd_clf.decision_function([some_digit])\ny_scores","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:21.974217Z","iopub.execute_input":"2022-08-11T19:31:21.975464Z","iopub.status.idle":"2022-08-11T19:31:21.990524Z","shell.execute_reply.started":"2022-08-11T19:31:21.975427Z","shell.execute_reply":"2022-08-11T19:31:21.989525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"threshold = 0\ny_some_digit_pred = (y_scores > threshold)\ny_some_digit_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:21.991953Z","iopub.execute_input":"2022-08-11T19:31:21.992501Z","iopub.status.idle":"2022-08-11T19:31:22.005738Z","shell.execute_reply.started":"2022-08-11T19:31:21.992466Z","shell.execute_reply":"2022-08-11T19:31:22.004551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's reduce the threshold\nthreshold = -9000\ny_some_digit_pred = (y_scores > threshold)\ny_some_digit_pred","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:22.006998Z","iopub.execute_input":"2022-08-11T19:31:22.008009Z","iopub.status.idle":"2022-08-11T19:31:22.018745Z","shell.execute_reply.started":"2022-08-11T19:31:22.007960Z","shell.execute_reply":"2022-08-11T19:31:22.017534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Let's plot the precision_recall_curve\ny_scores = cross_val_predict(sgd_clf, x_train, y_train_5, cv=3,\nmethod=\"decision_function\")\nfrom sklearn.metrics import precision_recall_curve\nprecisions, recalls, thresholds = precision_recall_curve(y_train_5, y_scores)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:22.020947Z","iopub.execute_input":"2022-08-11T19:31:22.021454Z","iopub.status.idle":"2022-08-11T19:31:35.332839Z","shell.execute_reply.started":"2022-08-11T19:31:22.021403Z","shell.execute_reply":"2022-08-11T19:31:35.330729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_precision_recall_vs_threshold(precisions, recalls, thresholds):\n    plt.plot(thresholds, precisions[:-1], \"b--\", label=\"Precision\")\n    plt.plot(thresholds, recalls[:-1], \"g-\", label=\"Recall\")\n    [...] # highlight the threshold, add the legend, axis label and grid\nplot_precision_recall_vs_threshold(precisions, recalls, thresholds)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:35.335331Z","iopub.execute_input":"2022-08-11T19:31:35.336431Z","iopub.status.idle":"2022-08-11T19:31:35.570175Z","shell.execute_reply.started":"2022-08-11T19:31:35.336358Z","shell.execute_reply":"2022-08-11T19:31:35.568842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Multiclass classifier","metadata":{}},{"cell_type":"markdown","source":"# 3-RandomForestClassifier","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\nRF=RandomForestClassifier(n_estimators=100)\nRF.fit(x_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:35.571770Z","iopub.execute_input":"2022-08-11T19:31:35.572115Z","iopub.status.idle":"2022-08-11T19:31:57.196020Z","shell.execute_reply.started":"2022-08-11T19:31:35.572083Z","shell.execute_reply":"2022-08-11T19:31:57.194642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"RF_pred=RF.predict(x_test)\nRF_pred\nprint(\"classification report \\n \\n\"+ classification_report(y_test, RF_pred))\nprint(\"confusion matrix \\n\")\nprint(confusion_matrix(y_test, RF_pred))","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:57.197494Z","iopub.execute_input":"2022-08-11T19:31:57.197887Z","iopub.status.idle":"2022-08-11T19:31:57.792216Z","shell.execute_reply.started":"2022-08-11T19:31:57.197851Z","shell.execute_reply":"2022-08-11T19:31:57.790927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RF Classifier has an accuracy of 96%","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:31:57.794606Z","iopub.execute_input":"2022-08-11T19:31:57.795096Z","iopub.status.idle":"2022-08-11T19:31:57.799952Z","shell.execute_reply.started":"2022-08-11T19:31:57.795048Z","shell.execute_reply":"2022-08-11T19:31:57.799070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = KNN_classifier.predict(test)\n\nprint('prediction: \\n', prediction)\nprediction.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:14:03.872459Z","iopub.execute_input":"2022-08-11T20:14:03.872892Z","iopub.status.idle":"2022-08-11T20:14:41.672245Z","shell.execute_reply.started":"2022-08-11T20:14:03.872857Z","shell.execute_reply":"2022-08-11T20:14:41.670922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = pd.Series(prediction, name='Label')\npred.head()\n\nindex_list = []\n\nfor i, item in enumerate(pred):\n    index_list.append(i+1)\n    \nimage_id = pd.Series(index_list, name='ImageId')","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:14:55.199120Z","iopub.execute_input":"2022-08-11T20:14:55.199841Z","iopub.status.idle":"2022-08-11T20:14:55.224702Z","shell.execute_reply.started":"2022-08-11T20:14:55.199802Z","shell.execute_reply":"2022-08-11T20:14:55.223225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.concat([image_id, pred], axis=1)\n\nsubmit.to_csv('submission.csv', index=False)\n\nsubmit.tail()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:15:01.114261Z","iopub.execute_input":"2022-08-11T20:15:01.114711Z","iopub.status.idle":"2022-08-11T20:15:01.160940Z","shell.execute_reply.started":"2022-08-11T20:15:01.114672Z","shell.execute_reply":"2022-08-11T20:15:01.159838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-11T20:15:08.707051Z","iopub.execute_input":"2022-08-11T20:15:08.707570Z","iopub.status.idle":"2022-08-11T20:15:08.716983Z","shell.execute_reply.started":"2022-08-11T20:15:08.707517Z","shell.execute_reply":"2022-08-11T20:15:08.715637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pd.read_csv('../input/digit-recognizer/sample_submission.csv').to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T19:59:43.791050Z","iopub.execute_input":"2022-08-11T19:59:43.791471Z","iopub.status.idle":"2022-08-11T19:59:43.797232Z","shell.execute_reply.started":"2022-08-11T19:59:43.791439Z","shell.execute_reply":"2022-08-11T19:59:43.796140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Thank You \nAmany Maayah","metadata":{}}]}