{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.ndimage # notum þetta fyrir myndir"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Notkun á k-means\n",
    "\n",
    "Í verkefninu megið þið nota fallið sem er gefið í kmeans.py eða innbyggða fallið `scipy.cluster.vq.kmeans` (https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.cluster.vq.kmeans.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from kmeans import kmeans # skráin kmeans.py þarf að vera í sömu möppu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = np.array(np.bmat([np.ones((3,10)), np.zeros((3,20))])) # dæmi um gögn,\n",
    "print(data.shape) #  30 stykki af 3-vigrum\n",
    "c,l,J = kmeans(data,2)\n",
    "print(c) # tveir vigrar af stærð 3\n",
    "print(l) # hópamerking fyrir 30 stök\n",
    "print(J) # J fallið"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fyrri hluti\n",
    "\n",
    "Notum scipy.ndimage.imread til að lesa myndir. Þetta skilar þrívíðu fylki `img[i][j][k]`, þar sem `i,j` er staðsetning pixels í hnitum og `k` er 0,1,2 fyrir R,G eða B hnit. Öll gildi eru á bilinu 0-255 og af taginu `uint8` þ.e. 8-bita heiltala"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "img = scipy.ndimage.imread('1.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "type(img)\n",
    "print(img.shape)\n",
    "print(img.ndim)\n",
    "print(img.dtype)\n",
    "print(img[0,0,:]) # efst í hægra horni, RGB gildi\n",
    "print(img[0,-1,:]) # neðst í vinstra horni"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Stundum þurfum við að vinna með myndirnar sem fleytitölur, þá getum við notað `np.asarray` eða `np.float` til að breyta fylkinu en myndirnar verða furðulegar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "p = img\n",
    "p = np.asarray(img,dtype='float')\n",
    "print(p.dtype)\n",
    "print(p.shape)\n",
    "plt.imshow(p,interpolation='nearest',vmin = 0, vmax = 255,)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "Þá þarf bara að kasta fylkinu yfir í heiltölur fyrir birtingu með `np.uint8`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.imshow(np.uint8(p),interpolation='nearest',vmin = 0, vmax = 255,)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Seinni hluti\n",
    "\n",
    "Til að ná í myndirnar þurfum við að hlaða inn fylkjunum. Myndunum hefur nú þegar verið breytt á fylkjaform svo að það er óþarfi að nota imread eins og að ofan."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A_images = np.load('A_images.npy')\n",
    "A_labels = np.load('A_labels.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(A_images.shape, A_images.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Við sjáum að A_images er þrívítt fylki en það er betra að vinna með það sem lista"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "A_imlist = [A_images[i,:,:] for i in range(A_images.shape[0])]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "len(A_imlist)\n",
    "A_imlist[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Til að vinna með hvern og einn tölustaf þarf að fletja út myndina, þ.e. breyta henni í 784-vigur.\n",
    "\n",
    "Fylkin sjálf, þ.e. 28x28 myndirnar er hægt að teikna á eftirfarandi hátt, takið eftir að á samsvarandi stað í `A_labels` er að finna rétta svarið."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.imshow(A_imlist[0],cmap=plt.get_cmap('gray'), interpolation='nearest', vmin=0,vmax=255)\n",
    "print(A_labels[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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": 2
}
