{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fyrirlestur 11\n",
    "\n",
    "Dagsetning: 23. mars\n",
    "\n",
    "Python uppsetning"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "%matplotlib inline\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Minnstu fervik\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "A = np.array([[1,1],[1,-1],[2,1]])\n",
    "b = np.array([4,2,6])\n",
    "\n",
    "#np.linalg.solve(A,b)\n",
    "x,res,rank,_ = np.linalg.lstsq(A,b) # lstsq skilar mörgum niðurstöðum\n",
    "print(\"Lausn = \",x)\n",
    "print(\"Fervik = \", res)\n",
    "print(\"Fjöldi óháðra dálka = \",rank)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Besta lína\n",
    "\n",
    "Búum til gögn fyrir línuna $f(x) = 2x+2$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "N = 20\n",
    "x = np.random.uniform(0,2,N)\n",
    "y = 2*x + 2 + np.random.normal(0.0,0.2,N) # random.normal bætir við smá suði"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.plot(x,y,'.')\n",
    "pz = np.polyfit(x,y,1) # 1-stigs margliða í gegnum punktana\n",
    "p = np.poly1d(pz)\n",
    "print(\"Jafna línu = \",p)\n",
    "xp = np.linspace(min(x),max(x),100)\n",
    "_= plt.plot(xp,p(xp))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Aðhvarfsgreining með margliðum\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "N = 20\n",
    "x = np.random.uniform(-3,3,N)\n",
    "y = 0.5*x**4 -0.4*x**3 - 4*x**2+x + 2 + np.random.normal(0.0,0.5,N)\n",
    "plt.plot(x,y,'.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for k in range(5):\n",
    "    pz = np.polyfit(x,y,k) # finnum k-ta stigs margliðu\n",
    "    p = np.poly1d(pz)\n",
    "    xp = np.linspace(min(x),max(x),100)\n",
    "    plt.plot(x,y,'.',xp,p(xp),'-') # plottum gögn vs margliðu\n",
    "    plt.show()\n",
    "    print(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for k in 6,9,15,20:\n",
    "    pz = np.polyfit(x,y,k) # finnum k-ta stigs margliðu\n",
    "    p = np.poly1d(pz)\n",
    "    xp = np.linspace(min(x),max(x),100)\n",
    "    plt.plot(x,y,'.',xp,p(xp),'-') # plottum gögn vs margliðu\n",
    "    plt.show()\n",
    "    print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Staðfesting\n",
    "Höldum eftir 20% af gögnunum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x1 = x[:-4]\n",
    "y1 = y[:-4]\n",
    "x2 = x[-4:]\n",
    "y2 = y[-4:]\n",
    "\n",
    "def rms(u):\n",
    "    return np.linalg.norm(u)/np.sqrt(len(u))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test_rms = []\n",
    "train_rms = []\n",
    "for k in range(15):\n",
    "    pz = np.polyfit(x1,y1,k) # finnum k-ta stigs margliðu\n",
    "    p = np.poly1d(pz)\n",
    "    train_rms.append(rms(y1 - p(x1)))\n",
    "    test_rms.append(rms(y2 - p(x2)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_rms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test_rms"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.plot(range(len(test_rms)), train_rms,'-',color='blue')\n",
    "plt.plot(range(len(train_rms)), np.clip(test_rms,0,4),'-',color='red')\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Af grafinu sést að 4-6. stigs margliða hefur besta spágildi"
   ]
  },
  {
   "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": 0
}
