{"metadata":{"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.5.2"}},"nbformat":4,"nbformat_minor":0,"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"28e5c2c3-a40e-6fff-6da2-55c8c9019b3d","_active":false},"source":"## Codemash 2017 - Precompiler \"Unleash the computer on your data\"\n### Amber McKenzie\n### January 10, 2017","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":18,"metadata":{"_cell_guid":"dd849149-0dab-8678-9814-7f384508cd97","_active":false},"outputs":[],"source":"from sklearn import svm, metrics\nfrom sklearn.neighbors import KNeighborsClassifier\nimport sklearn.model_selection as ms\nimport pandas as pd","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"bd1ae2a3-1f7a-ac70-5a32-801cd5b4e1cf","_active":false},"source":"## Importing and formatting data","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"b4f16369-2d3b-3725-bb12-6451befef5df","_active":false},"source":"### Importing data","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":19,"metadata":{"_cell_guid":"541abd40-c255-97aa-3d03-51cf0a17b60f","_active":false},"outputs":[],"source":"df = pd.read_csv(\"../input/train.csv\")\ndf_test = pd.read_csv(\"../input/test.csv\")\ndf.head()","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"c90f2ed0-c496-75e5-4147-6a27160d9752","_active":false},"source":"### Subsetting data to train faster\nDon't run this cell if you want to train on all the data.","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":20,"metadata":{"_cell_guid":"d9d445e9-c227-d221-8a5e-6fb2bc1da6fb","_active":false},"outputs":[],"source":"df = df.sample(frac=1)\ndf = df.head(5000)","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"47e106f4-df7c-51d6-30c0-279c2c7aea4e","_active":false},"source":"### Creating data and labels","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":21,"metadata":{"_cell_guid":"5c56b10f-4632-b9b4-bf16-94d77e112c31","_active":false,"collapsed":false},"outputs":[],"source":"x = df.drop(\"label\", axis=1) # x is the training data (feature set)\ny = df[\"label\"]              # y is answer\ntest = df_test.as_matrix()","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"0a19839e-5e33-5161-810b-186aa51d9ddf","_active":false},"source":"### Break into train and validation sets","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":23,"metadata":{"_cell_guid":"e7373a78-026d-f262-f16d-f67cffc43c08","_active":true,"collapsed":false},"outputs":[],"source":"x_train, x_val, y_train, y_val = ms.train_test_split(x, y, test_size=0.2, random_state=0)\n\nx_train.head()","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"90da472a-2a6a-fb6e-bbc7-18dd78e9903b","_active":false},"source":"## Model building","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"236546d4-e24a-b431-09a7-c9fed0373593","_active":false},"source":"### Train KNearestNeighbors model","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":24,"metadata":{"_cell_guid":"1d937abf-299c-bbb5-4546-7321cb409fda","_active":false},"outputs":[],"source":"classifier = KNeighborsClassifier()\nclassifier.fit(x_train, y_train)","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"13bc21a7-66da-8ba1-f345-2b4d23ae4b3f","_active":false},"source":"### Evaluate model","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":25,"metadata":{"_cell_guid":"03c60441-b54c-85e2-c156-fc4c7a55c0a0","_active":false},"outputs":[],"source":"predicted = classifier.predict(x_val)\nprint (metrics.classification_report(predicted, y_val))","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"04f1f30b-b87f-cd1a-2e3f-162e4ee3116d","_active":false},"source":"### Train and evaluate support vector machine (SVM)","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":26,"metadata":{"_cell_guid":"4d0d5a9e-eea1-4aac-e469-2ad089a50601","_active":false,"collapsed":false},"outputs":[],"source":"classifier = svm.SVC(gamma=0.001, kernel='linear')\n#Todo: put in lines to train model, classify validation set, and print out metrics\nclassifier.fit(x_train, y_train)\npredicted = classifier.predict(x_val)\nprint (metrics.classification_report(predicted, y_val))\n\n","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"563f432b-cc6a-ab69-e64d-a53884e14981","_active":false},"source":"### Perform cross validation of the svm model","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":27,"metadata":{"_cell_guid":"0b665e85-d2ec-05ac-e1fb-37aef4d3215d","_active":false},"outputs":[],"source":"from sklearn.model_selection import cross_val_predict\n\npreds = cross_val_predict(classifier, X=x, y=y, cv=3)\nprint (metrics.classification_report(y, preds, digits=4))\nprint (metrics.confusion_matrix(y, preds))","execution_state":"idle"},{"cell_type":"markdown","metadata":{"_cell_guid":"2e7d8b60-f463-d19a-4642-569c60b395c1","_active":false},"source":"## Write submission files\nOutput path may need to be modified before running this cell.","execution_count":null,"outputs":[],"execution_state":"idle"},{"cell_type":"code","execution_count":28,"metadata":{"_cell_guid":"3cc244d5-15ff-ab72-7997-9441f0879c47","_active":false},"outputs":[],"source":"predict_test = classifier.predict(test)\nout = zip(range(len(test)), predict_test)\nwith open(\"./solution.csv\", 'w') as g:\n    g.write(\"ImageId,Label\\n\")\n    for id, cat in out:\n        g.write(str(id + 1) + \",\" + str(cat) + \"\\n\")","execution_state":"idle"}]}