{"cells":[
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "%matplotlib inline"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# The competition datafiles are in the directory ../input\n# Read competition data files:\ntrain = pd.read_csv(\"../input/train.csv\")\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(train.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"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# Data\nAfter loading the data, store it in the following format:\n\n**Labels** (vector of length 42000)\n\n**Training Data** (matrix of shape 42000,784; the labels are sliced off)\n\nEach handwritten digit used for training is stored as a row in a matrix. \nEach row is 784 points long; this represents\npixel intensities of an image which is 28 by 28 pixels. The row must be reshaped in order to visualize it.\n\n**Weights** (list of vectors, one vector for each class. 10 by 784)\n\nEach weight vector is the same length as a handwritten digit vector. "
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "# Store the labels in a vector\ntrainlabels = train.label\n\n# Store the data in a matrix (without labels)\ntraindata = np.asmatrix(train.ix[:,'pixel0':])\ntraindata.shape\n\n# Visualize a single digit (one row from the training data matrix)\nsamplerow = traindata[3:4]#get one row from training data\nsamplerow = np.reshape(samplerow,(28,28))#reshape it to a 28*28 grid\nprint(\"A sample digit from the dataset:\")\nplt.imshow(samplerow, cmap=\"hot\")\n\n# Initialize the weight matrix (one row per class)\nweights = np.zeros((10,784))\nprint(weights.shape)"
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# Perceptron Learning (training)\nFor each handwritten digit in the data set, compare its vector to each of the weight \nvectors by taking the dot product. \n\nThe largest resulting dot product is the 'guess'.\nIf the guess is wrong, the incorrect weight vector is updated by subtracting the\ndigit vector and the correct weight vector is updated by adding the digit vector.\n\nThis routine repeats through the dataset many times (epochs) until the number of wrong guesses\nbegins to stabilize (in this case, 15 epochs. If you were to run 20 epochs, you'd see overfitting)."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "markdown",
  "metadata": {},
  "source": "# Perceptron Testing\nCreate a csv file to upload as submission."
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": ""
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "testdata = np.asmatrix(test)\nguesses = []\nfor i, z in enumerate(testdata):\n    dp=[] #create a new container for the calculated dot products\n    # For each digit class (0-9)\n    for w in weights:\n        dotproduct = np.dot(z,w) #take the dot product of the weight and the data\n        dp.append(dotproduct) #add the dot product to the list of dot products\n    guess = np.argmax(dp) #take the largest dot product and make that the guessed digit class\n    guesses.append(guess)"
 },
 {
  "cell_type": "code",
  "execution_count": null,
  "metadata": {
   "collapsed": false
  },
  "outputs": [],
  "source": "f = open('percep1.csv', 'w')\ni = 1\nf.write('ImageId,Label\\n')\nfor g in guesses:\n    foo = str(i) + ',' + str(g) + '\\n'\n    f.write(foo)\n    i += 1\nf.close()\nprint(i)"
 }
],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}}, "nbformat": 4, "nbformat_minor": 0}