{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import cross_val_predict\nimport sklearn.base\nfrom sklearn.metrics import confusion_matrix, f1_score, precision_recall_curve, roc_curve, roc_auc_score\nfrom sklearn.linear_model import SGDClassifier\nfrom sklearn.datasets import fetch_openml\nfrom sklearn.ensemble import RandomForestClassifier","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#Gather data from the scikit-learn openml dataset and display the keys\nmnist = fetch_openml('mnist_784', version = 1)\nmnist.keys()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Separate the data from the target labels into two different variables\nX = mnist[\"data\"]\ny = mnist[\"target\"]\nprint(\"Training data shape:\", X.shape)\nprint(\"Target values shape:\", y.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Examine the first row of training data in a 28x28 image. \nex_train_val = X[0]\ntrain_val_img = ex_train_val.reshape(28, 28)\nplt.imshow(train_val_img, cmap = \"binary\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#View the corresponding label for the first row of training data\ny[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Convert the labels into integers and separate the data into training and test sets. \ny = y.astype(np.uint8)\nX_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Let's focus on one digit (1) and attempt to classify a binary responses as either 1 or not 1. \ny_train_1 = (y_train == 1)\ny_test_1 = (y_test == 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Using SGD, attempt fit a model which will then correctly classify the training data as either a 1 or not 1.\nsgd_cls = SGDClassifier(random_state = 1)\nsgd_cls.fit(X_train, y_train_1)\n\n#Now if we attempt to predict the first value in the training set we should return a false boolean since the first target is a 5.\nsgd_cls.predict([ex_train_val])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Using cross validation, evaluate the SGD Classifier on it ability to predict the nubmer 1. Visualize the  true and false negatives and positives in a confusion matrix\ny_train_pred = cross_val_predict(sgd_cls, X_train, y_train_1, cv=3)\nconfusion_matrix(y_train_1, y_train_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Evaluate the models precision and recall using the F score\nf1_score(y_train_1, y_train_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_scores = cross_val_predict(sgd_cls, X_train, y_train_1, cv=3, method=\"decision_function\")\nprecisions, recalls, thresholds = precision_recall_curve(y_train_1, y_scores)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot the precision and recall of the SGD model\ndef 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    plt.grid(which=\"both\",linestyle=\"--\")\n    plt.legend()\n\nplot_precision_recall_vs_threshold(precisions, recalls, thresholds)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Plot the ROC curve of the SGD Model\nfpr, tpr, thresholds = roc_curve(y_train_1, y_scores)\ndef plot_roc_curve(fpr, tpr, label=None):\n    plt.plot(fpr, tpr, linewidth=2, label=label)\n    plt.plot([0,1], [0,1], 'k--')\n    plt.grid(which=\"both\",linestyle=\"--\")\n    plt.legend()\n    \nplot_roc_curve(fpr, tpr)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"roc_auc_score(y_train_1, y_scores)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Compare the SGD Model to a Random Forest Model using ROC \nforest_cls = RandomForestClassifier(random_state=1)\ny_prob_forest = cross_val_predict(forest_cls, X_train, y_train_1, cv=3, method=\"predict_proba\")\n\ny_scores_forest = y_prob_forest[:,1]\nfpr_forest, tpr_forest, thresholds_forest = roc_curve(y_train_1, y_scores_forest)\n\nplt.plot(fpr, tpr, \"b:\",label=\"SGD\")\nplot_roc_curve(fpr_forest, tpr_forest, \"Random Forest\")\nplt.legend(loc=\"lower right\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"roc_auc_score(y_train_1, y_scores_forest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Train the Random Forest classifier and predict the outcomes of the test data\nrnd_frst = RandomForestClassifier()\nrnd_frst.fit(X_train, y_train)\noutput = rnd_frst.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Gather the results from the Random Forest Classifier and measure the accuracy of thee predictions\ni = 0 \ncorrect = 0\nincorrect = 0\nfor i in range(len(output)):\n    if output[i] == y_test[i]:\n        correct += 1\n    else:\n        incorrect += 1\n        \nprint(\"The accuracy of our model is:\", correct / (correct + incorrect))","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}