{"cells":[{"metadata":{},"cell_type":"markdown","source":"> Notebook Updated\n\n# Author: Kazi Amit Hasan\n\nDepartment of Computer Science & Engineering, <br>\nRajshahi University of Engineering & Technology (RUET) <br>\nWebsite: https://amithasanshuvo.github.io/ <br>\nLinkedin: https://www.linkedin.com/in/kazi-amit-hasan-514443140/ <br>\nEmail: kaziamithasan89@gmail.com <br>\n\n\n### Comment: \nThis notebook represents the chapter three(3) of Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems book. I tried to implemented each examples while I was reading and practing this book. <br>\n<b>Please give your feedback how I can improve the notebook. Happy Learning!\n\n\n##### Reference \nHands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems book (The best book I ever got on Data Sciene <3) <br>\n\nLanguage: Python\n\n# Plese upvote if you like it.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"### Loading the dataset","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ntrain = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = (train.iloc[:,1:].values).astype('float32') # all pixel values\ny_train = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits\nX_test = test.iloc[:,1:].values.astype('float32')\ny_test = test.iloc[:,0:].values.astype('int32')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print (\"X_train: \", X_train)\nprint (\"y_train: \", y_train)\nprint(\"X_test: \", X_test)\nprint (\"y_test: \", y_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#X_train = X_train.reshape(X_train.shape[0], 28, 28,1)\n#X_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#X_test = X_test.reshape(X_test.shape[0], 28, 28,1)\n#X_test.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Making it a Binary classification problem by making it \"5-detector\" problem\n### It will distinguish between two classes. 5 or not! Sounds cool!","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train_5 = (y_train ==5)\ny_test_5 = (y_test ==5)\n\n# This means true for all 5's and false for others","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Now we have to pick a classifier and train it.\n\n# Stochastic Gradient Descent (it can handle big datasets every efficiently)\n\nfrom sklearn.linear_model import SGDClassifier\nsgd_clf = SGDClassifier( random_state=42)\nsgd_clf.fit(X_train,y_train_5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Performance Measure with k fold cross validation with three folds\nfrom sklearn.model_selection import cross_val_score\ncross_val_score(sgd_clf, X_train, y_train_5, cv=3, scoring=\"accuracy\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Accuracy isn't always the base of performance measure","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# A classifier that only images that are in 'not 5' class.\n\nfrom sklearn.base import BaseEstimator\nclass Never5Classifier(BaseEstimator):\n    def fit(self, X, y=None):\n        pass\n    def predict(self, X):\n        return np.zeros((len(X), 1), dtype=bool)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"never_5_clf = Never5Classifier()\ncross_val_score(never_5_clf, X_train, y_train_5, cv=3, scoring=\"accuracy\")\n\n# Good accuracy but only 10% images are 5's. So if that image is not a 5, then we are right about 90% time.\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Confusion Matrix","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nconfusion_matrix (y_train_5,y_train_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ny_train_perfect_predictions = y_train_5  \n# pretend we reached perfection\nconfusion_matrix(y_train_5, y_train_perfect_predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import precision_score, recall_score\n\nprecision_score(y_train_5, y_train_pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"recall_score(y_train_5, y_train_pred)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nfrom sklearn.metrics import f1_score\n\nf1_score(y_train_5, y_train_pred)","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":1}