{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Multi-output Operator-valued kernel Regression\n\n\nAn example to illustrate multi-output regression with operator-valued kernels.\n\nWe compare Operator-valued kernel (OVK)\n- without prior (ID),\n- with covariance constraint prior (covariance),\n- with multioutput decision tree.\n\nSetting the operator A as a good prior (here a covariance constraint) helps to\nachieve a better score on a noisy test dataset. OVK methods can generalise\nbetter than decision trees but are slower to train.\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Author: Romain Brault <romain.brault@telecom-paristech.fr> with help from\n#         the scikit-learn community.\n# License: MIT\n\nimport operalib as ovk\nfrom sklearn.tree import DecisionTreeRegressor\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport time\n\nprint(__doc__)\n\nseed = 123\nnp.random.seed(seed)\nprint(\"seed\", seed)\n\n# Create a random dataset\nprint(\"Creating dataset...\")\nX = 200 * np.random.rand(1000, 1) - 100\ny = np.array([np.pi * np.sin(X).ravel(), np.pi * np.cos(X).ravel()]).T\nTr = 2 * np.random.rand(2, 2) - 1\nTr = np.dot(Tr, Tr.T)\nTr = Tr / np.linalg.norm(Tr, 2)\nU = np.linalg.cholesky(Tr)\ny = np.dot(y, U)\n\n# Add some noise\nSigma = 2 * np.random.rand(2, 2) - 1\nSigma = np.dot(Sigma, Sigma.T)\nSigma = 1. * Sigma / np.linalg.norm(Sigma, 2)\nCov = np.linalg.cholesky(Sigma)\ny += np.dot(np.random.randn(y.shape[0], y.shape[1]), Cov)\n\n# Fit\n# real period is 2 * pi \\approx 6.28, but we set the period to 6 for\n# demonstration purpose\nprint(\"Fitting...\")\nstart = time.time()\nA = np.eye(2)\nregr_1 = ovk.Ridge('DPeriodic', lbda=0.01, period=6, theta=.995, A=A)\nregr_1.fit(X, y)\nprint(\"Leaning time DPeriodic ID: \", time.time() - start)\n\nstart = time.time()\nA = np.cov(y.T)\nregr_2 = ovk.Ridge('DPeriodic', lbda=0.01, period=6, theta=.995, A=A)\nregr_2.fit(X, y)\nprint(\"Leaning time DPeriodic covariance: \", time.time() - start)\n\nstart = time.time()\nregr_3 = DecisionTreeRegressor(max_depth=100)\nregr_3.fit(X, y)\nprint(\"Leaning time Trees: \", time.time() - start)\n\n\n# Predict\nprint(\"Predicting...\")\nX_test = np.arange(-100.0, 100.0, .5)[:, np.newaxis]\nstart = time.time()\ny_1 = regr_1.predict(X_test)\nprint(\"Test time DPeriodic ID: \", time.time() - start)\nstart = time.time()\n# regr_2.linop_.A = np.eye(2)\ny_2 = regr_2.predict(X_test)\nprint(\"Test time DPeriodic covariance: \", time.time() - start)\nstart = time.time()\ny_3 = regr_3.predict(X_test)\nprint(\"Test time Trees: \", time.time() - start)\n\n# Ground truth\ny_t = np.dot(np.array([np.pi * np.sin(X_test).ravel(),\n                       np.pi * np.cos(X_test).ravel()]).T, U)\n\n# Plot\nprint(\"plotting\")\nplt.figure()\nplt.scatter(y_1[::1, 0], y_1[::1, 1], c=\"g\", label=\"OVK Id\", s=5., lw=0)\nplt.scatter(y_2[::1, 0], y_2[::1, 1], c=\"b\", label=\"OVK Cov\", s=5., lw=0)\nplt.scatter(y_3[::1, 0], y_3[::1, 1], c=\"r\", label=\"D TREE\", s=5., lw=0)\n# plt.scatter(y[::1, 0], y[::1, 1], c=\"k\", label=\"Data\", s=5., lw = 0)\nplt.scatter(y_t[::1, 0], y_t[::1, 1], c=\"k\", label=\"True\", s=5., lw=0)\nplt.xlim([-4, 4])\nplt.ylim([-4, 4])\nplt.xlabel(\"data\")\nplt.ylabel(\"target\")\nplt.title(\"Multi-output OVK Regression\")\nplt.legend()\nplt.show()\n\nprint(\"Done\")"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }
  ], 
  "metadata": {
    "kernelspec": {
      "display_name": "Python 2", 
      "name": "python2", 
      "language": "python"
    }, 
    "language_info": {
      "mimetype": "text/x-python", 
      "nbconvert_exporter": "python", 
      "name": "python", 
      "file_extension": ".py", 
      "version": "2.7.11", 
      "pygments_lexer": "ipython2", 
      "codemirror_mode": {
        "version": 2, 
        "name": "ipython"
      }
    }
  }
}