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