{
 "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;'>Conhecendo o Dataset</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importando bibliotecas\n",
    "\n",
    "https://matplotlib.org/\n",
    "\n",
    "https://pandas.pydata.org/\n",
    "\n",
    "http://www.numpy.org/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bibliotecas opcionais\n",
    "\n",
    "https://docs.python.org/3/library/warnings.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "\n",
    "warnings.filterwarnings('ignore') # ou warnings.filterwarnings(action='once')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## O Dataset e o Projeto\n",
    "<hr>\n",
    "\n",
    "### Fonte: https://www.kaggle.com/dongeorge/beer-consumption-sao-paulo\n",
    "\n",
    "### Descrição:\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>A cerveja é uma das bebidas mais democráticas e consumidas no mundo. Não sem razão, é perfeito para quase todas as situações, desde o happy hour até grandes festas de casamento.</p>\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>O objetivo deste treinamento será estimar um modelo de <b>Machine Learning</b> utilizando a técnica de <b>Regressão Linear</b> para demonstrar os impactos das variáveis disponibilizadas neste dataset sobre o consumo de cerveja (Y). No final do projeto teremos um modelo de previsão para o consumo médio de cerveja segundo os inputs de um conjunto de variáveis (X's).</p>\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>Os dados (amostra) foram coletados em São Paulo - Brasil, em uma área universitária, onde existem algumas festas com grupos de alunos de 18 a 28 anos de idade (média).</p>\n",
    "\n",
    "### Dados:\n",
    "<ul style='font-size: 18px; line-height: 2; text-align: justify;'>\n",
    "    <li><b>data</b> - Data</li>\n",
    "    <li><b>temp_media</b> - Temperatura Média (°C)</li>\n",
    "    <li><b>temp_min</b> - Temperatura Mínima (°C)</li>\n",
    "    <li><b>temp_max</b> - Temperatura Máxima (°C)</li>\n",
    "    <li><b>chuva</b> - Precipitação (mm)</li>\n",
    "    <li><b>fds</b> - Final de Semana (1 = Sim; 0 = Não)</li>\n",
    "    <li><b>consumo</b> - Consumo de Cerveja (litros)</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Leitura dos dados"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dados = pd.read_csv('../Dados/Consumo_cerveja.csv', sep=';')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualizar os dados"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>data</th>\n",
       "      <th>temp_media</th>\n",
       "      <th>temp_min</th>\n",
       "      <th>temp_max</th>\n",
       "      <th>chuva</th>\n",
       "      <th>fds</th>\n",
       "      <th>consumo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>01/01/2015</td>\n",
       "      <td>27.30</td>\n",
       "      <td>23.9</td>\n",
       "      <td>32.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>25461</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>02/01/2015</td>\n",
       "      <td>27.02</td>\n",
       "      <td>24.5</td>\n",
       "      <td>33.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>03/01/2015</td>\n",
       "      <td>24.82</td>\n",
       "      <td>22.4</td>\n",
       "      <td>29.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>30814</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>04/01/2015</td>\n",
       "      <td>23.98</td>\n",
       "      <td>21.5</td>\n",
       "      <td>28.6</td>\n",
       "      <td>1.2</td>\n",
       "      <td>1</td>\n",
       "      <td>29799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>05/01/2015</td>\n",
       "      <td>23.82</td>\n",
       "      <td>21.0</td>\n",
       "      <td>28.3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>06/01/2015</td>\n",
       "      <td>23.78</td>\n",
       "      <td>20.1</td>\n",
       "      <td>30.5</td>\n",
       "      <td>12.2</td>\n",
       "      <td>0</td>\n",
       "      <td>28218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>07/01/2015</td>\n",
       "      <td>24.00</td>\n",
       "      <td>19.5</td>\n",
       "      <td>33.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>29732</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>08/01/2015</td>\n",
       "      <td>24.90</td>\n",
       "      <td>19.5</td>\n",
       "      <td>32.8</td>\n",
       "      <td>48.6</td>\n",
       "      <td>0</td>\n",
       "      <td>28397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>09/01/2015</td>\n",
       "      <td>28.20</td>\n",
       "      <td>21.9</td>\n",
       "      <td>34.0</td>\n",
       "      <td>4.4</td>\n",
       "      <td>0</td>\n",
       "      <td>24886</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10/01/2015</td>\n",
       "      <td>26.76</td>\n",
       "      <td>22.1</td>\n",
       "      <td>34.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>37937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>11/01/2015</td>\n",
       "      <td>27.62</td>\n",
       "      <td>22.2</td>\n",
       "      <td>34.8</td>\n",
       "      <td>3.4</td>\n",
       "      <td>1</td>\n",
       "      <td>36254</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>12/01/2015</td>\n",
       "      <td>25.96</td>\n",
       "      <td>21.4</td>\n",
       "      <td>35.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>25743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>13/01/2015</td>\n",
       "      <td>25.52</td>\n",
       "      <td>21.2</td>\n",
       "      <td>34.8</td>\n",
       "      <td>0.8</td>\n",
       "      <td>0</td>\n",
       "      <td>26990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>14/01/2015</td>\n",
       "      <td>25.96</td>\n",
       "      <td>21.3</td>\n",
       "      <td>34.0</td>\n",
       "      <td>1.6</td>\n",
       "      <td>0</td>\n",
       "      <td>31825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>15/01/2015</td>\n",
       "      <td>25.86</td>\n",
       "      <td>21.5</td>\n",
       "      <td>32.8</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0</td>\n",
       "      <td>25724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16/01/2015</td>\n",
       "      <td>26.50</td>\n",
       "      <td>22.3</td>\n",
       "      <td>32.7</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0</td>\n",
       "      <td>29938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>17/01/2015</td>\n",
       "      <td>28.86</td>\n",
       "      <td>22.0</td>\n",
       "      <td>35.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>37690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18/01/2015</td>\n",
       "      <td>28.26</td>\n",
       "      <td>23.4</td>\n",
       "      <td>35.6</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>30524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>19/01/2015</td>\n",
       "      <td>28.22</td>\n",
       "      <td>22.7</td>\n",
       "      <td>36.5</td>\n",
       "      <td>3.7</td>\n",
       "      <td>0</td>\n",
       "      <td>29265</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20/01/2015</td>\n",
       "      <td>27.68</td>\n",
       "      <td>23.3</td>\n",
       "      <td>35.6</td>\n",
       "      <td>0.6</td>\n",
       "      <td>0</td>\n",
       "      <td>35127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>21/01/2015</td>\n",
       "      <td>25.32</td>\n",
       "      <td>22.7</td>\n",
       "      <td>30.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>29130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>22/01/2015</td>\n",
       "      <td>21.74</td>\n",
       "      <td>19.2</td>\n",
       "      <td>26.1</td>\n",
       "      <td>31.0</td>\n",
       "      <td>0</td>\n",
       "      <td>25795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>23/01/2015</td>\n",
       "      <td>21.04</td>\n",
       "      <td>18.5</td>\n",
       "      <td>26.1</td>\n",
       "      <td>3.6</td>\n",
       "      <td>0</td>\n",
       "      <td>21784</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>24/01/2015</td>\n",
       "      <td>23.12</td>\n",
       "      <td>19.0</td>\n",
       "      <td>29.4</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1</td>\n",
       "      <td>28348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>25/01/2015</td>\n",
       "      <td>24.40</td>\n",
       "      <td>18.1</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>31088</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>26/01/2015</td>\n",
       "      <td>22.40</td>\n",
       "      <td>18.9</td>\n",
       "      <td>28.7</td>\n",
       "      <td>20.8</td>\n",
       "      <td>0</td>\n",
       "      <td>21520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>27/01/2015</td>\n",
       "      <td>23.60</td>\n",
       "      <td>20.5</td>\n",
       "      <td>28.7</td>\n",
       "      <td>1.2</td>\n",
       "      <td>0</td>\n",
       "      <td>29972</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>28/01/2015</td>\n",
       "      <td>25.68</td>\n",
       "      <td>20.1</td>\n",
       "      <td>29.9</td>\n",
       "      <td>4.9</td>\n",
       "      <td>0</td>\n",
       "      <td>22603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>29/01/2015</td>\n",
       "      <td>25.00</td>\n",
       "      <td>21.5</td>\n",
       "      <td>29.2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>22696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>30/01/2015</td>\n",
       "      <td>22.80</td>\n",
       "      <td>20.9</td>\n",
       "      <td>26.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>335</th>\n",
       "      <td>02/12/2015</td>\n",
       "      <td>22.10</td>\n",
       "      <td>18.2</td>\n",
       "      <td>29.4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>30471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>336</th>\n",
       "      <td>03/12/2015</td>\n",
       "      <td>22.44</td>\n",
       "      <td>20.2</td>\n",
       "      <td>26.1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>337</th>\n",
       "      <td>04/12/2015</td>\n",
       "      <td>22.76</td>\n",
       "      <td>19.0</td>\n",
       "      <td>29.1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>29513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>338</th>\n",
       "      <td>05/12/2015</td>\n",
       "      <td>24.80</td>\n",
       "      <td>19.5</td>\n",
       "      <td>30.6</td>\n",
       "      <td>0.1</td>\n",
       "      <td>1</td>\n",
       "      <td>32451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>339</th>\n",
       "      <td>06/12/2015</td>\n",
       "      <td>23.12</td>\n",
       "      <td>20.6</td>\n",
       "      <td>28.0</td>\n",
       "      <td>0.1</td>\n",
       "      <td>1</td>\n",
       "      <td>32780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>340</th>\n",
       "      <td>07/12/2015</td>\n",
       "      <td>20.04</td>\n",
       "      <td>18.0</td>\n",
       "      <td>23.9</td>\n",
       "      <td>47.8</td>\n",
       "      <td>0</td>\n",
       "      <td>23375</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>341</th>\n",
       "      <td>08/12/2015</td>\n",
       "      <td>21.70</td>\n",
       "      <td>18.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>27713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>342</th>\n",
       "      <td>09/12/2015</td>\n",
       "      <td>23.96</td>\n",
       "      <td>19.6</td>\n",
       "      <td>27.9</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>27137</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>343</th>\n",
       "      <td>10/12/2015</td>\n",
       "      <td>24.00</td>\n",
       "      <td>22.2</td>\n",
       "      <td>27.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>22933</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>344</th>\n",
       "      <td>11/12/2015</td>\n",
       "      <td>24.04</td>\n",
       "      <td>20.7</td>\n",
       "      <td>30.3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>30740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>345</th>\n",
       "      <td>12/12/2015</td>\n",
       "      <td>23.92</td>\n",
       "      <td>20.6</td>\n",
       "      <td>30.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1</td>\n",
       "      <td>29579</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>346</th>\n",
       "      <td>13/12/2015</td>\n",
       "      <td>24.54</td>\n",
       "      <td>19.3</td>\n",
       "      <td>31.0</td>\n",
       "      <td>12.2</td>\n",
       "      <td>1</td>\n",
       "      <td>29188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>347</th>\n",
       "      <td>14/12/2015</td>\n",
       "      <td>26.28</td>\n",
       "      <td>21.1</td>\n",
       "      <td>32.5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>348</th>\n",
       "      <td>15/12/2015</td>\n",
       "      <td>25.66</td>\n",
       "      <td>20.6</td>\n",
       "      <td>32.8</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0</td>\n",
       "      <td>28617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>349</th>\n",
       "      <td>16/12/2015</td>\n",
       "      <td>22.04</td>\n",
       "      <td>20.7</td>\n",
       "      <td>24.8</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0</td>\n",
       "      <td>21062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>350</th>\n",
       "      <td>17/12/2015</td>\n",
       "      <td>23.32</td>\n",
       "      <td>18.9</td>\n",
       "      <td>28.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>24337</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>351</th>\n",
       "      <td>18/12/2015</td>\n",
       "      <td>26.42</td>\n",
       "      <td>19.6</td>\n",
       "      <td>33.5</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0</td>\n",
       "      <td>27042</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>352</th>\n",
       "      <td>19/12/2015</td>\n",
       "      <td>23.74</td>\n",
       "      <td>19.8</td>\n",
       "      <td>31.3</td>\n",
       "      <td>1.3</td>\n",
       "      <td>1</td>\n",
       "      <td>32536</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>353</th>\n",
       "      <td>20/12/2015</td>\n",
       "      <td>22.84</td>\n",
       "      <td>19.6</td>\n",
       "      <td>29.0</td>\n",
       "      <td>8.4</td>\n",
       "      <td>1</td>\n",
       "      <td>30127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>354</th>\n",
       "      <td>21/12/2015</td>\n",
       "      <td>23.12</td>\n",
       "      <td>19.6</td>\n",
       "      <td>29.1</td>\n",
       "      <td>27.2</td>\n",
       "      <td>0</td>\n",
       "      <td>24834</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>355</th>\n",
       "      <td>22/12/2015</td>\n",
       "      <td>24.60</td>\n",
       "      <td>19.6</td>\n",
       "      <td>32.4</td>\n",
       "      <td>43.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26828</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>356</th>\n",
       "      <td>23/12/2015</td>\n",
       "      <td>27.46</td>\n",
       "      <td>22.1</td>\n",
       "      <td>32.4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26468</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>357</th>\n",
       "      <td>24/12/2015</td>\n",
       "      <td>24.72</td>\n",
       "      <td>20.5</td>\n",
       "      <td>31.5</td>\n",
       "      <td>3.4</td>\n",
       "      <td>0</td>\n",
       "      <td>31572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>358</th>\n",
       "      <td>25/12/2015</td>\n",
       "      <td>23.58</td>\n",
       "      <td>17.8</td>\n",
       "      <td>31.0</td>\n",
       "      <td>39.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>359</th>\n",
       "      <td>26/12/2015</td>\n",
       "      <td>23.34</td>\n",
       "      <td>17.8</td>\n",
       "      <td>29.8</td>\n",
       "      <td>94.8</td>\n",
       "      <td>1</td>\n",
       "      <td>21955</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360</th>\n",
       "      <td>27/12/2015</td>\n",
       "      <td>24.00</td>\n",
       "      <td>21.1</td>\n",
       "      <td>28.2</td>\n",
       "      <td>13.6</td>\n",
       "      <td>1</td>\n",
       "      <td>32307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>361</th>\n",
       "      <td>28/12/2015</td>\n",
       "      <td>22.64</td>\n",
       "      <td>21.1</td>\n",
       "      <td>26.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>26095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>362</th>\n",
       "      <td>29/12/2015</td>\n",
       "      <td>21.68</td>\n",
       "      <td>20.3</td>\n",
       "      <td>24.1</td>\n",
       "      <td>10.3</td>\n",
       "      <td>0</td>\n",
       "      <td>22309</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>363</th>\n",
       "      <td>30/12/2015</td>\n",
       "      <td>21.38</td>\n",
       "      <td>19.3</td>\n",
       "      <td>22.4</td>\n",
       "      <td>6.3</td>\n",
       "      <td>0</td>\n",
       "      <td>20467</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>364</th>\n",
       "      <td>31/12/2015</td>\n",
       "      <td>24.76</td>\n",
       "      <td>20.2</td>\n",
       "      <td>29.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>22446</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>365 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           data  temp_media  temp_min  temp_max  chuva  fds  consumo\n",
       "0    01/01/2015       27.30      23.9      32.5    0.0    0    25461\n",
       "1    02/01/2015       27.02      24.5      33.5    0.0    0    28972\n",
       "2    03/01/2015       24.82      22.4      29.9    0.0    1    30814\n",
       "3    04/01/2015       23.98      21.5      28.6    1.2    1    29799\n",
       "4    05/01/2015       23.82      21.0      28.3    0.0    0    28900\n",
       "5    06/01/2015       23.78      20.1      30.5   12.2    0    28218\n",
       "6    07/01/2015       24.00      19.5      33.7    0.0    0    29732\n",
       "7    08/01/2015       24.90      19.5      32.8   48.6    0    28397\n",
       "8    09/01/2015       28.20      21.9      34.0    4.4    0    24886\n",
       "9    10/01/2015       26.76      22.1      34.2    0.0    1    37937\n",
       "10   11/01/2015       27.62      22.2      34.8    3.4    1    36254\n",
       "11   12/01/2015       25.96      21.4      35.4    0.0    0    25743\n",
       "12   13/01/2015       25.52      21.2      34.8    0.8    0    26990\n",
       "13   14/01/2015       25.96      21.3      34.0    1.6    0    31825\n",
       "14   15/01/2015       25.86      21.5      32.8    3.0    0    25724\n",
       "15   16/01/2015       26.50      22.3      32.7    2.0    0    29938\n",
       "16   17/01/2015       28.86      22.0      35.8    0.0    1    37690\n",
       "17   18/01/2015       28.26      23.4      35.6    0.0    1    30524\n",
       "18   19/01/2015       28.22      22.7      36.5    3.7    0    29265\n",
       "19   20/01/2015       27.68      23.3      35.6    0.6    0    35127\n",
       "20   21/01/2015       25.32      22.7      30.9    0.0    0    29130\n",
       "21   22/01/2015       21.74      19.2      26.1   31.0    0    25795\n",
       "22   23/01/2015       21.04      18.5      26.1    3.6    0    21784\n",
       "23   24/01/2015       23.12      19.0      29.4   13.0    1    28348\n",
       "24   25/01/2015       24.40      18.1      30.0    0.0    1    31088\n",
       "25   26/01/2015       22.40      18.9      28.7   20.8    0    21520\n",
       "26   27/01/2015       23.60      20.5      28.7    1.2    0    29972\n",
       "27   28/01/2015       25.68      20.1      29.9    4.9    0    22603\n",
       "28   29/01/2015       25.00      21.5      29.2    0.0    0    22696\n",
       "29   30/01/2015       22.80      20.9      26.9    0.0    0    26845\n",
       "..          ...         ...       ...       ...    ...  ...      ...\n",
       "335  02/12/2015       22.10      18.2      29.4    0.0    0    30471\n",
       "336  03/12/2015       22.44      20.2      26.1    0.0    0    28405\n",
       "337  04/12/2015       22.76      19.0      29.1    0.0    0    29513\n",
       "338  05/12/2015       24.80      19.5      30.6    0.1    1    32451\n",
       "339  06/12/2015       23.12      20.6      28.0    0.1    1    32780\n",
       "340  07/12/2015       20.04      18.0      23.9   47.8    0    23375\n",
       "341  08/12/2015       21.70      18.0      28.0    0.0    0    27713\n",
       "342  09/12/2015       23.96      19.6      27.9    0.0    0    27137\n",
       "343  10/12/2015       24.00      22.2      27.7    0.0    0    22933\n",
       "344  11/12/2015       24.04      20.7      30.3    0.0    0    30740\n",
       "345  12/12/2015       23.92      20.6      30.8    0.0    1    29579\n",
       "346  13/12/2015       24.54      19.3      31.0   12.2    1    29188\n",
       "347  14/12/2015       26.28      21.1      32.5    2.0    0    28131\n",
       "348  15/12/2015       25.66      20.6      32.8    4.0    0    28617\n",
       "349  16/12/2015       22.04      20.7      24.8    1.5    0    21062\n",
       "350  17/12/2015       23.32      18.9      28.7    0.0    0    24337\n",
       "351  18/12/2015       26.42      19.6      33.5    0.1    0    27042\n",
       "352  19/12/2015       23.74      19.8      31.3    1.3    1    32536\n",
       "353  20/12/2015       22.84      19.6      29.0    8.4    1    30127\n",
       "354  21/12/2015       23.12      19.6      29.1   27.2    0    24834\n",
       "355  22/12/2015       24.60      19.6      32.4   43.0    0    26828\n",
       "356  23/12/2015       27.46      22.1      32.4    3.0    0    26468\n",
       "357  24/12/2015       24.72      20.5      31.5    3.4    0    31572\n",
       "358  25/12/2015       23.58      17.8      31.0   39.0    0    26308\n",
       "359  26/12/2015       23.34      17.8      29.8   94.8    1    21955\n",
       "360  27/12/2015       24.00      21.1      28.2   13.6    1    32307\n",
       "361  28/12/2015       22.64      21.1      26.7    0.0    0    26095\n",
       "362  29/12/2015       21.68      20.3      24.1   10.3    0    22309\n",
       "363  30/12/2015       21.38      19.3      22.4    6.3    0    20467\n",
       "364  31/12/2015       24.76      20.2      29.0    0.0    0    22446\n",
       "\n",
       "[365 rows x 7 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dados"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Verificando o tamanho do dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(365, 7)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dados.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Análises Preliminares</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Estatísticas descritivas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_media</th>\n",
       "      <th>temp_min</th>\n",
       "      <th>temp_max</th>\n",
       "      <th>chuva</th>\n",
       "      <th>fds</th>\n",
       "      <th>consumo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>365.00</td>\n",
       "      <td>365.00</td>\n",
       "      <td>365.00</td>\n",
       "      <td>365.00</td>\n",
       "      <td>365.00</td>\n",
       "      <td>365.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>21.23</td>\n",
       "      <td>17.46</td>\n",
       "      <td>26.61</td>\n",
       "      <td>5.20</td>\n",
       "      <td>0.28</td>\n",
       "      <td>25401.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>3.18</td>\n",
       "      <td>2.83</td>\n",
       "      <td>4.32</td>\n",
       "      <td>12.42</td>\n",
       "      <td>0.45</td>\n",
       "      <td>4399.14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>12.90</td>\n",
       "      <td>10.60</td>\n",
       "      <td>14.50</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>14343.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>19.02</td>\n",
       "      <td>15.30</td>\n",
       "      <td>23.80</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>22008.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>21.38</td>\n",
       "      <td>17.90</td>\n",
       "      <td>26.90</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>24867.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>23.28</td>\n",
       "      <td>19.60</td>\n",
       "      <td>29.40</td>\n",
       "      <td>3.20</td>\n",
       "      <td>1.00</td>\n",
       "      <td>28631.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>28.86</td>\n",
       "      <td>24.50</td>\n",
       "      <td>36.50</td>\n",
       "      <td>94.80</td>\n",
       "      <td>1.00</td>\n",
       "      <td>37937.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       temp_media  temp_min  temp_max   chuva     fds   consumo\n",
       "count      365.00    365.00    365.00  365.00  365.00    365.00\n",
       "mean        21.23     17.46     26.61    5.20    0.28  25401.37\n",
       "std          3.18      2.83      4.32   12.42    0.45   4399.14\n",
       "min         12.90     10.60     14.50    0.00    0.00  14343.00\n",
       "25%         19.02     15.30     23.80    0.00    0.00  22008.00\n",
       "50%         21.38     17.90     26.90    0.00    0.00  24867.00\n",
       "75%         23.28     19.60     29.40    3.20    1.00  28631.00\n",
       "max         28.86     24.50     36.50   94.80    1.00  37937.00"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dados.describe().round(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Matriz de correlação\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>O <b>coeficiente de correlação</b> é uma medida de associação linear entre duas variáveis e situa-se entre <b>-1</b> e <b>+1</b> sendo que <b>-1</b> indica associação negativa perfeita e <b>+1</b> indica associação positiva perfeita.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_media</th>\n",
       "      <th>temp_min</th>\n",
       "      <th>temp_max</th>\n",
       "      <th>chuva</th>\n",
       "      <th>fds</th>\n",
       "      <th>consumo</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>temp_media</th>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.8628</td>\n",
       "      <td>0.9225</td>\n",
       "      <td>0.0244</td>\n",
       "      <td>-0.0508</td>\n",
       "      <td>0.5746</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>temp_min</th>\n",
       "      <td>0.8628</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.6729</td>\n",
       "      <td>0.0986</td>\n",
       "      <td>-0.0595</td>\n",
       "      <td>0.3925</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>temp_max</th>\n",
       "      <td>0.9225</td>\n",
       "      <td>0.6729</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>-0.0493</td>\n",
       "      <td>-0.0403</td>\n",
       "      <td>0.6427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>chuva</th>\n",
       "      <td>0.0244</td>\n",
       "      <td>0.0986</td>\n",
       "      <td>-0.0493</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.0016</td>\n",
       "      <td>-0.1938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>fds</th>\n",
       "      <td>-0.0508</td>\n",
       "      <td>-0.0595</td>\n",
       "      <td>-0.0403</td>\n",
       "      <td>0.0016</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.5060</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>consumo</th>\n",
       "      <td>0.5746</td>\n",
       "      <td>0.3925</td>\n",
       "      <td>0.6427</td>\n",
       "      <td>-0.1938</td>\n",
       "      <td>0.5060</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            temp_media  temp_min  temp_max   chuva     fds  consumo\n",
       "temp_media      1.0000    0.8628    0.9225  0.0244 -0.0508   0.5746\n",
       "temp_min        0.8628    1.0000    0.6729  0.0986 -0.0595   0.3925\n",
       "temp_max        0.9225    0.6729    1.0000 -0.0493 -0.0403   0.6427\n",
       "chuva           0.0244    0.0986   -0.0493  1.0000  0.0016  -0.1938\n",
       "fds            -0.0508   -0.0595   -0.0403  0.0016  1.0000   0.5060\n",
       "consumo         0.5746    0.3925    0.6427 -0.1938  0.5060   1.0000"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dados.corr().round(4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Comportamento da Variável Dependente (Y)</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Análises gráficas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotando a variável *dependente* (y)\n",
    "https://pandas.pydata.org/pandas-docs/stable/visualization.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 128752 characters]\n",
      "text/plain": [
       "<Figure size 1440x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(figsize=(20, 6))\n",
    "\n",
    "ax.set_title('Consumo de Cerveja', fontsize=20)\n",
    "ax.set_ylabel('Litros', fontsize=16)\n",
    "ax.set_xlabel('Dias', fontsize=16)\n",
    "ax = dados['consumo'].plot(fontsize=14)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Box Plot</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width='700px' src='../Dados/img/Box-Plot.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importando biblioteca seaborn\n",
    "https://seaborn.pydata.org/\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>O Seaborn é uma biblioteca Python de visualização de dados baseada no matplotlib. Ela fornece uma interface de alto nível para desenhar gráficos estatísticos.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Box plot da variável *dependente* (y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://seaborn.pydata.org/generated/seaborn.boxplot.html?highlight=boxplot#seaborn.boxplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a6316d1d0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 12068 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(data=dados['consumo'], orient='v', width=0.2)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Consumo de Cerveja', fontsize=20)\n",
    "ax.set_ylabel('Litros', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Box Plot com Duas Variáveis</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Investigando a variável *dependente* (y) segundo determinada característica"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a631cc240>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15104 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(y='consumo', x='fds', data=dados, orient='v', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Consumo de Cerveja', fontsize=20)\n",
    "ax.set_ylabel('Litros', fontsize=16)\n",
    "ax.set_xlabel('Final de Semana', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configurações de estilo e cor da biblioteca *seaborn*\n",
    "\n",
    "### Controle de estilo\n",
    "\n",
    "> ####  API\n",
    "> https://seaborn.pydata.org/api.html#style-api\n",
    "\n",
    "> #### Tutorial\n",
    "> https://seaborn.pydata.org/tutorial/aesthetics.html#aesthetics-tutorial\n",
    "\n",
    "### Paleta de cores\n",
    "\n",
    "> #### API\n",
    "> https://seaborn.pydata.org/api.html#palette-api\n",
    "\n",
    "> #### Tutorial\n",
    "> https://seaborn.pydata.org/tutorial/color_palettes.html#palette-tutorial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# palette -> Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, icefire, icefire_r, inferno, inferno_r, jet, jet_r, magma, magma_r, mako, mako_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, rocket, rocket_r, seismic, seismic_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, vlag, vlag_r, winter, winter_r\n",
    "sns.set_palette(\"Accent\")\n",
    "\n",
    "# style -> white, dark, whitegrid, darkgrid, ticks\n",
    "sns.set_style(\"darkgrid\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a631fe198>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18216 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(y='consumo', x='fds', data=dados, orient='v', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Consumo de Cerveja', fontsize=20)\n",
    "ax.set_ylabel('Litros', fontsize=16)\n",
    "ax.set_xlabel('Final de Semana', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Distribuição de Frequências</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Distribuição de frequências da variável *dependente* (y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://seaborn.pydata.org/generated/seaborn.distplot.html?highlight=distplot#seaborn.distplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a634f5400>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39372 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.distplot(dados['consumo'])\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Distribuição de Frequências', fontsize=20)\n",
    "ax.set_ylabel('Consumo de Cerveja (Litros)', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Variável Dependente X Variáveis Explicativas (pairplot)</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gráficos de dispersão entre as variáveis do dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## seaborn.pairplot\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.pairplot.html?highlight=pairplot#seaborn.pairplot\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>Plota o relacionamento entre pares de variáveis em um dataset.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 305304 characters]\n",
      "text/plain": [
       "<Figure size 1080x1080 with 42 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.pairplot(dados)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotando o pairplot fixando somente uma variável no eixo y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x26a648cbf60>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 72128 characters]\n",
      "text/plain": [
       "<Figure size 900x180 with 5 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.pairplot(dados, y_vars='consumo', x_vars=['temp_min', 'temp_media', 'temp_max', 'chuva', 'fds'])\n",
    "ax.fig.suptitle('Dispersão entre as Variáveis', fontsize=20, y=1.05)\n",
    "ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x26a64e9cc50>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 71840 characters]\n",
      "text/plain": [
       "<Figure size 900x180 with 5 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.pairplot(dados, y_vars='consumo', x_vars=['temp_min', 'temp_media', 'temp_max', 'chuva', 'fds'], kind='reg')\n",
    "ax.fig.suptitle('Dispersão entre as Variáveis', fontsize=20, y=1.05)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Variável Dependente X Variáveis Explicativas (jointplot)</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## seaborn.jointplot\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.jointplot.html?highlight=jointplot#seaborn.jointplot\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>Plota o relacionamento entre duas variáveis e suas respectivas distribuições de frequência.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.JointGrid at 0x26a63d557b8>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39132 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.jointplot(x=\"temp_max\", y=\"consumo\", data=dados)\n",
    "ax.fig.suptitle('Dispersão - Consumo X Temperatura', fontsize=18, y=1.05)\n",
    "ax.set_axis_labels(\"Temperatura Máxima\", \"Consumo de  Cerveja\", fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotando um jointplot com a reta de regressão estimada"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.JointGrid at 0x26a63aa4a58>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 64476 characters]\n",
      "text/plain": [
       "<Figure size 432x432 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.jointplot(x=\"temp_max\", y=\"consumo\", data=dados, kind='reg')\n",
    "ax.fig.suptitle('Dispersão - Consumo X Temperatura', fontsize=18, y=1.05)\n",
    "ax.set_axis_labels(\"Temperatura Máxima\", \"Consumo de  Cerveja\", fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Variável Dependente X Variáveis Explicativas (lmplot)</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## seaborn.lmplot\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.lmplot.html?highlight=lmplot#seaborn.lmplot\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>Plota a reta de regressão entre duas variáveis juntamente com a dispersão entre elas.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x26a63d04128>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 56960 characters]\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.lmplot(x=\"temp_max\", y=\"consumo\", data=dados)\n",
    "ax.fig.suptitle('Reta de Regressão - Consumo X Temperatura', fontsize=16, y=1.02)\n",
    "ax.set_xlabels(\"Temperatura Máxima (°C)\", fontsize=14)\n",
    "ax.set_ylabels(\"Consumo de Cerveja (litros)\", fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotando um lmplot utilizando uma terceira variável na análise (tipo I)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x26a640f5a58>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 74748 characters]\n",
      "text/plain": [
       "<Figure size 438.75x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.lmplot(x=\"temp_max\", y=\"consumo\", data=dados, hue=\"fds\", markers=['o', '*'], legend=False)\n",
    "ax.fig.suptitle('Reta de Regressão - Consumo X Temperatura X Final de Semana', fontsize=16, y=1.02)\n",
    "ax.set_xlabels(\"Temperatura Máxima (°C)\", fontsize=14)\n",
    "ax.set_ylabels(\"Consumo de Cerveja (litros)\", fontsize=14)\n",
    "ax.add_legend(title=\"Fim de Semana\")\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotando um lmplot utilizando uma terceira variável na análise (tipo II)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x26a64157fd0>"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 75168 characters]\n",
      "text/plain": [
       "<Figure size 720x360 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.lmplot(x=\"temp_max\", y=\"consumo\", data=dados, col=\"fds\")\n",
    "ax.fig.suptitle('Reta de Regressão - Consumo X Temperatura X Final de Semana', fontsize=16, y=1.02)\n",
    "ax.set_xlabels(\"Temperatura Máxima (°C)\", fontsize=14)\n",
    "ax.set_ylabels(\"Consumo de Cerveja (litros)\", fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Estimando um Modelo de Regressão Linear para o Consumo</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regresão Linear\n",
    "<hr>\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>A análise de regressão diz respeito ao estudo da dependência de uma variável (a variável <b>dependente</b>) em relação a uma ou mais variáveis, as variáveis explanatórias, visando estimar e/ou prever o valor médio da primeira em termos dos valores conhecidos ou fixados das segundas.</p>\n",
    "\n",
    "\n",
    "## scikit-learn (https://scikit-learn.org/stable/)\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>O *scikit-learn* é um módulo Python especializado em soluções para *machine learning*.</p>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importando o *train_test_split* da biblioteca *scikit-learn*\n",
    "\n",
    "https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando uma Series (pandas) para armazenar o Consumo de Cerveja (y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = dados['consumo']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando um DataFrame (pandas) para armazenar as variáveis explicativas (X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = dados[['temp_max', 'chuva', 'fds']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando os datasets de treino e de teste"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=2811)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Verificando os tamanhos dos arquivos gerados pela função *train_test_split*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(255, 3)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(110, 3)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "365"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape[0] + X_test.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "109.5"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape[0] * 0.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "255.49999999999997"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape[0] * 0.7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width='600px' src='../Dados/img/reg_01.jpg'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importando *LinearRegression* e *metrics* da biblioteca *scikit-learn*\n",
    "\n",
    "https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html\n",
    "\n",
    "https://scikit-learn.org/stable/modules/classes.html#regression-metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn import metrics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Instanciando a classe *LinearRegression()*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelo = LinearRegression()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utilizando o método *fit()* do objeto \"modelo\" para estimar nosso modelo linear utilizando os dados de TREINO (y_train e X_train)\n",
    "\n",
    "https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression.fit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelo.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo o coeficiente de determinação (R²) do modelo estimado com os dados de TREINO\n",
    "\n",
    "https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression.score\n",
    "\n",
    "### Coeficiente de Determinação - R²\n",
    "\n",
    "O coeficiente de determinação (R²) é uma medida resumida que diz quanto a linha de regressão ajusta-se aos dados. É um valor entra 0 e 1.\n",
    "\n",
    "$$R^2(y, \\hat{y}) = 1 - \\frac {\\sum_{i=0}^{n-1}(y_i-\\hat{y}_i)^2}{\\sum_{i=0}^{n-1}(y_i-\\bar{y}_i)^2}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² = 0.73\n"
     ]
    }
   ],
   "source": [
    "print('R² = {}'.format(modelo.score(X_train, y_train).round(2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gerando previsões para os dados de TESTE (X_test) utilizando o método *predict()* do objeto \"modelo\"\n",
    "\n",
    "https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression.predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_previsto = modelo.predict(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo o coeficiente de determinação (R²) para as previsões do nosso modelo\n",
    "\n",
    "https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html#sklearn.metrics.r2_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² = 0.69\n"
     ]
    }
   ],
   "source": [
    "print('R² = %s' % metrics.r2_score(y_test, y_previsto).round(2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Obtendo Previsões Pontuais</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dados de entrada"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_max</th>\n",
       "      <th>chuva</th>\n",
       "      <th>fds</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>30.5</td>\n",
       "      <td>12.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   temp_max  chuva  fds\n",
       "5      30.5   12.2    0"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "entrada = X_test[0:1]\n",
    "entrada"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gerando previsão pontual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26094.901775262017"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelo.predict(entrada)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando um simulador simples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38742.53 litros\n"
     ]
    }
   ],
   "source": [
    "temp_max=40\n",
    "chuva=0\n",
    "fds=1\n",
    "entrada=[[temp_max, chuva, fds]]\n",
    "\n",
    "print('{0:.2f} litros'.format(modelo.predict(entrada)[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Interpretação dos Coeficientes Estimados</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width='600px' src='../Dados/img/reg_02.jpg'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo o intercepto do modelo\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>O <b>intercepto</b> representa o efeito médio em $Y$ (Consumo de Cerveja) tendo todas as variáveis explicativas excluídas do modelo. De forma mais simples, o <b>intercepto</b> representa o efeito médio em $Y$ (Consumo de Cerveja) quando $X_2$ (Temperatura Máxima), $X_3$ (Chuva) e $X_4$ (Final de Semana) são iguais a zero.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5951.9763393124485"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelo.intercept_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.float64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(modelo.intercept_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo os coeficientes de regressão\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>Os <b>coeficientes de regressão</b> $\\beta_2$, $\\beta_3$ e $\\beta_4$ são conhecidos como <b>coeficientes parciais de regressão</b> ou <b>coeficientes parciais angulares</b>. Considerando o número de variáveis explicativas de nosso modelo, seu significado seria o seguinte: $\\beta_2$ mede a variação no valor médio de $Y$ (Consumo de Cerveja), por unidade de variação em $X_2$ (Temperatura Máxima), mantendo-se os valores de $X_3$ (Chuva) e $X_4$ (Final de Semana) constantes. Em outras palavras, ele nos dá o efeito \"direto\" ou \"líquido\" de uma unidade de variação em $X_2$ sobre o valor médio de $Y$, excluídos os efeitos que $X_3$ e $X_4$ possam ter sobre a média de $Y$. De modo análogo podemos interpretar os demais coeficientes de regressão.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 684.73675898,  -60.7824355 , 5401.08333866])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelo.coef_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(modelo.coef_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Confirmando a ordem das variáveis explicativas no DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['temp_max', 'chuva', 'fds'], dtype='object')"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando uma lista com os nomes das variáveis do modelo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "index=['Intercepto', 'Temperatura Máxima', 'Chuva (mm)', 'Final de Semana']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando um DataFrame para armazenar os coeficientes do modelo\n",
    "\n",
    "https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html?#numpy.append"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Parâmetros</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Intercepto</th>\n",
       "      <td>5951.976339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Temperatura Máxima</th>\n",
       "      <td>684.736759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chuva (mm)</th>\n",
       "      <td>-60.782435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Final de Semana</th>\n",
       "      <td>5401.083339</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     Parâmetros\n",
       "Intercepto          5951.976339\n",
       "Temperatura Máxima   684.736759\n",
       "Chuva (mm)           -60.782435\n",
       "Final de Semana     5401.083339"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(data=np.append(modelo.intercept_, modelo.coef_), index=index, columns=['Parâmetros'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Interpretação dos Coeficientes Estimados\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>\n",
    "<b>Intercepto</b> → Excluindo o efeito das variáveis explicativas ($X_2=X_3=X_4=0$) o efeito médio no Consumo de Cerveja seria de <b>5951,98 litros</b>.\n",
    "</p>\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>\n",
    "<b>Temperatura Máxima (°C)</b> → Mantendo-se os valores de $X_3$ (Chuva) e $X_4$ (Final de Semana) constantes, o acréscimo de 1°C na Temperatura Máxima gera uma variação média no Consumo de Cerveja de <b>684,74 litros</b>.\n",
    "</p>\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>\n",
    "<b>Chuva (mm)</b> → Mantendo-se os valores de $X_2$ (Temperatura Máxima) e $X_4$ (Final de Semana) constantes, o acréscimo de 1mm de Chuva gera uma variação média no Consumo de Cerveja de <b>-60,78 litros</b>.\n",
    "</p>\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>\n",
    "<b>Final de Semana (Sim/Não)</b> → Mantendo-se os valores de $X_2$ (Temperatura Máxima) e $X_3$ (Chuva) constantes, o fato de o dia ser classificado como Final de Semana gera uma variação média no Consumo de Cerveja de <b>5401,08 litros</b>.\n",
    "</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Análises Gráficas das Previsões do Modelo</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gerando as previsões do modelo para os dados de TREINO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_previsto_train = modelo.predict(X_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gráfico de dispersão entre valor estimado e valor real\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.scatterplot.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a64caef98>"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 42772 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.scatterplot(x=y_previsto_train, y=y_train)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Previsão X Real', fontsize=18)\n",
    "ax.set_xlabel('Consumo de Cerveja (litros) - Previsão', fontsize=14)\n",
    "ax.set_ylabel('Consumo de Cerveja (litros) - Real', fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo os resíduos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "residuo = y_train - y_previsto_train"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gráfico de dispersão entre valor estimado e resíduos\n",
    "\n",
    "Método informal de verificação da hipótese de variância constante dos resíduos (homocedasticidade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a64cfa710>"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 58472 characters]\n",
      "text/plain": [
       "<Figure size 1440x576 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.scatterplot(x=y_previsto_train, y=residuo, s=150)\n",
    "ax.figure.set_size_inches(20, 8)\n",
    "ax.set_title('Resíduos X Previsão', fontsize=18)\n",
    "ax.set_xlabel('Consumo de Cerveja (litros) - Previsão', fontsize=14)\n",
    "ax.set_ylabel('Resíduos', fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utilizando os resíduos ao quadrado"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img width='800px' src='../Dados/img/var_u.jpg'>\n",
    "Fonte: Econometria Básica - 5ª edição - Gujarati e Porter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a671cc780>"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 58844 characters]\n",
      "text/plain": [
       "<Figure size 1440x576 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.scatterplot(x=y_previsto_train, y=residuo**2, s=150)\n",
    "ax.figure.set_size_inches(20, 8)\n",
    "ax.set_title('Resíduos X Previsão', fontsize=18)\n",
    "ax.set_xlabel('Consumo de Cerveja (litros) - Previsão', fontsize=14)\n",
    "ax.set_ylabel('Resíduos²', fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotando a distribuição de frequências dos resíduos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x26a6768b668>"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 38664 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.distplot(residuo)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Distribuição de Frequências dos Resíduos', fontsize=18)\n",
    "ax.set_xlabel('Litros', fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Comparando Modelos</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Estimando um novo modelo com a substituição da variável explicativa Temperatura Máxima pela Temperatuda Média"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = dados[['temp_media', 'chuva', 'fds']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando os datasets de treino e de teste"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2_train, X2_test, y2_train, y2_test = train_test_split(X2, y, test_size=0.3, random_state=2811)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Instanciando a classe *LinearRegression()*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelo_2 = LinearRegression()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utilizando o método *fit()* do objeto \"modelo_2\" para estimar nosso modelo linear utilizando os dados de TREINO (y2_train e X2_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelo_2.fit(X2_train, y2_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo o coeficiente de determinação (R²) do novo modelo estimado e comparando com o resultado do modelo anterior"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Modelo com Temp. Média\n",
      "R² = 0.66\n"
     ]
    }
   ],
   "source": [
    "print('Modelo com Temp. Média')\n",
    "print('R² = {}'.format(modelo_2.score(X2_train, y2_train).round(2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Modelo com Temp. Máxima\n",
      "R² = 0.73\n"
     ]
    }
   ],
   "source": [
    "print('Modelo com Temp. Máxima')\n",
    "print('R² = {}'.format(modelo.score(X_train, y_train).round(2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gerando previsões para os dados de TESTE (X_test e X2_test) utilizando o método *predict()* dos objetos \"modelo\" e \"modelo_2\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_previsto = modelo.predict(X_test)\n",
    "y_previsto_2 = modelo_2.predict(X2_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo o coeficiente de determinação (R²) para as previsões dos dois modelos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Modelo com Temp. Média\n",
      "R² = 0.66\n"
     ]
    }
   ],
   "source": [
    "print('Modelo com Temp. Média')\n",
    "print('R² = {}'.format(metrics.r2_score(y2_test, y_previsto_2).round(2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Modelo com Temp. Máxima\n",
      "R² = 0.69\n"
     ]
    }
   ],
   "source": [
    "print('Modelo com Temp. Máxima')\n",
    "print('R² = {}'.format(metrics.r2_score(y_test, y_previsto).round(2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Outras Métricas de Regressão</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Métricas da regressão\n",
    "<hr>\n",
    "\n",
    "fonte: https://scikit-learn.org/stable/modules/model_evaluation.html#regression-metrics\n",
    "\n",
    "Algumas estatísticas obtidas do modelo de regressão são muito úteis como critério de comparação entre modelos estimados e de seleção do melhor modelo, as principais métricas de regressão que o scikit-learn disponibiliza para modelos lineares são as seguintes:\n",
    "\n",
    "### Erro Quadrático Médio\n",
    "\n",
    "Média dos quadrados dos erros. Ajustes melhores apresentam $EQM$ mais baixo.\n",
    "\n",
    "$$EQM(y, \\hat{y}) = \\frac 1n\\sum_{i=0}^{n-1}(y_i-\\hat{y}_i)^2$$\n",
    "\n",
    "### Raíz do Erro Quadrático Médio\n",
    "\n",
    "Raíz quadrada da média dos quadrados dos erros. Ajustes melhores apresentam $\\sqrt{EQM}$ mais baixo.\n",
    "\n",
    "$$\\sqrt{EQM(y, \\hat{y})} = \\sqrt{\\frac 1n\\sum_{i=0}^{n-1}(y_i-\\hat{y}_i)^2}$$\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo métricas para o modelo com Temperatura Média"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Métricas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>EQM</th>\n",
       "      <td>6060775.46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>REQM</th>\n",
       "      <td>2461.86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>R²</th>\n",
       "      <td>0.66</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Métricas\n",
       "EQM   6060775.46\n",
       "REQM     2461.86\n",
       "R²          0.66"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "EQM_2 = metrics.mean_squared_error(y2_test, y_previsto_2).round(2)\n",
    "REQM_2 = np.sqrt(metrics.mean_squared_error(y2_test, y_previsto_2)).round(2)\n",
    "R2_2 = metrics.r2_score(y2_test, y_previsto_2).round(2)\n",
    "\n",
    "pd.DataFrame([EQM_2, REQM_2, R2_2], ['EQM', 'REQM', 'R²'], columns=['Métricas'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo métricas para o modelo com Temperatura Máxima"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Métricas</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>EQM</th>\n",
       "      <td>5471976.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>REQM</th>\n",
       "      <td>2339.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>R²</th>\n",
       "      <td>0.69</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Métricas\n",
       "EQM   5471976.38\n",
       "REQM     2339.23\n",
       "R²          0.69"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "EQM = metrics.mean_squared_error(y_test, y_previsto).round(2)\n",
    "REQM = np.sqrt(metrics.mean_squared_error(y_test, y_previsto)).round(2)\n",
    "R2 = metrics.r2_score(y_test, y_previsto).round(2)\n",
    "\n",
    "pd.DataFrame([EQM, REQM, R2], ['EQM', 'REQM', 'R²'], columns=['Métricas'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>Salvando e Carregando o Modelo Estimado</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dados de entrada"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>temp_max</th>\n",
       "      <th>chuva</th>\n",
       "      <th>fds</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>30.5</td>\n",
       "      <td>12.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   temp_max  chuva  fds\n",
       "5      30.5   12.2    0"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test[0:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [],
   "source": [
    "entrada = X_test[0:1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gerando previsão pontual"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "26094.901775262017"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelo.predict(entrada)[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando um simulador simples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "32599.90 litros\n"
     ]
    }
   ],
   "source": [
    "temp_max=40\n",
    "chuva=12.2\n",
    "fds=0\n",
    "entrada=[[temp_max, chuva, fds]]\n",
    "\n",
    "print('{0:.2f} litros'.format(modelo.predict(entrada)[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Salvando o modelo estimado"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pickle (https://docs.python.org/3/library/pickle.html)\n",
    "\n",
    "<p style='font-size: 20px; line-height: 2; margin: 10px 50px; text-align: justify;'>O módulo <b>pickle</b> implementa protocolos binários para serializar e desserializar a estrutura de um objeto Python.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "output = open('modelo_consumo_cerveja', 'wb')\n",
    "pickle.dump(modelo, output)\n",
    "output.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Em um novo notebook/projeto Python\n",
    "\n",
    "<h4 style='color: blue; font-weight: normal'>In [1]:</h4>\n",
    "\n",
    "```sh\n",
    "import pickle\n",
    "\n",
    "modelo = open('modelo_consumo_cerveja','rb')\n",
    "lm_new = pickle.load(modelo)\n",
    "modelo.close()\n",
    "\n",
    "temp_max = 30.5\n",
    "chuva = 12.2\n",
    "fds = 0\n",
    "entrada = [[temp_max, chuva, fds]]\n",
    "print('{0:.2f} litros'.format(lm_new.predict(entrada)[0]))\n",
    "```\n",
    "\n",
    "<h4 style='color: red; font-weight: normal'>Out [1]:</h4>\n",
    "\n",
    "```\n",
    "26094.90 litros\n",
    "```"
   ]
  },
  {
   "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
