{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Save and Load\n\nWe want to build a regression model and save it on the disk.\n\nThis regression model is an approximation of the following discipline\nwith two inputs and two outputs:\n\n- $y_1=1+2x_1+3x_2$\n- $y_2=-1-2x_1-3x_2$\n\nover the unit hypercube $[0,1]\\times[0,1]$.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from __future__ import division, unicode_literals\n\nfrom numpy import array\n\nfrom gemseo.api import (\n    configure_logger,\n    create_design_space,\n    create_discipline,\n    create_scenario,\n)\nfrom gemseo.mlearning.api import create_regression_model, import_regression_model\n\nconfigure_logger()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create the discipline to learn\nWe can implement this analytic discipline by means of the\n:class:`~gemseo.core.analytic_discipline.AnalyticDiscipline` class.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "expressions_dict = {\"y_1\": \"1+2*x_1+3*x_2\", \"y_2\": \"-1-2*x_1-3*x_2\"}\ndiscipline = create_discipline(\n    \"AnalyticDiscipline\", name=\"func\", expressions_dict=expressions_dict\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create the input sampling space\nWe create the input sampling space by adding the variables one by one.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "design_space = create_design_space()\ndesign_space.add_variable(\"x_1\", l_b=0.0, u_b=1.0)\ndesign_space.add_variable(\"x_2\", l_b=0.0, u_b=1.0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create the learning set\nWe can build a learning set by means of a\n:class:`~gemseo.core.doe_scenario.DOEScenario` with a full factorial design of\nexperiments. The number of samples can be equal to 9 for example.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "discipline.set_cache_policy(discipline.MEMORY_FULL_CACHE)\nscenario = create_scenario(\n    [discipline], \"DisciplinaryOpt\", \"y_1\", design_space, scenario_type=\"DOE\"\n)\nscenario.execute({\"algo\": \"fullfact\", \"n_samples\": 9})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Create the regression model\nThen, we build the linear regression model from the discipline cache and\ndisplays this model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "dataset = discipline.cache.export_to_dataset()\nmodel = create_regression_model(\"RBFRegression\", data=dataset)\nmodel.learn()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Use it for prediction\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "input_value = {\"x_1\": array([1.0]), \"x_2\": array([2.0])}\nprint(model.predict(input_value))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Save the regression model\nLastly, we save the model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "directory = model.save()\nprint(directory)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Load the regression model\nIn an other study, we could load this model.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "loaded_model = import_regression_model(directory)\nprint(loaded_model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Use the loaded regression model\nAnd use it!\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(loaded_model.predict(input_value))"
      ]
    }
  ],
  "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.8.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}