{
 "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bibliotecas opcionais"
   ]
  },
  {
   "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/greenwing1985/housepricing\n",
    "\n",
    "### Descrição:\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>Nosso objetivo neste exercício é criar um modelo de machine learning, utilizando a técnica de Regressão Linear, que faça previsões sobre os preços de imóveis a partir de um conjunto de características conhecidas dos imóveis.</p>\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>Vamos utilizar um dataset disponível no Kaggle que foi gerado por computador para treinamento de machine learning para iniciantes. Este dataset foi modificado para facilitar o nosso objetivo, que é fixar o conhecimento adquirido no treinamento de Regressão Linear.</p>\n",
    "\n",
    "<p style='font-size: 18px; line-height: 2; margin: 10px 50px; text-align: justify;'>Siga os passos propostos nos comentários acima de cada célular e bons estudos.</p>\n",
    "\n",
    "### Dados:\n",
    "<ul style='font-size: 18px; line-height: 2; text-align: justify;'>\n",
    "    <li><b>precos</b> - Preços do imóveis</li>\n",
    "    <li><b>area</b> - Área do imóvel</li>\n",
    "    <li><b>garagem</b> - Número de vagas de garagem</li>\n",
    "    <li><b>banheiros</b> - Número de banheiros</li>\n",
    "    <li><b>lareira</b> - Número de lareiras</li>\n",
    "    <li><b>marmore</b> - Se o imóvel possui acabamento em mármore branco (1) ou não (0)</li>\n",
    "    <li><b>andares</b> - Se o imóvel possui mais de um andar (1) ou não (0)</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Leitura dos dados\n",
    "\n",
    "Dataset está na pasta \"Dados\" com o nome \"HousePrices_HalfMil.csv\" em usa como separador \";\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dados = pd.read_csv('../Dados/HousePrices_HalfMil.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>precos</th>\n",
       "      <th>area</th>\n",
       "      <th>garagem</th>\n",
       "      <th>banheiros</th>\n",
       "      <th>lareira</th>\n",
       "      <th>marmore</th>\n",
       "      <th>andares</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>51875</td>\n",
       "      <td>25</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>17875</td>\n",
       "      <td>35</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>47075</td>\n",
       "      <td>195</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>38575</td>\n",
       "      <td>33</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>33775</td>\n",
       "      <td>11</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>35450</td>\n",
       "      <td>148</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>52025</td>\n",
       "      <td>211</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>37550</td>\n",
       "      <td>172</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>31500</td>\n",
       "      <td>62</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>37650</td>\n",
       "      <td>218</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>57075</td>\n",
       "      <td>215</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>38750</td>\n",
       "      <td>220</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>29950</td>\n",
       "      <td>58</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>30750</td>\n",
       "      <td>42</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>45750</td>\n",
       "      <td>182</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>52875</td>\n",
       "      <td>57</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>41850</td>\n",
       "      <td>136</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>36150</td>\n",
       "      <td>186</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>60850</td>\n",
       "      <td>206</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>60225</td>\n",
       "      <td>171</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>56600</td>\n",
       "      <td>186</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>51175</td>\n",
       "      <td>229</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>42175</td>\n",
       "      <td>87</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>53825</td>\n",
       "      <td>135</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>23725</td>\n",
       "      <td>101</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>33275</td>\n",
       "      <td>173</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>61700</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>24700</td>\n",
       "      <td>248</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>54200</td>\n",
       "      <td>218</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>56550</td>\n",
       "      <td>84</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</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>970</th>\n",
       "      <td>56700</td>\n",
       "      <td>238</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>971</th>\n",
       "      <td>34875</td>\n",
       "      <td>237</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>972</th>\n",
       "      <td>31600</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>973</th>\n",
       "      <td>44400</td>\n",
       "      <td>248</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>974</th>\n",
       "      <td>33975</td>\n",
       "      <td>79</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>975</th>\n",
       "      <td>56650</td>\n",
       "      <td>98</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>976</th>\n",
       "      <td>49600</td>\n",
       "      <td>124</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>977</th>\n",
       "      <td>39325</td>\n",
       "      <td>105</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>978</th>\n",
       "      <td>43425</td>\n",
       "      <td>227</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>979</th>\n",
       "      <td>35000</td>\n",
       "      <td>110</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>980</th>\n",
       "      <td>22375</td>\n",
       "      <td>55</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>981</th>\n",
       "      <td>27550</td>\n",
       "      <td>172</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>982</th>\n",
       "      <td>34975</td>\n",
       "      <td>159</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>983</th>\n",
       "      <td>54325</td>\n",
       "      <td>243</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>984</th>\n",
       "      <td>41275</td>\n",
       "      <td>223</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>985</th>\n",
       "      <td>63150</td>\n",
       "      <td>188</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>986</th>\n",
       "      <td>40825</td>\n",
       "      <td>53</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>987</th>\n",
       "      <td>29500</td>\n",
       "      <td>122</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>988</th>\n",
       "      <td>54775</td>\n",
       "      <td>113</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>989</th>\n",
       "      <td>35725</td>\n",
       "      <td>121</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>990</th>\n",
       "      <td>29125</td>\n",
       "      <td>207</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>991</th>\n",
       "      <td>46125</td>\n",
       "      <td>55</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>992</th>\n",
       "      <td>25850</td>\n",
       "      <td>86</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>993</th>\n",
       "      <td>41350</td>\n",
       "      <td>226</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>994</th>\n",
       "      <td>46600</td>\n",
       "      <td>36</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>29150</td>\n",
       "      <td>48</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>43550</td>\n",
       "      <td>112</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>56575</td>\n",
       "      <td>185</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>56075</td>\n",
       "      <td>185</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>13350</td>\n",
       "      <td>94</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     precos  area  garagem  banheiros  lareira  marmore  andares\n",
       "0     51875    25        3          4        3        0        1\n",
       "1     17875    35        1          3        1        0        0\n",
       "2     47075   195        2          4        2        0        0\n",
       "3     38575    33        2          2        1        0        1\n",
       "4     33775    11        2          3        0        0        1\n",
       "5     35450   148        2          5        2        0        0\n",
       "6     52025   211        2          3        2        1        0\n",
       "7     37550   172        2          2        3        1        0\n",
       "8     31500    62        1          4        2        0        0\n",
       "9     37650   218        3          1        0        1        0\n",
       "10    57075   215        3          5        2        1        1\n",
       "11    38750   220        2          2        4        0        1\n",
       "12    29950    58        1          1        0        1        0\n",
       "13    30750    42        1          4        0        1        0\n",
       "14    45750   182        1          5        3        1        0\n",
       "15    52875    57        2          2        4        1        0\n",
       "16    41850   136        2          1        0        0        0\n",
       "17    36150   186        1          3        4        1        0\n",
       "18    60850   206        3          5        3        0        1\n",
       "19    60225   171        1          2        4        0        1\n",
       "20    56600   186        1          2        2        1        1\n",
       "21    51175   229        1          3        1        1        0\n",
       "22    42175    87        1          3        0        0        1\n",
       "23    53825   135        3          1        4        1        1\n",
       "24    23725   101        1          1        3        0        0\n",
       "25    33275   173        1          4        2        0        0\n",
       "26    61700    10        2          3        0        1        1\n",
       "27    24700   248        3          2        1        0        0\n",
       "28    54200   218        3          5        4        0        1\n",
       "29    56550    84        1          4        4        1        1\n",
       "..      ...   ...      ...        ...      ...      ...      ...\n",
       "970   56700   238        3          3        4        0        1\n",
       "971   34875   237        1          4        2        0        0\n",
       "972   31600     6        2          5        0        0        0\n",
       "973   44400   248        2          4        4        0        0\n",
       "974   33975    79        1          3        1        0        0\n",
       "975   56650    98        3          1        1        0        1\n",
       "976   49600   124        2          4        1        0        1\n",
       "977   39325   105        3          4        1        1        0\n",
       "978   43425   227        3          4        3        0        0\n",
       "979   35000   110        3          1        1        1        0\n",
       "980   22375    55        2          5        3        0        0\n",
       "981   27550   172        2          1        3        0        0\n",
       "982   34975   159        2          4        2        0        0\n",
       "983   54325   243        1          4        1        0        1\n",
       "984   41275   223        1          4        2        0        1\n",
       "985   63150   188        3          4        0        0        1\n",
       "986   40825    53        1          5        0        1        0\n",
       "987   29500   122        2          4        1        0        0\n",
       "988   54775   113        1          1        4        1        1\n",
       "989   35725   121        2          3        4        1        0\n",
       "990   29125   207        2          4        0        0        0\n",
       "991   46125    55        1          2        3        0        1\n",
       "992   25850    86        2          2        1        0        0\n",
       "993   41350   226        1          2        2        1        0\n",
       "994   46600    36        2          2        1        1        1\n",
       "995   29150    48        1          5        4        0        0\n",
       "996   43550   112        2          2        3        0        1\n",
       "997   56575   185        3          4        4        0        1\n",
       "998   56075   185        2          3        1        0        1\n",
       "999   13350    94        1          1        3        0        0\n",
       "\n",
       "[1000 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": [
       "(1000, 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>precos</th>\n",
       "      <th>area</th>\n",
       "      <th>garagem</th>\n",
       "      <th>banheiros</th>\n",
       "      <th>lareira</th>\n",
       "      <th>marmore</th>\n",
       "      <th>andares</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1000.00</td>\n",
       "      <td>1000.00</td>\n",
       "      <td>1000.00</td>\n",
       "      <td>1000.00</td>\n",
       "      <td>1000.00</td>\n",
       "      <td>1000.00</td>\n",
       "      <td>1000.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>41985.60</td>\n",
       "      <td>124.33</td>\n",
       "      <td>2.01</td>\n",
       "      <td>3.00</td>\n",
       "      <td>2.03</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>12140.39</td>\n",
       "      <td>72.39</td>\n",
       "      <td>0.81</td>\n",
       "      <td>1.43</td>\n",
       "      <td>1.42</td>\n",
       "      <td>0.47</td>\n",
       "      <td>0.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>13150.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>33112.50</td>\n",
       "      <td>60.75</td>\n",
       "      <td>1.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>41725.00</td>\n",
       "      <td>123.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>51175.00</td>\n",
       "      <td>187.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>4.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>73675.00</td>\n",
       "      <td>249.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>5.00</td>\n",
       "      <td>4.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         precos     area  garagem  banheiros  lareira  marmore  andares\n",
       "count   1000.00  1000.00  1000.00    1000.00  1000.00  1000.00  1000.00\n",
       "mean   41985.60   124.33     2.01       3.00     2.03     0.33     0.48\n",
       "std    12140.39    72.39     0.81       1.43     1.42     0.47     0.50\n",
       "min    13150.00     1.00     1.00       1.00     0.00     0.00     0.00\n",
       "25%    33112.50    60.75     1.00       2.00     1.00     0.00     0.00\n",
       "50%    41725.00   123.00     2.00       3.00     2.00     0.00     0.00\n",
       "75%    51175.00   187.00     3.00       4.00     3.00     1.00     1.00\n",
       "max    73675.00   249.00     3.00       5.00     4.00     1.00     1.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>\n",
    "\n",
    "### Observe as correlações entre as variáveis:\n",
    "<ul style='font-size: 16px; line-height: 2; text-align: justify;'>\n",
    "    <li>Quais são mais correlacionadas com a variável dependete (Preço)?</li>\n",
    "    <li>Qual o relacionamento entre elas (positivo ou negativo)?</li>\n",
    "    <li>Existe correlação forte entre as variáveis explicativas?</li>\n",
    "</ul>"
   ]
  },
  {
   "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>precos</th>\n",
       "      <th>area</th>\n",
       "      <th>garagem</th>\n",
       "      <th>banheiros</th>\n",
       "      <th>lareira</th>\n",
       "      <th>marmore</th>\n",
       "      <th>andares</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>precos</th>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.1177</td>\n",
       "      <td>0.1028</td>\n",
       "      <td>0.1244</td>\n",
       "      <td>0.1072</td>\n",
       "      <td>0.4308</td>\n",
       "      <td>0.6315</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>area</th>\n",
       "      <td>0.1177</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>-0.0075</td>\n",
       "      <td>-0.0114</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>-0.0153</td>\n",
       "      <td>-0.0180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>garagem</th>\n",
       "      <td>0.1028</td>\n",
       "      <td>-0.0075</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.0671</td>\n",
       "      <td>0.0605</td>\n",
       "      <td>-0.0156</td>\n",
       "      <td>-0.0206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>banheiros</th>\n",
       "      <td>0.1244</td>\n",
       "      <td>-0.0114</td>\n",
       "      <td>0.0671</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.0484</td>\n",
       "      <td>-0.0253</td>\n",
       "      <td>-0.0182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lareira</th>\n",
       "      <td>0.1072</td>\n",
       "      <td>0.0121</td>\n",
       "      <td>0.0605</td>\n",
       "      <td>0.0484</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>0.0296</td>\n",
       "      <td>-0.0035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>marmore</th>\n",
       "      <td>0.4308</td>\n",
       "      <td>-0.0153</td>\n",
       "      <td>-0.0156</td>\n",
       "      <td>-0.0253</td>\n",
       "      <td>0.0296</td>\n",
       "      <td>1.0000</td>\n",
       "      <td>-0.0065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>andares</th>\n",
       "      <td>0.6315</td>\n",
       "      <td>-0.0180</td>\n",
       "      <td>-0.0206</td>\n",
       "      <td>-0.0182</td>\n",
       "      <td>-0.0035</td>\n",
       "      <td>-0.0065</td>\n",
       "      <td>1.0000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           precos    area  garagem  banheiros  lareira  marmore  andares\n",
       "precos     1.0000  0.1177   0.1028     0.1244   0.1072   0.4308   0.6315\n",
       "area       0.1177  1.0000  -0.0075    -0.0114   0.0121  -0.0153  -0.0180\n",
       "garagem    0.1028 -0.0075   1.0000     0.0671   0.0605  -0.0156  -0.0206\n",
       "banheiros  0.1244 -0.0114   0.0671     1.0000   0.0484  -0.0253  -0.0182\n",
       "lareira    0.1072  0.0121   0.0605     0.0484   1.0000   0.0296  -0.0035\n",
       "marmore    0.4308 -0.0153  -0.0156    -0.0253   0.0296   1.0000  -0.0065\n",
       "andares    0.6315 -0.0180  -0.0206    -0.0182  -0.0035  -0.0065   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": [
    "<img width='700px' src='../Dados/img/Box-Plot.png'>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Importando biblioteca seaborn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Configure o estilo e cor dos gráficos (opcional)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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": "markdown",
   "metadata": {},
   "source": [
    "## Box plot da variável *dependente* (y)\n",
    "\n",
    "\n",
    "### Avalie o comportamento da distribuição da variável dependente:\n",
    "<ul style='font-size: 16px; line-height: 2; text-align: justify;'>\n",
    "    <li>Parecem existir valores discrepantes (outliers)?</li>\n",
    "    <li>O box plot apresenta alguma tendência?</li>\n",
    "</ul>"
   ]
  },
  {
   "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 0x18108649e10>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 13836 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(data=dados['precos'], orient='v', width=0.2)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Preço dos Imóveis', fontsize=20)\n",
    "ax.set_ylabel('$', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Investigando a variável *dependente* (y) juntamente com outras característica\n",
    "\n",
    "Faça um box plot da variável dependente em conjunto com cada variável explicativa (somente as categóricas).\n",
    "\n",
    "### Avalie o comportamento da distribuição da variável dependente com cada variável explicativa categórica:\n",
    "<ul style='font-size: 16px; line-height: 2; text-align: justify;'>\n",
    "    <li>As estatísticas apresentam mudança significativa entre as categorias?</li>\n",
    "    <li>O box plot apresenta alguma tendência bem definida?</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Box-plot (Preço X Garagem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x18108ade240>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 21132 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(y='precos', x='garagem', data=dados, orient='v', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Preço dos Imóveis', fontsize=20)\n",
    "ax.set_ylabel('$', fontsize=16)\n",
    "ax.set_xlabel('Número de Vagas de Garagem', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Box-plot (Preço X Banheiros)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x181089928d0>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 19952 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(y='precos', x='banheiros', data=dados, orient='v', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Preço dos Imóveis', fontsize=20)\n",
    "ax.set_ylabel('$', fontsize=16)\n",
    "ax.set_xlabel('Número de Banheiros', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Box-plot (Preço X Lareira)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x18108a431d0>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 19232 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(y='precos', x='lareira', data=dados, orient='v', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Preço dos Imóveis', fontsize=20)\n",
    "ax.set_ylabel('$', fontsize=16)\n",
    "ax.set_xlabel('Número de Lareiras', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Box-plot (Preço X Acabamento em Mármore)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x18108c49908>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 19388 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(y='precos', x='marmore', data=dados, orient='v', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Preço dos Imóveis', fontsize=20)\n",
    "ax.set_ylabel('$', fontsize=16)\n",
    "ax.set_xlabel('Acabamento em Mármore', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Box-plot (Preço X Andares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x18108cadc18>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 17996 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(y='precos', x='andares', data=dados, orient='v', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Preço dos Imóveis', fontsize=20)\n",
    "ax.set_ylabel('$', fontsize=16)\n",
    "ax.set_xlabel('Mais de um Andar', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Distribuição de frequências da variável *dependente* (y)\n",
    "\n",
    "Construa um histograma da variável dependente (Preço).\n",
    "\n",
    "### Avalie:\n",
    "<ul style='font-size: 16px; line-height: 2; text-align: justify;'>\n",
    "    <li>A distribuição de frequências da variável dependente parece ser assimétrica?</li>\n",
    "    <li>É possível supor que a variável dependente segue uma distribuição normal?</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://seaborn.pydata.org/generated/seaborn.distplot.html?highlight=distplot#seaborn.distplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x18108d0ef28>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 39056 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.distplot(dados['precos'])\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Distribuição de Frequências', fontsize=20)\n",
    "ax.set_ylabel('Frequências', fontsize=16)\n",
    "ax.set_xlabel('$', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gráficos de dispersão entre as variáveis do dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plotando o pairplot fixando somente uma variável no eixo y\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.pairplot.html?highlight=pairplot#seaborn.pairplot\n",
    "\n",
    "Plote gráficos de dispersão da variável dependente contra cada variável explicativa. Utilize o pairplot da biblioteca seaborn para isso.\n",
    "\n",
    "Plote o mesmo gráfico utilizando o parâmetro kind='reg'.\n",
    "\n",
    "### Avalie:\n",
    "<ul style='font-size: 16px; line-height: 2; text-align: justify;'>\n",
    "    <li>É possível identificar alguma relação linear entre as variáveis?</li>\n",
    "    <li>A relação é positiva ou negativa?</li>\n",
    "    <li>Compare com os resultados obtidos na matriz de correlação.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x18108d07e48>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 75260 characters]\n",
      "text/plain": [
       "<Figure size 1080x180 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.pairplot(dados, y_vars='precos', x_vars=['area', 'garagem', 'banheiros', 'lareira', 'marmore', 'andares'])\n",
    "ax.fig.suptitle('Dispersão entre as Variáveis', fontsize=20, y=1.05)\n",
    "ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x1810a4dbcf8>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 53860 characters]\n",
      "text/plain": [
       "<Figure size 1080x180 with 6 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.pairplot(dados, y_vars='precos', x_vars=['area', 'garagem', 'banheiros', 'lareira', 'marmore', 'andares'], 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;'>Estimando um Modelo de Regressão Linear</font>\n",
    "<hr style='border: 2px solid red;'>"
   ]
  },
  {
   "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": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando uma Series (pandas) para armazenar a variável dependente (y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = dados['precos']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando um DataFrame (pandas) para armazenar as variáveis explicativas (X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = dados[['area', 'garagem', 'banheiros', 'lareira', 'marmore', 'andares']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Criando os datasets de treino e de teste"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "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": [
    "## 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": 23,
   "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": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "modelo = LinearRegression()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Utilizando o método *fit()* para estimar o 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": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)"
      ]
     },
     "execution_count": 25,
     "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",
    "\n",
    "### Avalie:\n",
    "<ul style='font-size: 16px; line-height: 2; text-align: justify;'>\n",
    "    <li>O modelo apresenta um bom ajuste?</li>\n",
    "    <li>Você lembra o que representa o R²?</li>\n",
    "    <li>Qual medida podemos tomar para melhorar essa estatística?</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² = 0.64\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()*\n",
    "\n",
    "https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html#sklearn.linear_model.LinearRegression.predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "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": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R² = 0.67\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": [
    "## Criando um simulador simples\n",
    "\n",
    "Crie um simulador que gere estimativas de preço a partir de um conjunto de informações de um imóvel."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$ 46389.80\n"
     ]
    }
   ],
   "source": [
    "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(modelo.predict(entrada)[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='red' style='font-size: 30px;'>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",
    "### 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": "markdown",
   "metadata": {},
   "source": [
    "## Obtendo métricas para o modelo com Temperatura Máxima"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "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>50197019.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>REQM</th>\n",
       "      <td>7084.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>R²</th>\n",
       "      <td>0.67</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         Métricas\n",
       "EQM   50197019.50\n",
       "REQM      7084.99\n",
       "R²           0.67"
      ]
     },
     "execution_count": 30,
     "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": [
    "## Importando a biblioteca pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Salvando o modelo estimado"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "output = open('modelo_preço', '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_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]))\n",
    "```\n",
    "\n",
    "<h4 style='color: red; font-weight: normal'>Out [1]:</h4>\n",
    "\n",
    "```\n",
    "$ 46389.80\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.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
