{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1 style='color: green; font-size: 36px; font-weight: bold;'>Data Science - Regressão Linear</h1>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Bônus</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importando nosso modelo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$ 46389.80\n"
     ]
    }
   ],
   "source": [
    "import pickle\n",
    "\n",
    "modelo = open('../Exercício/modelo_preço','rb')\n",
    "lm_new = pickle.load(modelo)\n",
    "modelo.close()\n",
    "\n",
    "area = 38\n",
    "garagem = 2\n",
    "banheiros = 4\n",
    "lareira = 4\n",
    "marmore = 0\n",
    "andares = 1\n",
    "\n",
    "entrada = [[area, garagem, banheiros, lareira, marmore, andares]]\n",
    "\n",
    "print('$ {0:.2f}'.format(lm_new.predict(entrada)[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exemplo de um simulador interativo para Jupyter\n",
    "\n",
    "https://ipywidgets.readthedocs.io/en/stable/index.html\n",
    "\n",
    "https://github.com/jupyter-widgets/ipywidgets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importando bibliotecas\n",
    "from ipywidgets import widgets, HBox, VBox\n",
    "from IPython.display import display\n",
    "\n",
    "# Criando os controles do formulário\n",
    "area = widgets.Text(description=\"Área\")\n",
    "garagem = widgets.Text(description=\"Garagem\")\n",
    "banheiros = widgets.Text(description=\"Banheiros\")\n",
    "lareira = widgets.Text(description=\"Lareira\")\n",
    "marmore = widgets.Text(description=\"Mármore?\")\n",
    "andares = widgets.Text(description=\"Andares?\")\n",
    "\n",
    "botao = widgets.Button(description=\"Simular\")\n",
    "\n",
    "# Posicionando os controles\n",
    "left = VBox([area, banheiros, marmore])\n",
    "right = VBox([garagem, lareira, andares])\n",
    "inputs = HBox([left, right])\n",
    "\n",
    "# Função de simulação\n",
    "def simulador(sender):\n",
    "    entrada=[[\n",
    "                float(area.value if area.value else 0), \n",
    "                float(garagem.value if garagem.value else 0), \n",
    "                float(banheiros.value if banheiros.value else 0), \n",
    "                float(lareira.value if lareira.value else 0), \n",
    "                float(marmore.value if marmore.value else 0), \n",
    "                float(andares.value if andares.value else 0)\n",
    "             ]]\n",
    "    print('$ {0:.2f}'.format(lm_new.predict(entrada)[0]))\n",
    "    \n",
    "# Atribuindo a função \"simulador\" ao evento click do botão\n",
    "botao.on_click(simulador)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e536e3917f8945b89daa698ddfe43c3f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(Text(value='38', description='Área'), Text(value='4', description='Banheiros'), …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7254f99544e548b0a6cc905d6a97e331",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Button(description='Simular', style=ButtonStyle())"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$ 46389.80\n"
     ]
    }
   ],
   "source": [
    "display(inputs, botao)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
