{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"7cbe0911-040e-900e-39ae-37855518a5d8"},"source":"# Introduction\n\nFor this tutorial we use **Support Vector Classification.**.\n\nFirst of all, we'd like to say this is not the best aproach to solve digit recognition problem. But we choose that because it is simple and very useful for beginner machine learner.\n\nDespite the fact that SVC is not the best way to solve this problem, we could get a good results. You can see details about SVC at [sklearn webpage](http://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html)."},{"cell_type":"markdown","metadata":{"_cell_guid":"9990773e-c2a1-27b9-a7a3-187b32a7267d"},"source":"### Python imports"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"62f8bd12-ebb0-3450-1e5b-38fc0f71d9a8"},"outputs":[],"source":"import pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\n\nimport warnings\n\n# Trick for plotting inline in Jupter Notebook\n%matplotlib inline\n\n# Ignoring warnings\nwarnings.filterwarnings(\"ignore\")"},{"cell_type":"markdown","metadata":{"_cell_guid":"bf4ea207-da4d-0d5a-6280-a4304ae70ea1"},"source":"### Utilities method"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"198aa814-b7da-b8e4-e03b-699f8d42d9bc"},"outputs":[],"source":"def show_some_sample_images(dataset, k=5):\n    '''\n        Shows k random image samples from dataset.\n        \n        In the train dataset, there are 728 columns that represent the image.\n        We need to reshape this 728 x 1 array to 28 x 28, in order to plot the image correctly.\n        You can see it at line: \"img.reshape((28, 28))\"\n        \n        :param dataset: Pandas DataFrame\n        :param k: Number of images to be shown\n    '''\n    sample = dataset.sample(n=k)\n    for index in range(k):\n        img = sample.iloc[index].as_matrix()\n        img = img.reshape((28, 28))\n        plt.figure(figsize = (20,2))\n        plt.grid(False)\n        plt.axis('off')\n        plt.xticks([])\n        plt.yticks([])\n        plt.imshow(img)\n        plt.show()"},{"cell_type":"markdown","metadata":{"_cell_guid":"e759bb49-3892-755f-5056-f20960e56239"},"source":"### Loading the data"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3778d8cd-91a5-a710-32f5-7c140f3f93db"},"outputs":[],"source":"data = pd.read_csv('../input/train.csv')\ndata.head()"},{"cell_type":"markdown","metadata":{"_cell_guid":"5c0b6187-bbd8-a6d3-defd-979f4e25d8e1"},"source":"### Separating the dataset into images and labels\n\nAs we can see label is the first column of csv and the others columns are the pixels of digit image.\n\nNote that we are using only a few datapoints in order to save running time."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"6ae1ea62-3587-5696-e11b-ade9aa2092a6"},"outputs":[],"source":"labels = data.iloc[0:10000, :1]\nimages = data.iloc[0:10000, 1:]"},{"cell_type":"markdown","metadata":{"_cell_guid":"2ce3f8f2-1be7-8e3b-a8c1-9e57a70e0251"},"source":"### Showing some images from dataset"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"4a70efed-7443-bd0b-8338-112762ad65e5"},"outputs":[],"source":"show_some_sample_images(images)"},{"cell_type":"markdown","metadata":{"_cell_guid":"3513881a-065d-d656-b950-c2c86244bbae"},"source":"### Splitting the data into testing and training data points"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"07185ec5-329c-3207-211c-874da43f7d2c"},"outputs":[],"source":"train_images, test_images, train_labels, test_labels = train_test_split(images, labels, train_size=0.8, random_state=0)"},{"cell_type":"markdown","metadata":{"_cell_guid":"9bf6ad66-bf63-5c55-8ed3-335209bad6b8"},"source":"### Using SVC with sklearn\n\n[sklearn](http://scikit-learn.org) is a very simple tool as you can see bellow.\n\nIn the next cell we will just create a classifier and train with our training data points."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f8a67e88-0cec-537c-d2b6-71745dff25fa"},"outputs":[],"source":"clf = svm.SVC(kernel='linear')\nclf = clf.fit(train_images, train_labels.values.ravel())"},{"cell_type":"markdown","metadata":{"_cell_guid":"3b84b4ad-e99f-86e0-0b31-e867d6a2db4c"},"source":"### Validating the classifier\n\nNow we use the testing data points to validate and get a score for our aproach. The score we got is not too bad for a SVC classifier!"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"aef85252-29a1-2ba6-d934-3fad24ddf95d"},"outputs":[],"source":"print(clf.score(test_images, test_labels))"},{"cell_type":"markdown","metadata":{"_cell_guid":"f5902113-a4e0-b7e5-fe80-2d64bb61e871"},"source":"### Submission\n\nFor making competition submission we need to load and make predictions to unlabeled images from **test.csv**. "},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"9073ed18-930c-f308-8ccf-853c9d955a68"},"outputs":[],"source":"test_data=pd.read_csv('../input/test.csv')\nresults=clf.predict(test_data)\n\ntest_data['Label'] = pd.Series(results)\ntest_data['ImageId'] = test_data.index +1\nsub = test_data[['ImageId','Label']]\n\nsub.to_csv('submission.csv', index=False)"}],"metadata":{"_change_revision":0,"_is_fork":false,"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.6.0"}},"nbformat":4,"nbformat_minor":0}