{"cells":[{"metadata":{"_uuid":"a3d5ed74d8634606a2b35ebc444e03ad2470e8eb"},"cell_type":"markdown","source":"## This is a solution for [digit-recognizer(MNIST)](http://https://www.kaggle.com/c/digit-recognizer) using scikitlearn's SVM, it is very easy to implement and it gives a not bad score of **0.97542** on the official dataset"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"scrolled":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\nfrom sklearn import svm\nimport matplotlib.pyplot as plt\n\nimport os\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"07ae6d41578b5b2bbc16b5d299320e21ae606e7a"},"cell_type":"code","source":"#load data\ndf = pd.read_csv(\"../input/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"76be3c5604b4c8fce247583eaf280064cca0d309"},"cell_type":"code","source":"# Take out label column\nlabels = df['label']\ndf = df.drop('label', 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db189828c35cb6fce222c2a2d8c60c4bf9c34bb4"},"cell_type":"code","source":"# Let's see how data looks like \nimage = df.values[123]\nplt.imshow(image.reshape(28, 28), cmap='Greys')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":false,"_uuid":"b92a007e53ddb8558072a7cdf15928101c6860e0"},"cell_type":"code","source":"# Split data\nx_train, x_test, y_train, y_test = train_test_split(df.values, labels, test_size = 0.3, random_state = 0)\n\nprint(x_train.shape)\nprint(x_test.shape)\n\nprint(y_train.shape)\nprint(y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"807aeaf7a100f45b150fd69eb490c85753545bef"},"cell_type":"code","source":"# Setup SVM model\nclf = svm.SVC(gamma=0.001, kernel='poly')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"b1066225e7be66b0447dbfb612db03ed8d5e0054"},"cell_type":"code","source":"#Train model\nclf.fit(x_train, y_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f84630cb9368f761c9cffcddbec1065ed4095c13"},"cell_type":"code","source":"# Predict\npredictions = clf.predict(x_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"0047c39dd7836241591a230d331e2209a7352ad2"},"cell_type":"code","source":"# evaluate predictions\nprint (\"Overal accuracy:\", accuracy_score(predictions, y_test))\nprint (classification_report(predictions, y_test))\nprint (confusion_matrix(predictions, y_test))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}