{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"975c570b-062e-1d6e-182c-d58b5cc0a7d9"},"outputs":[],"source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom sklearn.ensemble import BaggingClassifier\nfrom sklearn import cross_validation"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"7491456a-4077-3f3b-76c9-3777ce723721"},"outputs":[],"source":"%matplotlib inline"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"024a1310-4251-2383-6222-8727cc1f8db1"},"outputs":[],"source":"def showDigit (pixelArray):\n    pixelMatrix = pixelArray.reshape( (28, 28) )\n    plt.imshow(pixelMatrix, cmap='Greys')\n    plt.show()\n\ndef decisionTreeWithBagging(X, Y):\n    clf = BaggingClassifier(max_samples=0.5, max_features=0.4, n_estimators = 25)\n    clf = clf.fit(X, Y)\n    return clf\n\ndef prepareSubmission(name, clf, test):\n    prediction = clf.predict(test)\n    df = pd.DataFrame(prediction, columns = ['Label'])\n    df.index += 1 \n    df.to_csv(name, index_label = \"ImageId\")\n\ndef crossValidation(clf, X, Y):\n    scores = cross_validation.cross_val_score(clf, X, Y, cv=5)\n    print(\"Accuracy: %0.4f (+/- %0.4f)\" % (scores.mean(), scores.std() * 2))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"ea2474d4-75dc-cc94-137d-e79e36a79a0a"},"outputs":[],"source":"# load data\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"c6b3bb61-0bc8-22c9-b2da-4f9ee3585e89"},"outputs":[],"source":"# extract labels and features \nY = train.ix[:,0].as_matrix()\nX = train.ix[0:,1:].as_matrix()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"27300a08-bf57-f388-35c6-b90b241950cd"},"outputs":[],"source":"# prepare classifier\ntree = decisionTreeWithBagging(X, Y)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"09edf05f-3946-6cb1-560d-2d5cc9e1b177"},"outputs":[],"source":"# check accuracy\ncrossValidation(tree, X, Y)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"f9cfdb62-41eb-551c-736a-69246aa387ba"},"outputs":[],"source":"# prepareSubmission\nprepareSubmission('decisionTree.bagging.csv', tree, test)"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"96f1ef3f-e933-3c14-ca05-29f41c0a558d"},"outputs":[],"source":""}],"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}