{"cells":[
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "#Assignment 2\nAmy Rogers - 1211246"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "%matplotlib inline\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.cm as cm\n\n# The competition datafiles are in the directory ../input\n# Read competition data files:\ntraindata = pd.read_csv(\"../input/train.csv\")\n# remove label row\ntrain = traindata.iloc[:,1:]\ntest  = pd.read_csv(\"../input/test.csv\")\n\n# Write to the log:\nprint(\"Training set has {0[0]} rows and {0[1]} columns\".format(traindata.shape))\nprint(\"Test set has {0[0]} rows and {0[1]} columns\".format(test.shape))\n# Any files you write to the current directory get shown as outputs\n\n# reshape and change type of training data\ntrainnp = np.array(train).reshape((-1,1,28,28)).astype(float)\ntestnp = np.array(test).reshape((-1,1,28,28)).astype(np.int)\n\ntrainn = trainnp.copy()\ntestn = testnp.copy()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "#Feature Engineering\nUsing a combination of thresholding and skeletonising. Aim is to make the thickness of all digits\nin the data the same."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# images\nfrom skimage import data,io,filters,exposure\nfrom skimage.morphology import skeletonize\nfrom skimage.morphology import dilation\ntesti = 15\nthresh = filters.threshold_otsu(trainnp[testi][0])\nbinary = trainnp[testi][0] > (thresh)\ntrainn[testi][0] = dilation(skeletonize(binary))\n# plot the progress\nax1 = plt.subplot(1,3,1)\nax2 = plt.subplot(1,3,2)\nax3 = plt.subplot(1,3,3)\nax1.imshow(trainnp[testi][0], cmap=cm.binary)\nax1.set_title('Normal')\nax2.imshow(skeletonize(binary), cmap=cm.binary)\nax2.set_title('Skeleton')\nax3.imshow(trainn[testi][0], cmap=cm.binary)\nax3.set_title('Dilated')\nplt.show()    \n#reshape skeletondata\ntrainna = trainn.reshape(-1,784).astype(np.uint)\ntestna = testn.reshape(-1,784).astype(np.uint)    "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# images\nfrom skimage import data,io,filters,exposure\nfrom skimage.morphology import skeletonize\nfrom skimage.morphology import dilation\n\ntesti = 0\n\nthresh = filters.threshold_otsu(trainnp[testi][0])\nbinary = trainnp[testi][0] > (thresh)\ntrainn[testi][0] = dilation(skeletonize(binary))\n\n# plot the progress\nax1 = plt.subplot(1,3,1)\nax2 = plt.subplot(1,3,2)\nax3 = plt.subplot(1,3,3)\nax1.imshow(trainnp[testi][0], cmap=cm.binary)\nax1.set_title('Normal')\nax2.imshow(binary, cmap=cm.binary)\nax2.set_title('Altered')\nax3.imshow(trainn[testi][0], cmap=cm.binary)\nax3.set_title('Dilated')\nplt.show()    \n\n#reshape skeletondata\ntrainna = trainn.reshape(-1,784).astype(np.uint)\ntestna = testn.reshape(-1,784).astype(np.uint)\n\n    \nprint('Done!')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# images\nfrom skimage import data,io,filters\nfrom skimage.morphology import skeletonize, dilation\n\n# skeletonise all training data\n#0 to 42000\nfor ind in range(0,28000):\n    thresh = filters.threshold_otsu(trainnp[ind][0])\n    binary = trainnp[ind][0] > (thresh)\n    trainn[ind][0] = dilation(skeletonize(binary))\n    thresh = filters.threshold_otsu(testnp[ind][0])\n    binary = testnp[ind][0] > (thresh)\n    testn[ind][0] = dilation(skeletonize(binary))\n    \nfor ind in range(28001,42000):\n    thresh = filters.threshold_otsu(trainnp[ind][0])\n    binary = trainnp[ind][0] > (thresh)\n    trainn[ind][0] = dilation(skeletonize(binary))\n    \n#reshape skeletondata\ntrainna = trainn.reshape(-1,784).astype(np.uint)\ntestna = testn.reshape(-1,784).astype(np.uint)\n    \nprint('Done!')"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# plot the progress\nax1 = plt.subplot(1,3,1)\nax2 = plt.subplot(1,3,2)\nax1.imshow(trainnp[231][0], cmap=cm.binary)\nax1.set_title('Default')\nax2.imshow(trainn[231][0], cmap=cm.binary)\nax2.set_title('Skeleton')\nplt.show()"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# Machine Learning Methods\nMethods, results, and analysis."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "from sklearn.ensemble import RandomForestClassifier as rf\nfrom sklearn.ensemble import AdaBoostClassifier as adaBoost\nfrom sklearn.tree import DecisionTreeClassifier as dt\nfrom sklearn.ensemble import BaggingClassifier as bagging\nfrom sklearn.ensemble import VotingClassifier as vc\nfrom sklearn import svm\n\nX = pd.DataFrame(trainna)\ny = traindata.loc[:,'label']\nt = pd.DataFrame(testna)\n\nprint(y)\n\nrfc = rf(n_estimators=200)\nsvmlin = svm.LinearSVC(loss = 'hinge', penalty = 'l2')\nbagSVM = bagging(svmlin,n_estimators=500,bootstrap=True, max_samples = 0.6, max_features=0.6)\ndecisionStump = dt(criterion=\"entropy\",max_depth=1)\nboost = adaBoost(base_estimator = decisionStump,n_estimators = 1000)\n\nvc1 = vc(estimators=[('RFC', rfc),('Bagged',bagSVM),('AdaBoost',boost)], voting='soft', weights=[1,2,2])#,('SVC',svc)])\nvc1.fit(X,y)\nprint(vc1.predict(t))\n\nvc1.fit(X.iloc[0:1000],y.iloc[0:1000])\nprint(vc1.score(X.iloc[1001:2000],y.iloc[1001:2000]))\n\n\n#print(vc1.predict(t.iloc[0:1000]))\n\nvc1 = vc(estimators=[('RFC', rfc),('Bagged',bagSVM),('AdaBoost',boost)], voting='soft', weights=[2,1,1])#,('SVC',svc)])\nvc1.fit(X.iloc[0:1000],y.iloc[0:1000])\nprint(vc1.score(X.iloc[1001:2000],y.iloc[1001:2000]))\n#print(vc1.predict(t.iloc[0:1000]))\n\n#pd.Series(vc1.predict(t.iloc[0:1000])).to_csv(\"./skeletonbenchmark.csv\")"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "from sklearn.ensemble import RandomForestClassifier as rf\nfrom sklearn.ensemble import AdaBoostClassifier as adaBoost\nfrom sklearn.tree import DecisionTreeClassifier as dt\nfrom sklearn.ensemble import BaggingClassifier as bagging\nfrom sklearn.ensemble import VotingClassifier as vc\nfrom sklearn import svm\n\ndebug = 27999\n\n# random forest\nxvalues = [100,200,300,400,500,600,700,800]\nyvalues = []\n\nfor a in xvalues:\n    rfc = rf(n_estimators=100)\n    rfc.fit(X,y)\n    yvalues.append(rfc.score(X,y))\n    \nplt.plot(xvalues,yvalues)\n    \n#print(rfc.predict(t.iloc[0:debug]))\n#rfc.fit(X,y)\n#pd.Series(rfc.predict(t)).to_csv(\"./skeletonbenchmark.csv\")\n\n#svm\nsvmlin = svm.LinearSVC(loss = 'hinge', penalty = 'l2')\nbagSVM = bagging(svmlin,n_estimators=10,bootstrap=True, max_samples = 0.6, max_features=0.6)\n#bagSVM.fit(X,y)\n#print(bagSVM.predict(t.iloc[0:debug]))\n\ndecisionStump = dt(criterion=\"entropy\",max_depth=1)\nboost = adaBoost(base_estimator = decisionStump,n_estimators = 80)\n#boost.fit(X.iloc[0:debug],y.iloc[0:debug])\n#print(boost.predict(t.iloc[0:debug]))\n\n\n#print(ada.predict(t))\n#svc = SVC()\n\nvc1 = vc(estimators=[('RFC', rfc),('Bagged',bagSVM),('AdaBoost',boost)])#,('SVC',svc)])\n#print('Voting classifier declared')\n\nvc1.fit(X,y)"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}