Files
sensoriamento-rgb-svm/trabalho/amostra/trabalho_svm_rgb.html
Cesa-V 00c5e86967 Versiona HTMLs renderizados do Quarto
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-17 10:54:27 -03:00

2701 lines
215 KiB
HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.9.37">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="1T Generoso">
<meta name="author" content="1T João Marcos">
<meta name="author" content="1T Vitor Cesa">
<title>Classificacao de Imagens RGB com SVM</title>
<style>
/* Default styles provided by pandoc.
** See https://pandoc.org/MANUAL.html#variables-for-html for config info.
*/
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
html { -webkit-text-size-adjust: 100%; }
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="trabalho_svm_rgb_files/libs/clipboard/clipboard.min.js"></script>
<script src="trabalho_svm_rgb_files/libs/quarto-html/quarto.js" type="module"></script>
<script src="trabalho_svm_rgb_files/libs/quarto-html/tabsets/tabsets.js" type="module"></script>
<script src="trabalho_svm_rgb_files/libs/quarto-html/popper.min.js"></script>
<script src="trabalho_svm_rgb_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="trabalho_svm_rgb_files/libs/quarto-html/anchor.min.js"></script>
<link href="trabalho_svm_rgb_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="trabalho_svm_rgb_files/libs/quarto-html/quarto-syntax-highlighting-7f8f88aac4f3542376d5c11b86a4c14d.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="trabalho_svm_rgb_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="trabalho_svm_rgb_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="trabalho_svm_rgb_files/libs/bootstrap/bootstrap-67270d1595a62d14f123c043d4fb2f18.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">
<link href="trabalho_svm_rgb_files/libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet">
<script src="trabalho_svm_rgb_files/libs/pagedtable-1.1/js/pagedtable.js"></script>
</head>
<body class="quarto-light">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#introducao" id="toc-introducao" class="nav-link active" data-scroll-target="#introducao"><span class="header-section-number">1</span> Introducao</a></li>
<li><a href="#preparacao-do-ambiente" id="toc-preparacao-do-ambiente" class="nav-link" data-scroll-target="#preparacao-do-ambiente"><span class="header-section-number">2</span> Preparacao do ambiente</a></li>
<li><a href="#caminho-do-dataset" id="toc-caminho-do-dataset" class="nav-link" data-scroll-target="#caminho-do-dataset"><span class="header-section-number">3</span> Caminho do dataset</a></li>
<li><a href="#leitura-dos-arquivos-de-imagem" id="toc-leitura-dos-arquivos-de-imagem" class="nav-link" data-scroll-target="#leitura-dos-arquivos-de-imagem"><span class="header-section-number">4</span> Leitura dos arquivos de imagem</a></li>
<li><a href="#amostra-balanceada" id="toc-amostra-balanceada" class="nav-link" data-scroll-target="#amostra-balanceada"><span class="header-section-number">5</span> Amostra balanceada</a></li>
<li><a href="#modelo-pre-treinado-transfer-learning" id="toc-modelo-pre-treinado-transfer-learning" class="nav-link" data-scroll-target="#modelo-pre-treinado-transfer-learning"><span class="header-section-number">6</span> Modelo pre-treinado (Transfer Learning)</a></li>
<li><a href="#extracao-de-atributos-por-transfer-learning" id="toc-extracao-de-atributos-por-transfer-learning" class="nav-link" data-scroll-target="#extracao-de-atributos-por-transfer-learning"><span class="header-section-number">7</span> Extracao de atributos por Transfer Learning</a></li>
<li><a href="#separacao-entre-treino-e-teste" id="toc-separacao-entre-treino-e-teste" class="nav-link" data-scroll-target="#separacao-entre-treino-e-teste"><span class="header-section-number">8</span> Separacao entre treino e teste</a></li>
<li><a href="#pre-processamento" id="toc-pre-processamento" class="nav-link" data-scroll-target="#pre-processamento"><span class="header-section-number">9</span> Pre-processamento</a></li>
<li><a href="#modelo-1-svm-linear" id="toc-modelo-1-svm-linear" class="nav-link" data-scroll-target="#modelo-1-svm-linear"><span class="header-section-number">10</span> Modelo 1: SVM linear</a></li>
<li><a href="#modelo-2-svm-radial" id="toc-modelo-2-svm-radial" class="nav-link" data-scroll-target="#modelo-2-svm-radial"><span class="header-section-number">11</span> Modelo 2: SVM radial</a></li>
<li><a href="#modelo-3-ajuste-de-hiperparametros-do-svm-radial" id="toc-modelo-3-ajuste-de-hiperparametros-do-svm-radial" class="nav-link" data-scroll-target="#modelo-3-ajuste-de-hiperparametros-do-svm-radial"><span class="header-section-number">12</span> Modelo 3: Ajuste de hiperparametros do SVM radial</a></li>
<li><a href="#modelo-4-pca-svm-radial" id="toc-modelo-4-pca-svm-radial" class="nav-link" data-scroll-target="#modelo-4-pca-svm-radial"><span class="header-section-number">13</span> Modelo 4: PCA + SVM radial</a></li>
<li><a href="#modelo-5-random-forest" id="toc-modelo-5-random-forest" class="nav-link" data-scroll-target="#modelo-5-random-forest"><span class="header-section-number">14</span> Modelo 5: Random Forest</a></li>
<li><a href="#comparacao-dos-modelos" id="toc-comparacao-dos-modelos" class="nav-link" data-scroll-target="#comparacao-dos-modelos"><span class="header-section-number">15</span> Comparacao dos modelos</a></li>
<li><a href="#melhor-modelo" id="toc-melhor-modelo" class="nav-link" data-scroll-target="#melhor-modelo"><span class="header-section-number">16</span> Melhor modelo</a></li>
<li><a href="#importancia-das-variaveis" id="toc-importancia-das-variaveis" class="nav-link" data-scroll-target="#importancia-das-variaveis"><span class="header-section-number">17</span> Importancia das variaveis</a></li>
<li><a href="#salvamento-dos-resultados" id="toc-salvamento-dos-resultados" class="nav-link" data-scroll-target="#salvamento-dos-resultados"><span class="header-section-number">18</span> Salvamento dos resultados</a></li>
<li><a href="#validacao-cruzada-k-fold" id="toc-validacao-cruzada-k-fold" class="nav-link" data-scroll-target="#validacao-cruzada-k-fold"><span class="header-section-number">19</span> Validacao Cruzada K-Fold</a></li>
<li><a href="#comparacao-holdout-vs-k-fold" id="toc-comparacao-holdout-vs-k-fold" class="nav-link" data-scroll-target="#comparacao-holdout-vs-k-fold"><span class="header-section-number">20</span> Comparacao: Holdout vs K-Fold</a></li>
<li><a href="#discussao" id="toc-discussao" class="nav-link" data-scroll-target="#discussao"><span class="header-section-number">21</span> Discussao</a></li>
<li><a href="#conclusao" id="toc-conclusao" class="nav-link" data-scroll-target="#conclusao"><span class="header-section-number">22</span> Conclusao</a></li>
</ul>
</nav>
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Classificacao de Imagens RGB com SVM</h1>
<p class="subtitle lead">CEAO-802 - Metodos de Analise de Dados</p>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Authors</div>
<div class="quarto-title-meta-contents">
<p>1T Generoso </p>
<p>1T João Marcos </p>
<p>1T Vitor Cesa </p>
</div>
</div>
</div>
</header>
<section id="introducao" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introducao</h1>
<p>Este trabalho aplica tecnicas de classificacao supervisionada ao dataset RGB disponibilizado na disciplina CEAO-802 - Metodos de Analise de Dados.</p>
<p>O documento de datasets apresenta um problema de classificacao de cenas urbanas com imagens RGB e TIR coletadas por drone sobre Guaratingueta (SP), com 13 classes de cobertura urbana. Nesta analise sera utilizada apenas a parte RGB do dataset, armazenada localmente em <code>D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB</code>.</p>
<p>A abordagem adotada segue a sugestao do material da disciplina: em vez de extrair atributos estatisticos simples (media, desvio, histogramas), serao utilizadas <strong>redes neurais convolucionais pre-treinadas</strong> como extratores de atributos (<em>transfer learning</em>). A rede MobileNetV2, pre-treinada no ImageNet, e carregada sem a camada de classificacao final. Cada imagem e redimensionada para <code>224x224</code> pixels e processada pela rede, que produz um vetor de 1280 atributos representando caracteristicas visuais de alto nivel aprendidas em milhoes de imagens. Esses vetores sao entao usados como entrada para os classificadores SVM e Random Forest.</p>
<p>A aula de Support Vector Machine (SVM) apresentou o metodo como um classificador baseado na busca de um hiperplano de separacao entre classes. A ideia central e encontrar uma fronteira que maximize a margem entre os grupos. Quando os dados nao sao linearmente separaveis, o SVM pode usar variaveis de folga, controladas pelo parametro <code>cost</code>, e tambem kernels, como o kernel radial ou RBF, para criar fronteiras de decisao nao lineares por meio do chamado <em>kernel trick</em>.</p>
<p>Como a pasta RGB possui grande volume de dados, sera usada uma amostra estratificada e balanceada de 50 imagens por classe. Essa escolha torna o processamento viavel em um computador comum, preservando a comparacao entre as classes.</p>
<p>Neste relatorio, serao avaliados:</p>
<ol type="1">
<li>SVM com kernel linear;</li>
<li>SVM com kernel radial;</li>
<li>SVM radial com ajuste simples de hiperparametros;</li>
<li>PCA + SVM radial;</li>
<li>Random Forest, como modelo de comparacao.</li>
</ol>
</section>
<section id="preparacao-do-ambiente" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Preparacao do ambiente</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 1. Instalacao e carregamento dos pacotes</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>pacotes <span class="ot">&lt;-</span> <span class="fu">c</span>(</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"tidyverse"</span>,</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"keras3"</span>,</span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"caret"</span>,</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"e1071"</span>,</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"randomForest"</span>,</span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> <span class="st">"knitr"</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>pacotes_faltando <span class="ot">&lt;-</span> <span class="fu">setdiff</span>(pacotes, <span class="fu">rownames</span>(<span class="fu">installed.packages</span>()))</span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">length</span>(pacotes_faltando) <span class="sc">&gt;</span> <span class="dv">0</span>) {</span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">install.packages</span>(pacotes_faltando)</span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-21"><a href="#cb1-21" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(keras3)</span>
<span id="cb1-22"><a href="#cb1-22" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(caret)</span>
<span id="cb1-23"><a href="#cb1-23" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(e1071)</span>
<span id="cb1-24"><a href="#cb1-24" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(randomForest)</span>
<span id="cb1-25"><a href="#cb1-25" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(knitr)</span>
<span id="cb1-26"><a href="#cb1-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-27"><a href="#cb1-27" aria-hidden="true" tabindex="-1"></a>backend_ativo <span class="ot">&lt;-</span> <span class="fu">tryCatch</span>(<span class="fu">config_backend</span>(), <span class="at">error =</span> <span class="cf">function</span>(e) <span class="cn">NA_character_</span>)</span>
<span id="cb1-28"><a href="#cb1-28" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Backend keras3:"</span>, backend_ativo, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Backend keras3: tensorflow </code></pre>
</div>
</div>
</section>
<section id="caminho-do-dataset" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Caminho do dataset</h1>
<p>Em R, e mais seguro usar barras <code>/</code> no caminho, mesmo no Windows.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 2. Caminho do dataset RGB</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a>caminho_rgb <span class="ot">&lt;-</span> <span class="st">"D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB"</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="sc">!</span><span class="fu">dir.exists</span>(caminho_rgb)) {</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"A pasta do dataset RGB nao foi encontrada. Verifique o caminho informado em caminho_rgb."</span>)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a>caminho_rgb</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB"</code></pre>
</div>
</div>
</section>
<section id="leitura-dos-arquivos-de-imagem" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Leitura dos arquivos de imagem</h1>
<p>O codigo procura imagens dentro da pasta RGB. Se o dataset estiver organizado em subpastas, o nome da subpasta sera usado como classe. No dataset usado neste trabalho, os arquivos TIFF estao diretamente na pasta <code>RGB</code>, entao a classe e inferida pelo prefixo do nome do arquivo, como <code>C1</code>, <code>C2</code>, …, <code>C13</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 3. Listar imagens</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>extensoes_imagem <span class="ot">&lt;-</span> <span class="st">"</span><span class="sc">\\</span><span class="st">.(jpg|jpeg|png|bmp|tif|tiff)$"</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a>arquivos <span class="ot">&lt;-</span> <span class="fu">list.files</span>(</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="at">path =</span> caminho_rgb,</span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="at">pattern =</span> extensoes_imagem,</span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="at">recursive =</span> <span class="cn">TRUE</span>,</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> <span class="at">full.names =</span> <span class="cn">TRUE</span>,</span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="at">ignore.case =</span> <span class="cn">TRUE</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">length</span>(arquivos) <span class="sc">==</span> <span class="dv">0</span>) {</span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"Nenhuma imagem foi encontrada na pasta RGB."</span>)</span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a><span class="fu">length</span>(arquivos)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1248</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 4. Criar metadados</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a>obter_classe <span class="ot">&lt;-</span> <span class="cf">function</span>(arquivo, raiz) {</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> arquivo_norm <span class="ot">&lt;-</span> <span class="fu">normalizePath</span>(arquivo, <span class="at">winslash =</span> <span class="st">"/"</span>, <span class="at">mustWork =</span> <span class="cn">FALSE</span>)</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> raiz_norm <span class="ot">&lt;-</span> <span class="fu">normalizePath</span>(raiz, <span class="at">winslash =</span> <span class="st">"/"</span>, <span class="at">mustWork =</span> <span class="cn">FALSE</span>)</span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> nome_sem_extensao <span class="ot">&lt;-</span> tools<span class="sc">::</span><span class="fu">file_path_sans_ext</span>(<span class="fu">basename</span>(arquivo))</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> classe_inferida <span class="ot">&lt;-</span> stringr<span class="sc">::</span><span class="fu">str_extract</span>(nome_sem_extensao, <span class="st">"^C</span><span class="sc">\\</span><span class="st">d+"</span>)</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">is.na</span>(classe_inferida)) {</span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(classe_inferida)</span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> caminho_relativo <span class="ot">&lt;-</span> stringr<span class="sc">::</span><span class="fu">str_remove</span>(</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> arquivo_norm,</span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"^"</span>, stringr<span class="sc">::</span><span class="fu">fixed</span>(raiz_norm), <span class="st">"/?"</span>)</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> partes <span class="ot">&lt;-</span> <span class="fu">strsplit</span>(caminho_relativo, <span class="st">"/"</span>, <span class="at">fixed =</span> <span class="cn">TRUE</span>)[[<span class="dv">1</span>]]</span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(partes) <span class="sc">&gt;=</span> <span class="dv">2</span>) {</span>
<span id="cb7-24"><a href="#cb7-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">return</span>(partes[<span class="dv">1</span>])</span>
<span id="cb7-25"><a href="#cb7-25" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb7-26"><a href="#cb7-26" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb7-27"><a href="#cb7-27" aria-hidden="true" tabindex="-1"></a> stringr<span class="sc">::</span><span class="fu">str_extract</span>(nome_sem_extensao, <span class="st">"^[^_-]+"</span>)</span>
<span id="cb7-28"><a href="#cb7-28" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb7-29"><a href="#cb7-29" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-30"><a href="#cb7-30" aria-hidden="true" tabindex="-1"></a>metadados_completo <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb7-31"><a href="#cb7-31" aria-hidden="true" tabindex="-1"></a> <span class="at">arquivo =</span> arquivos,</span>
<span id="cb7-32"><a href="#cb7-32" aria-hidden="true" tabindex="-1"></a> <span class="at">classe =</span> <span class="fu">map_chr</span>(arquivos, obter_classe, <span class="at">raiz =</span> caminho_rgb)</span>
<span id="cb7-33"><a href="#cb7-33" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%&gt;%</span></span>
<span id="cb7-34"><a href="#cb7-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb7-35"><a href="#cb7-35" aria-hidden="true" tabindex="-1"></a> <span class="at">classe =</span> <span class="fu">factor</span>(classe, <span class="at">levels =</span> <span class="fu">paste0</span>(<span class="st">"C"</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">13</span>)),</span>
<span id="cb7-36"><a href="#cb7-36" aria-hidden="true" tabindex="-1"></a> <span class="at">tamanho_mb =</span> <span class="fu">file.info</span>(arquivo)<span class="sc">$</span>size <span class="sc">/</span> <span class="dv">1024</span><span class="sc">^</span><span class="dv">2</span></span>
<span id="cb7-37"><a href="#cb7-37" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%&gt;%</span></span>
<span id="cb7-38"><a href="#cb7-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(classe, arquivo)</span>
<span id="cb7-39"><a href="#cb7-39" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-40"><a href="#cb7-40" aria-hidden="true" tabindex="-1"></a>resumo_dataset <span class="ot">&lt;-</span> metadados_completo <span class="sc">%&gt;%</span></span>
<span id="cb7-41"><a href="#cb7-41" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(classe) <span class="sc">%&gt;%</span></span>
<span id="cb7-42"><a href="#cb7-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(</span>
<span id="cb7-43"><a href="#cb7-43" aria-hidden="true" tabindex="-1"></a> <span class="at">n_imagens =</span> <span class="fu">n</span>(),</span>
<span id="cb7-44"><a href="#cb7-44" aria-hidden="true" tabindex="-1"></a> <span class="at">tamanho_total_mb =</span> <span class="fu">sum</span>(tamanho_mb, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb7-45"><a href="#cb7-45" aria-hidden="true" tabindex="-1"></a> <span class="at">tamanho_medio_mb =</span> <span class="fu">mean</span>(tamanho_mb, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb7-46"><a href="#cb7-46" aria-hidden="true" tabindex="-1"></a> <span class="at">.groups =</span> <span class="st">"drop"</span></span>
<span id="cb7-47"><a href="#cb7-47" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-48"><a href="#cb7-48" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-49"><a href="#cb7-49" aria-hidden="true" tabindex="-1"></a>resumo_dataset <span class="sc">%&gt;%</span></span>
<span id="cb7-50"><a href="#cb7-50" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">where</span>(is.numeric), <span class="sc">~</span> <span class="fu">round</span>(.x, <span class="dv">2</span>))) <span class="sc">%&gt;%</span></span>
<span id="cb7-51"><a href="#cb7-51" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">classe</th>
<th style="text-align: right;">n_imagens</th>
<th style="text-align: right;">tamanho_total_mb</th>
<th style="text-align: right;">tamanho_medio_mb</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">C1</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="even">
<td style="text-align: left;">C2</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C3</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="even">
<td style="text-align: left;">C4</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C5</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="even">
<td style="text-align: left;">C6</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C7</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="even">
<td style="text-align: left;">C8</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C9</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="even">
<td style="text-align: left;">C10</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C11</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="even">
<td style="text-align: left;">C12</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C13</td>
<td style="text-align: right;">96</td>
<td style="text-align: right;">3388.31</td>
<td style="text-align: right;">35.29</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="amostra-balanceada" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Amostra balanceada</h1>
<p>A amostragem estratificada foi usada para manter a mesma quantidade de imagens em cada classe. Isso evita que classes maiores dominem o treinamento e reduz o tempo de processamento.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 5. Criar amostra balanceada por classe</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>max_imagens_por_classe <span class="ot">&lt;-</span> <span class="dv">50</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a>metadados <span class="ot">&lt;-</span> metadados_completo <span class="sc">%&gt;%</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(classe) <span class="sc">%&gt;%</span></span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_modify</span>(<span class="sc">~</span> {</span>
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a> qtd <span class="ot">&lt;-</span> <span class="fu">min</span>(max_imagens_por_classe, <span class="fu">nrow</span>(.x))</span>
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">slice_sample</span>(.x, <span class="at">n =</span> qtd)</span>
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> }) <span class="sc">%&gt;%</span></span>
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">ungroup</span>() <span class="sc">%&gt;%</span></span>
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">classe =</span> <span class="fu">factor</span>(classe, <span class="at">levels =</span> <span class="fu">paste0</span>(<span class="st">"C"</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">13</span>)))</span>
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-18"><a href="#cb8-18" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Total de imagens no dataset completo:"</span>, <span class="fu">nrow</span>(metadados_completo), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Total de imagens no dataset completo: 1248 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Total de imagens na amostra:"</span>, <span class="fu">nrow</span>(metadados), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Total de imagens na amostra: 650 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>metadados <span class="sc">%&gt;%</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(classe, <span class="at">name =</span> <span class="st">"n_imagens_amostra"</span>) <span class="sc">%&gt;%</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(classe) <span class="sc">%&gt;%</span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">classe</th>
<th style="text-align: right;">n_imagens_amostra</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">C1</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C2</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C3</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C4</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C5</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C6</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C7</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C8</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C9</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C10</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C11</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C12</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C13</td>
<td style="text-align: right;">50</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(metadados, <span class="fu">aes</span>(<span class="at">x =</span> classe)) <span class="sc">+</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">fill =</span> <span class="st">"#2C7FB8"</span>) <span class="sc">+</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Distribuicao de imagens por classe na amostra"</span>,</span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Classe"</span>,</span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Numero de imagens"</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="modelo-pre-treinado-transfer-learning" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> Modelo pre-treinado (Transfer Learning)</h1>
<p>A estrategia de <em>transfer learning</em> consiste em usar uma rede neural convolucional (CNN) ja treinada em um grande dataset — neste caso, o ImageNet, com mais de 1 milhao de imagens e 1000 classes — e aproveitá-la como extrator de atributos.</p>
<p>A rede escolhida e a <strong>MobileNetV2</strong>, uma arquitetura compacta e eficiente desenvolvida pelo Google. Ela e carregada sem a camada de classificacao final (<code>include_top = FALSE</code>) e com pooling global medio (<code>pooling = "avg"</code>), o que produz um vetor de <strong>1280 atributos</strong> por imagem. Esses atributos representam caracteristicas visuais de alto nivel (bordas, texturas, formas, padroes complexos) que a rede aprendeu ao longo de seu treinamento no ImageNet.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 6. Carregar modelo pre-treinado MobileNetV2</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a><span class="fu">dir.create</span>(<span class="st">"outputs"</span>, <span class="at">showWarnings =</span> <span class="cn">FALSE</span>)</span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a>modelo_base <span class="ot">&lt;-</span> <span class="fu">application_mobilenet_v2</span>(</span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a> <span class="at">include_top =</span> <span class="cn">FALSE</span>,</span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a> <span class="at">weights =</span> <span class="st">"imagenet"</span>,</span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a> <span class="at">pooling =</span> <span class="st">"avg"</span>,</span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a> <span class="at">input_shape =</span> <span class="fu">c</span>(<span class="dv">224</span>L, <span class="dv">224</span>L, <span class="dv">3</span>L)</span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Modelo carregado:"</span>, modelo_base<span class="sc">$</span>name, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Modelo carregado: mobilenetv2_1.00_224 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Dimensao do vetor de features:"</span>, modelo_base<span class="sc">$</span>output_shape[[<span class="dv">2</span>]], <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Dimensao do vetor de features: 1280 </code></pre>
</div>
</div>
</section>
<section id="extracao-de-atributos-por-transfer-learning" class="level1" data-number="7">
<h1 data-number="7"><span class="header-section-number">7</span> Extracao de atributos por Transfer Learning</h1>
<p>Cada imagem e redimensionada para <code>224x224</code> pixels (formato padrao do MobileNetV2), normalizada com a funcao de preprocessamento especifica da rede e entao passada pela CNN. O vetor de saida e armazenado como atributos da imagem.</p>
<p>O processamento e feito em lotes (<em>batch</em>) para maior eficiencia, e o resultado e armazenado em cache para evitar reprocessamento em execucoes posteriores.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 7. Extracao de features via CNN pre-treinada (com cache)</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a>pasta_saida <span class="ot">&lt;-</span> <span class="st">"outputs/parcial"</span></span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a><span class="fu">dir.create</span>(pasta_saida, <span class="at">recursive =</span> <span class="cn">TRUE</span>, <span class="at">showWarnings =</span> <span class="cn">FALSE</span>)</span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a>arquivo_cache_features <span class="ot">&lt;-</span> <span class="fu">file.path</span>(pasta_saida, <span class="st">"features_rgb_mobilenetv2.rds"</span>)</span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a>arquivo_cache_parcial <span class="ot">&lt;-</span> <span class="fu">file.path</span>(pasta_saida, <span class="st">"features_rgb_mobilenetv2_temp.rds"</span>)</span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a>tamanho_imagem <span class="ot">&lt;-</span> <span class="dv">224</span>L</span>
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a>tamanho_lote <span class="ot">&lt;-</span> <span class="dv">16</span>L</span>
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a>extrair_features_batch <span class="ot">&lt;-</span> <span class="cf">function</span>(arquivos, modelo, <span class="at">tamanho =</span> <span class="dv">224</span>L, <span class="at">lote =</span> <span class="dv">16</span>L) {</span>
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a> n <span class="ot">&lt;-</span> <span class="fu">length</span>(arquivos)</span>
<span id="cb18-16"><a href="#cb18-16" aria-hidden="true" tabindex="-1"></a> resultados <span class="ot">&lt;-</span> <span class="fu">matrix</span>(<span class="cn">NA_real_</span>, <span class="at">nrow =</span> n, <span class="at">ncol =</span> <span class="fu">as.integer</span>(modelo<span class="sc">$</span>output_shape[[<span class="dv">2</span>]]))</span>
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-18"><a href="#cb18-18" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (inicio <span class="cf">in</span> <span class="fu">seq</span>(<span class="dv">1</span>, n, <span class="at">by =</span> lote)) {</span>
<span id="cb18-19"><a href="#cb18-19" aria-hidden="true" tabindex="-1"></a> fim <span class="ot">&lt;-</span> <span class="fu">min</span>(inicio <span class="sc">+</span> lote <span class="sc">-</span> <span class="dv">1</span>L, n)</span>
<span id="cb18-20"><a href="#cb18-20" aria-hidden="true" tabindex="-1"></a> bloco <span class="ot">&lt;-</span> arquivos[inicio<span class="sc">:</span>fim]</span>
<span id="cb18-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a> tam_bloco <span class="ot">&lt;-</span> <span class="fu">length</span>(bloco)</span>
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-23"><a href="#cb18-23" aria-hidden="true" tabindex="-1"></a> batch_array <span class="ot">&lt;-</span> <span class="fu">array</span>(<span class="dv">0</span>, <span class="at">dim =</span> <span class="fu">c</span>(tam_bloco, tamanho, tamanho, <span class="dv">3</span>L))</span>
<span id="cb18-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (j <span class="cf">in</span> <span class="fu">seq_len</span>(tam_bloco)) {</span>
<span id="cb18-26"><a href="#cb18-26" aria-hidden="true" tabindex="-1"></a> img <span class="ot">&lt;-</span> <span class="fu">image_load</span>(bloco[j], <span class="at">target_size =</span> <span class="fu">c</span>(tamanho, tamanho))</span>
<span id="cb18-27"><a href="#cb18-27" aria-hidden="true" tabindex="-1"></a> batch_array[j, , , ] <span class="ot">&lt;-</span> <span class="fu">image_to_array</span>(img)</span>
<span id="cb18-28"><a href="#cb18-28" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-29"><a href="#cb18-29" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-30"><a href="#cb18-30" aria-hidden="true" tabindex="-1"></a> batch_prep <span class="ot">&lt;-</span> (batch_array <span class="sc">/</span> <span class="fl">127.5</span>) <span class="sc">-</span> <span class="fl">1.0</span></span>
<span id="cb18-31"><a href="#cb18-31" aria-hidden="true" tabindex="-1"></a> features <span class="ot">&lt;-</span> <span class="fu">predict</span>(modelo, batch_prep, <span class="at">verbose =</span> <span class="dv">0</span>L)</span>
<span id="cb18-32"><a href="#cb18-32" aria-hidden="true" tabindex="-1"></a> resultados[inicio<span class="sc">:</span>fim, ] <span class="ot">&lt;-</span> features</span>
<span id="cb18-33"><a href="#cb18-33" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-34"><a href="#cb18-34" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-35"><a href="#cb18-35" aria-hidden="true" tabindex="-1"></a> resultados</span>
<span id="cb18-36"><a href="#cb18-36" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb18-37"><a href="#cb18-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-38"><a href="#cb18-38" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">file.exists</span>(arquivo_cache_features)) {</span>
<span id="cb18-39"><a href="#cb18-39" aria-hidden="true" tabindex="-1"></a> dados <span class="ot">&lt;-</span> <span class="fu">readRDS</span>(arquivo_cache_features)</span>
<span id="cb18-40"><a href="#cb18-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"Features carregadas do cache:"</span>, arquivo_cache_features, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb18-41"><a href="#cb18-41" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb18-42"><a href="#cb18-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"Extraindo features com MobileNetV2. Isso pode levar alguns minutos...</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb18-43"><a href="#cb18-43" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-44"><a href="#cb18-44" aria-hidden="true" tabindex="-1"></a> erros_idx <span class="ot">&lt;-</span> <span class="fu">integer</span>(<span class="dv">0</span>)</span>
<span id="cb18-45"><a href="#cb18-45" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-46"><a href="#cb18-46" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">file.exists</span>(arquivo_cache_parcial)) {</span>
<span id="cb18-47"><a href="#cb18-47" aria-hidden="true" tabindex="-1"></a> features_matrix <span class="ot">&lt;-</span> <span class="fu">readRDS</span>(arquivo_cache_parcial)</span>
<span id="cb18-48"><a href="#cb18-48" aria-hidden="true" tabindex="-1"></a> linhas_prontas <span class="ot">&lt;-</span> <span class="fu">which</span>(<span class="fu">complete.cases</span>(features_matrix))</span>
<span id="cb18-49"><a href="#cb18-49" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"Cache parcial encontrado:"</span>, <span class="fu">length</span>(linhas_prontas), <span class="st">"imagens ja processadas.</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb18-50"><a href="#cb18-50" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb18-51"><a href="#cb18-51" aria-hidden="true" tabindex="-1"></a> n_features <span class="ot">&lt;-</span> <span class="fu">as.integer</span>(modelo_base<span class="sc">$</span>output_shape[[<span class="dv">2</span>]])</span>
<span id="cb18-52"><a href="#cb18-52" aria-hidden="true" tabindex="-1"></a> features_matrix <span class="ot">&lt;-</span> <span class="fu">matrix</span>(<span class="cn">NA_real_</span>, <span class="at">nrow =</span> <span class="fu">nrow</span>(metadados), <span class="at">ncol =</span> n_features)</span>
<span id="cb18-53"><a href="#cb18-53" aria-hidden="true" tabindex="-1"></a> linhas_prontas <span class="ot">&lt;-</span> <span class="fu">integer</span>(<span class="dv">0</span>)</span>
<span id="cb18-54"><a href="#cb18-54" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-55"><a href="#cb18-55" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-56"><a href="#cb18-56" aria-hidden="true" tabindex="-1"></a> linhas_pendentes <span class="ot">&lt;-</span> <span class="fu">setdiff</span>(<span class="fu">seq_len</span>(<span class="fu">nrow</span>(metadados)), linhas_prontas)</span>
<span id="cb18-57"><a href="#cb18-57" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-58"><a href="#cb18-58" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(linhas_pendentes) <span class="sc">&gt;</span> <span class="dv">0</span>) {</span>
<span id="cb18-59"><a href="#cb18-59" aria-hidden="true" tabindex="-1"></a> <span class="cf">for</span> (inicio <span class="cf">in</span> <span class="fu">seq</span>(<span class="dv">1</span>, <span class="fu">length</span>(linhas_pendentes), <span class="at">by =</span> tamanho_lote)) {</span>
<span id="cb18-60"><a href="#cb18-60" aria-hidden="true" tabindex="-1"></a> fim <span class="ot">&lt;-</span> <span class="fu">min</span>(inicio <span class="sc">+</span> tamanho_lote <span class="sc">-</span> <span class="dv">1</span>L, <span class="fu">length</span>(linhas_pendentes))</span>
<span id="cb18-61"><a href="#cb18-61" aria-hidden="true" tabindex="-1"></a> idxs <span class="ot">&lt;-</span> linhas_pendentes[inicio<span class="sc">:</span>fim]</span>
<span id="cb18-62"><a href="#cb18-62" aria-hidden="true" tabindex="-1"></a> arquivos_bloco <span class="ot">&lt;-</span> metadados<span class="sc">$</span>arquivo[idxs]</span>
<span id="cb18-63"><a href="#cb18-63" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-64"><a href="#cb18-64" aria-hidden="true" tabindex="-1"></a> resultado <span class="ot">&lt;-</span> <span class="fu">tryCatch</span>(</span>
<span id="cb18-65"><a href="#cb18-65" aria-hidden="true" tabindex="-1"></a> <span class="fu">extrair_features_batch</span>(arquivos_bloco, modelo_base, tamanho_imagem, <span class="fu">length</span>(idxs)),</span>
<span id="cb18-66"><a href="#cb18-66" aria-hidden="true" tabindex="-1"></a> <span class="at">error =</span> <span class="cf">function</span>(e) {</span>
<span id="cb18-67"><a href="#cb18-67" aria-hidden="true" tabindex="-1"></a> <span class="fu">message</span>(<span class="st">"Erro no lote "</span>, inicio, <span class="st">"-"</span>, fim, <span class="st">": "</span>, <span class="fu">conditionMessage</span>(e))</span>
<span id="cb18-68"><a href="#cb18-68" aria-hidden="true" tabindex="-1"></a> erros_idx <span class="ot">&lt;&lt;-</span> <span class="fu">c</span>(erros_idx, idxs)</span>
<span id="cb18-69"><a href="#cb18-69" aria-hidden="true" tabindex="-1"></a> <span class="cn">NULL</span></span>
<span id="cb18-70"><a href="#cb18-70" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-71"><a href="#cb18-71" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb18-72"><a href="#cb18-72" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-73"><a href="#cb18-73" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">is.null</span>(resultado)) {</span>
<span id="cb18-74"><a href="#cb18-74" aria-hidden="true" tabindex="-1"></a> features_matrix[idxs, ] <span class="ot">&lt;-</span> resultado</span>
<span id="cb18-75"><a href="#cb18-75" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-76"><a href="#cb18-76" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-77"><a href="#cb18-77" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="fu">sprintf</span>(<span class="st">" Processadas: %d / %d</span><span class="sc">\r</span><span class="st">"</span>, <span class="fu">min</span>(fim, <span class="fu">length</span>(linhas_pendentes)), <span class="fu">length</span>(linhas_pendentes)))</span>
<span id="cb18-78"><a href="#cb18-78" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-79"><a href="#cb18-79" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (fim <span class="sc">%%</span> (tamanho_lote <span class="sc">*</span> <span class="dv">5</span>L) <span class="sc">==</span> <span class="dv">0</span>L) {</span>
<span id="cb18-80"><a href="#cb18-80" aria-hidden="true" tabindex="-1"></a> <span class="fu">saveRDS</span>(features_matrix, arquivo_cache_parcial)</span>
<span id="cb18-81"><a href="#cb18-81" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-82"><a href="#cb18-82" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-83"><a href="#cb18-83" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb18-84"><a href="#cb18-84" aria-hidden="true" tabindex="-1"></a> <span class="fu">saveRDS</span>(features_matrix, arquivo_cache_parcial)</span>
<span id="cb18-85"><a href="#cb18-85" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-86"><a href="#cb18-86" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-87"><a href="#cb18-87" aria-hidden="true" tabindex="-1"></a> imagens_ok <span class="ot">&lt;-</span> <span class="fu">complete.cases</span>(features_matrix)</span>
<span id="cb18-88"><a href="#cb18-88" aria-hidden="true" tabindex="-1"></a> n_features <span class="ot">&lt;-</span> <span class="fu">ncol</span>(features_matrix)</span>
<span id="cb18-89"><a href="#cb18-89" aria-hidden="true" tabindex="-1"></a> nomes_feat <span class="ot">&lt;-</span> <span class="fu">paste0</span>(<span class="st">"feat_"</span>, stringr<span class="sc">::</span><span class="fu">str_pad</span>(<span class="fu">seq_len</span>(n_features), <span class="dv">4</span>, <span class="at">pad =</span> <span class="st">"0"</span>))</span>
<span id="cb18-90"><a href="#cb18-90" aria-hidden="true" tabindex="-1"></a> <span class="fu">colnames</span>(features_matrix) <span class="ot">&lt;-</span> nomes_feat</span>
<span id="cb18-91"><a href="#cb18-91" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-92"><a href="#cb18-92" aria-hidden="true" tabindex="-1"></a> dados <span class="ot">&lt;-</span> <span class="fu">bind_cols</span>(</span>
<span id="cb18-93"><a href="#cb18-93" aria-hidden="true" tabindex="-1"></a> metadados[imagens_ok, ],</span>
<span id="cb18-94"><a href="#cb18-94" aria-hidden="true" tabindex="-1"></a> <span class="fu">as_tibble</span>(features_matrix[imagens_ok, ])</span>
<span id="cb18-95"><a href="#cb18-95" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb18-96"><a href="#cb18-96" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-97"><a href="#cb18-97" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(erros_idx) <span class="sc">&gt;</span> <span class="dv">0</span>) {</span>
<span id="cb18-98"><a href="#cb18-98" aria-hidden="true" tabindex="-1"></a> erros_df <span class="ot">&lt;-</span> metadados[erros_idx, ] <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="at">erro =</span> <span class="st">"falha na extracao CNN"</span>)</span>
<span id="cb18-99"><a href="#cb18-99" aria-hidden="true" tabindex="-1"></a> arquivo_erros <span class="ot">&lt;-</span> <span class="fu">file.path</span>(pasta_saida, <span class="st">"erros_processamento_rgb.csv"</span>)</span>
<span id="cb18-100"><a href="#cb18-100" aria-hidden="true" tabindex="-1"></a> <span class="fu">write_csv</span>(erros_df, arquivo_erros)</span>
<span id="cb18-101"><a href="#cb18-101" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"Imagens com erro:"</span>, <span class="fu">length</span>(erros_idx), <span class="st">"(ver"</span>, arquivo_erros, <span class="st">")</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb18-102"><a href="#cb18-102" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb18-103"><a href="#cb18-103" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-104"><a href="#cb18-104" aria-hidden="true" tabindex="-1"></a> <span class="fu">saveRDS</span>(dados, arquivo_cache_features)</span>
<span id="cb18-105"><a href="#cb18-105" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">file.exists</span>(arquivo_cache_parcial)) <span class="fu">file.remove</span>(arquivo_cache_parcial)</span>
<span id="cb18-106"><a href="#cb18-106" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-107"><a href="#cb18-107" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"Features salvas em:"</span>, arquivo_cache_features, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb18-108"><a href="#cb18-108" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Features carregadas do cache: outputs/parcial/features_rgb_mobilenetv2.rds </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Imagens na base final:"</span>, <span class="fu">nrow</span>(dados), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Imagens na base final: 650 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Numero de atributos extraidos (MobileNetV2):"</span>, <span class="fu">ncol</span>(dados) <span class="sc">-</span> <span class="dv">3</span>L, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Numero de atributos extraidos (MobileNetV2): 1280 </code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>dados <span class="sc">%&gt;%</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(classe, <span class="at">name =</span> <span class="st">"n_imagens_processadas"</span>) <span class="sc">%&gt;%</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(classe) <span class="sc">%&gt;%</span></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">classe</th>
<th style="text-align: right;">n_imagens_processadas</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">C1</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C2</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C3</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C4</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C5</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C6</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C7</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C8</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C9</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C10</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C11</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="even">
<td style="text-align: left;">C12</td>
<td style="text-align: right;">50</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C13</td>
<td style="text-align: right;">50</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>dados <span class="sc">%&gt;%</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(arquivo, classe, feat_0001, feat_0002, feat_0003, feat_0004, feat_0005) <span class="sc">%&gt;%</span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">10</span>) <span class="sc">%&gt;%</span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">starts_with</span>(<span class="st">"feat_"</span>), <span class="sc">~</span> <span class="fu">round</span>(.x, <span class="dv">4</span>))) <span class="sc">%&gt;%</span></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 58%">
<col style="width: 5%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
<col style="width: 7%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">arquivo</th>
<th style="text-align: left;">classe</th>
<th style="text-align: right;">feat_0001</th>
<th style="text-align: right;">feat_0002</th>
<th style="text-align: right;">feat_0003</th>
<th style="text-align: right;">feat_0004</th>
<th style="text-align: right;">feat_0005</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2023_09_T_03_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.4923</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0283</td>
<td style="text-align: right;">0.8341</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2024_03_T_03_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.1868</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2023_12_M_03_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.1640</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2023_07_T_02_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.0364</td>
<td style="text-align: right;">0.0782</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.2644</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2024_02_M_03_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0045</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0260</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2023_11_M_02_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0472</td>
<td style="text-align: right;">0.0027</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2023_12_M_02_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.2422</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0045</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.1251</td>
</tr>
<tr class="even">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2023_11_M_03_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.0865</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0076</td>
<td style="text-align: right;">1.1901</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2024_05_T_01_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.6739</td>
<td style="text-align: right;">0.0044</td>
<td style="text-align: right;">0.0148</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0000</td>
</tr>
<tr class="even">
<td style="text-align: left;">D:/Vitor/Documents/CEAO/802/HAB/DADOS CEAO 2026/RGB/RGB/C1_2023_09_M_01_RGB.tiff</td>
<td style="text-align: left;">C1</td>
<td style="text-align: right;">0.3725</td>
<td style="text-align: right;">0.1405</td>
<td style="text-align: right;">0.0000</td>
<td style="text-align: right;">0.0020</td>
<td style="text-align: right;">0.0000</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="separacao-entre-treino-e-teste" class="level1" data-number="8">
<h1 data-number="8"><span class="header-section-number">8</span> Separacao entre treino e teste</h1>
<p>A separacao treino/teste sera feita de forma estratificada, mantendo a proporcao das classes sempre que possivel.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 8. Separacao treino/teste</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a>idx_treino <span class="ot">&lt;-</span> <span class="fu">createDataPartition</span>(</span>
<span id="cb26-8"><a href="#cb26-8" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> dados<span class="sc">$</span>classe,</span>
<span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a> <span class="at">p =</span> <span class="fl">0.70</span>,</span>
<span id="cb26-10"><a href="#cb26-10" aria-hidden="true" tabindex="-1"></a> <span class="at">list =</span> <span class="cn">FALSE</span></span>
<span id="cb26-11"><a href="#cb26-11" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb26-12"><a href="#cb26-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-13"><a href="#cb26-13" aria-hidden="true" tabindex="-1"></a>treino <span class="ot">&lt;-</span> dados[idx_treino, ]</span>
<span id="cb26-14"><a href="#cb26-14" aria-hidden="true" tabindex="-1"></a>teste <span class="ot">&lt;-</span> dados[<span class="sc">-</span>idx_treino, ]</span>
<span id="cb26-15"><a href="#cb26-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-16"><a href="#cb26-16" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Imagens no treino:"</span>, <span class="fu">nrow</span>(treino), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Imagens no treino: 455 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Imagens no teste:"</span>, <span class="fu">nrow</span>(teste), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Imagens no teste: 195 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>treino <span class="sc">%&gt;%</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(classe, <span class="at">name =</span> <span class="st">"treino"</span>) <span class="sc">%&gt;%</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">full_join</span>(</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> teste <span class="sc">%&gt;%</span> <span class="fu">count</span>(classe, <span class="at">name =</span> <span class="st">"teste"</span>),</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="st">"classe"</span></span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%&gt;%</span></span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(classe) <span class="sc">%&gt;%</span></span>
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">classe</th>
<th style="text-align: right;">treino</th>
<th style="text-align: right;">teste</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">C1</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">C2</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C3</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">C4</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C5</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">C6</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C7</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">C8</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C9</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">C10</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C11</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="even">
<td style="text-align: left;">C12</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
<tr class="odd">
<td style="text-align: left;">C13</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">15</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="pre-processamento" class="level1" data-number="9">
<h1 data-number="9"><span class="header-section-number">9</span> Pre-processamento</h1>
<p>Como o SVM e sensivel a escala das variaveis, os atributos serao centralizados e padronizados. Tambem serao removidas variaveis com variancia quase nula.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 9. Preparar matrizes X e vetor y</span></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a>colunas_nao_preditoras <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="st">"arquivo"</span>, <span class="st">"classe"</span>, <span class="st">"tamanho_mb"</span>)</span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a>x_treino <span class="ot">&lt;-</span> treino <span class="sc">%&gt;%</span></span>
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="sc">-</span><span class="fu">all_of</span>(colunas_nao_preditoras))</span>
<span id="cb31-9"><a href="#cb31-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-10"><a href="#cb31-10" aria-hidden="true" tabindex="-1"></a>x_teste <span class="ot">&lt;-</span> teste <span class="sc">%&gt;%</span></span>
<span id="cb31-11"><a href="#cb31-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="sc">-</span><span class="fu">all_of</span>(colunas_nao_preditoras))</span>
<span id="cb31-12"><a href="#cb31-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-13"><a href="#cb31-13" aria-hidden="true" tabindex="-1"></a>y_treino <span class="ot">&lt;-</span> <span class="fu">droplevels</span>(treino<span class="sc">$</span>classe)</span>
<span id="cb31-14"><a href="#cb31-14" aria-hidden="true" tabindex="-1"></a>y_teste <span class="ot">&lt;-</span> <span class="fu">factor</span>(teste<span class="sc">$</span>classe, <span class="at">levels =</span> <span class="fu">levels</span>(y_treino))</span>
<span id="cb31-15"><a href="#cb31-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-16"><a href="#cb31-16" aria-hidden="true" tabindex="-1"></a>variaveis_nzv <span class="ot">&lt;-</span> <span class="fu">nearZeroVar</span>(x_treino)</span>
<span id="cb31-17"><a href="#cb31-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-18"><a href="#cb31-18" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">length</span>(variaveis_nzv) <span class="sc">&gt;</span> <span class="dv">0</span>) {</span>
<span id="cb31-19"><a href="#cb31-19" aria-hidden="true" tabindex="-1"></a> x_treino <span class="ot">&lt;-</span> x_treino[, <span class="sc">-</span>variaveis_nzv, drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
<span id="cb31-20"><a href="#cb31-20" aria-hidden="true" tabindex="-1"></a> x_teste <span class="ot">&lt;-</span> x_teste[, <span class="fu">colnames</span>(x_treino), drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
<span id="cb31-21"><a href="#cb31-21" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb31-22"><a href="#cb31-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-23"><a href="#cb31-23" aria-hidden="true" tabindex="-1"></a>preproc <span class="ot">&lt;-</span> <span class="fu">preProcess</span>(</span>
<span id="cb31-24"><a href="#cb31-24" aria-hidden="true" tabindex="-1"></a> x_treino,</span>
<span id="cb31-25"><a href="#cb31-25" aria-hidden="true" tabindex="-1"></a> <span class="at">method =</span> <span class="fu">c</span>(<span class="st">"center"</span>, <span class="st">"scale"</span>)</span>
<span id="cb31-26"><a href="#cb31-26" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb31-27"><a href="#cb31-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-28"><a href="#cb31-28" aria-hidden="true" tabindex="-1"></a>x_treino_norm <span class="ot">&lt;-</span> <span class="fu">predict</span>(preproc, x_treino)</span>
<span id="cb31-29"><a href="#cb31-29" aria-hidden="true" tabindex="-1"></a>x_teste_norm <span class="ot">&lt;-</span> <span class="fu">predict</span>(preproc, x_teste)</span>
<span id="cb31-30"><a href="#cb31-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-31"><a href="#cb31-31" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Preditores usados nos modelos:"</span>, <span class="fu">ncol</span>(x_treino_norm), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Preditores usados nos modelos: 1265 </code></pre>
</div>
</div>
</section>
<section id="modelo-1-svm-linear" class="level1" data-number="10">
<h1 data-number="10"><span class="header-section-number">10</span> Modelo 1: SVM linear</h1>
<p>O SVM linear tenta separar as classes por hiperplanos lineares. E o modelo mais simples e serve como referencia inicial.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 10. SVM linear</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb33-6"><a href="#cb33-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-7"><a href="#cb33-7" aria-hidden="true" tabindex="-1"></a>modelo_svm_linear <span class="ot">&lt;-</span> <span class="fu">svm</span>(</span>
<span id="cb33-8"><a href="#cb33-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_norm,</span>
<span id="cb33-9"><a href="#cb33-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
<span id="cb33-10"><a href="#cb33-10" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"linear"</span>,</span>
<span id="cb33-11"><a href="#cb33-11" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> <span class="dv">1</span>,</span>
<span id="cb33-12"><a href="#cb33-12" aria-hidden="true" tabindex="-1"></a> <span class="at">scale =</span> <span class="cn">FALSE</span></span>
<span id="cb33-13"><a href="#cb33-13" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb33-14"><a href="#cb33-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-15"><a href="#cb33-15" aria-hidden="true" tabindex="-1"></a>pred_svm_linear <span class="ot">&lt;-</span> <span class="fu">predict</span>(modelo_svm_linear, x_teste_norm)</span>
<span id="cb33-16"><a href="#cb33-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-17"><a href="#cb33-17" aria-hidden="true" tabindex="-1"></a>cm_svm_linear <span class="ot">&lt;-</span> <span class="fu">confusionMatrix</span>(pred_svm_linear, y_teste)</span>
<span id="cb33-18"><a href="#cb33-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-19"><a href="#cb33-19" aria-hidden="true" tabindex="-1"></a>cm_svm_linear</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Confusion Matrix and Statistics
Reference
Prediction C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
C1 4 3 0 1 0 0 0 0 1 1 0 0 0
C2 4 6 0 0 0 2 0 0 0 0 0 0 1
C3 0 0 7 6 1 0 0 2 1 0 0 0 0
C4 3 2 0 6 0 0 2 1 0 0 0 1 4
C5 0 0 2 0 9 0 0 0 0 0 0 0 1
C6 2 3 0 0 1 8 1 4 1 3 2 3 0
C7 0 1 0 0 1 0 6 0 2 0 0 6 2
C8 0 0 1 0 0 1 1 8 0 0 1 1 0
C9 1 0 1 0 0 0 0 0 9 1 0 0 1
C10 0 0 1 0 0 0 0 0 1 5 8 0 0
C11 1 0 0 1 0 1 0 0 0 5 4 1 0
C12 0 0 0 0 1 3 5 0 0 0 0 2 2
C13 0 0 3 1 2 0 0 0 0 0 0 1 4
Overall Statistics
Accuracy : 0.4
95% CI : (0.3307, 0.4724)
No Information Rate : 0.0769
P-Value [Acc &gt; NIR] : &lt; 2.2e-16
Kappa : 0.35
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
Sensitivity 0.26667 0.40000 0.46667 0.40000 0.60000
Specificity 0.96667 0.96111 0.94444 0.92778 0.98333
Pos Pred Value 0.40000 0.46154 0.41176 0.31579 0.75000
Neg Pred Value 0.94054 0.95055 0.95506 0.94886 0.96721
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.02051 0.03077 0.03590 0.03077 0.04615
Detection Prevalence 0.05128 0.06667 0.08718 0.09744 0.06154
Balanced Accuracy 0.61667 0.68056 0.70556 0.66389 0.79167
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
Sensitivity 0.53333 0.40000 0.53333 0.60000 0.33333
Specificity 0.88889 0.93333 0.97222 0.97778 0.94444
Pos Pred Value 0.28571 0.33333 0.61538 0.69231 0.33333
Neg Pred Value 0.95808 0.94915 0.96154 0.96703 0.94444
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.04103 0.03077 0.04103 0.04615 0.02564
Detection Prevalence 0.14359 0.09231 0.06667 0.06667 0.07692
Balanced Accuracy 0.71111 0.66667 0.75278 0.78889 0.63889
Class: C11 Class: C12 Class: C13
Sensitivity 0.26667 0.13333 0.26667
Specificity 0.95000 0.93889 0.96111
Pos Pred Value 0.30769 0.15385 0.36364
Neg Pred Value 0.93956 0.92857 0.94022
Prevalence 0.07692 0.07692 0.07692
Detection Rate 0.02051 0.01026 0.02051
Detection Prevalence 0.06667 0.06667 0.05641
Balanced Accuracy 0.60833 0.53611 0.61389</code></pre>
</div>
</div>
</section>
<section id="modelo-2-svm-radial" class="level1" data-number="11">
<h1 data-number="11"><span class="header-section-number">11</span> Modelo 2: SVM radial</h1>
<p>O kernel radial, tambem chamado de RBF, permite construir fronteiras de decisao nao lineares. Nesta primeira versao, serao usados valores fixos para <code>cost</code> e <code>gamma</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 11. SVM radial com parametros fixos</span></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-7"><a href="#cb35-7" aria-hidden="true" tabindex="-1"></a>modelo_svm_radial <span class="ot">&lt;-</span> <span class="fu">svm</span>(</span>
<span id="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_norm,</span>
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
<span id="cb35-10"><a href="#cb35-10" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>,</span>
<span id="cb35-11"><a href="#cb35-11" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> <span class="dv">10</span>,</span>
<span id="cb35-12"><a href="#cb35-12" aria-hidden="true" tabindex="-1"></a> <span class="at">gamma =</span> <span class="fl">0.01</span>,</span>
<span id="cb35-13"><a href="#cb35-13" aria-hidden="true" tabindex="-1"></a> <span class="at">scale =</span> <span class="cn">FALSE</span></span>
<span id="cb35-14"><a href="#cb35-14" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb35-15"><a href="#cb35-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-16"><a href="#cb35-16" aria-hidden="true" tabindex="-1"></a>pred_svm_radial <span class="ot">&lt;-</span> <span class="fu">predict</span>(modelo_svm_radial, x_teste_norm)</span>
<span id="cb35-17"><a href="#cb35-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-18"><a href="#cb35-18" aria-hidden="true" tabindex="-1"></a>cm_svm_radial <span class="ot">&lt;-</span> <span class="fu">confusionMatrix</span>(pred_svm_radial, y_teste)</span>
<span id="cb35-19"><a href="#cb35-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb35-20"><a href="#cb35-20" aria-hidden="true" tabindex="-1"></a>cm_svm_radial</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Confusion Matrix and Statistics
Reference
Prediction C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
C1 2 1 0 0 0 0 0 0 0 0 0 0 0
C2 2 0 0 0 0 0 0 1 0 0 0 0 1
C3 0 0 3 2 0 0 0 1 1 0 0 0 0
C4 0 0 0 2 0 0 1 0 0 0 0 0 3
C5 0 0 1 0 4 0 0 0 0 0 0 0 2
C6 0 3 0 0 0 2 0 5 0 2 0 2 0
C7 0 0 0 0 0 0 0 0 1 0 0 3 1
C8 0 0 0 0 0 2 1 1 1 0 0 1 0
C9 1 0 1 0 0 0 0 0 2 0 0 0 0
C10 0 0 0 0 0 1 0 0 1 2 9 0 0
C11 1 0 0 0 0 0 0 1 0 3 0 0 0
C12 0 0 0 0 0 1 4 0 1 1 0 2 0
C13 9 11 10 11 11 9 9 6 8 7 6 7 8
Overall Statistics
Accuracy : 0.1436
95% CI : (0.0976, 0.2008)
No Information Rate : 0.0769
P-Value [Acc &gt; NIR] : 0.001068
Kappa : 0.0722
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
Sensitivity 0.13333 0.00000 0.20000 0.13333 0.26667
Specificity 0.99444 0.97778 0.97778 0.97778 0.98333
Pos Pred Value 0.66667 0.00000 0.42857 0.33333 0.57143
Neg Pred Value 0.93229 0.92147 0.93617 0.93122 0.94149
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.01026 0.00000 0.01538 0.01026 0.02051
Detection Prevalence 0.01538 0.02051 0.03590 0.03077 0.03590
Balanced Accuracy 0.56389 0.48889 0.58889 0.55556 0.62500
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
Sensitivity 0.13333 0.00000 0.066667 0.13333 0.13333
Specificity 0.93333 0.97222 0.972222 0.98889 0.93889
Pos Pred Value 0.14286 0.00000 0.166667 0.50000 0.15385
Neg Pred Value 0.92818 0.92105 0.925926 0.93194 0.92857
Prevalence 0.07692 0.07692 0.076923 0.07692 0.07692
Detection Rate 0.01026 0.00000 0.005128 0.01026 0.01026
Detection Prevalence 0.07179 0.02564 0.030769 0.02051 0.06667
Balanced Accuracy 0.53333 0.48611 0.519444 0.56111 0.53611
Class: C11 Class: C12 Class: C13
Sensitivity 0.00000 0.13333 0.53333
Specificity 0.97222 0.96111 0.42222
Pos Pred Value 0.00000 0.22222 0.07143
Neg Pred Value 0.92105 0.93011 0.91566
Prevalence 0.07692 0.07692 0.07692
Detection Rate 0.00000 0.01026 0.04103
Detection Prevalence 0.02564 0.04615 0.57436
Balanced Accuracy 0.48611 0.54722 0.47778</code></pre>
</div>
</div>
</section>
<section id="modelo-3-ajuste-de-hiperparametros-do-svm-radial" class="level1" data-number="12">
<h1 data-number="12"><span class="header-section-number">12</span> Modelo 3: Ajuste de hiperparametros do SVM radial</h1>
<p>O parametro <code>cost</code> controla o quanto o modelo penaliza erros de classificacao. O parametro <code>gamma</code> influencia o alcance do kernel radial. Valores maiores podem gerar fronteiras mais complexas.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb37-2"><a href="#cb37-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 12. Ajuste simples de hiperparametros do SVM radial</span></span>
<span id="cb37-3"><a href="#cb37-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb37-4"><a href="#cb37-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-5"><a href="#cb37-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb37-6"><a href="#cb37-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-7"><a href="#cb37-7" aria-hidden="true" tabindex="-1"></a>k_cv <span class="ot">&lt;-</span> <span class="fu">min</span>(<span class="dv">5</span>, <span class="fu">as.integer</span>(<span class="fu">min</span>(<span class="fu">table</span>(y_treino))))</span>
<span id="cb37-8"><a href="#cb37-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb37-9"><a href="#cb37-9" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (k_cv <span class="sc">&gt;=</span> <span class="dv">2</span>) {</span>
<span id="cb37-10"><a href="#cb37-10" aria-hidden="true" tabindex="-1"></a> ajuste_svm_radial <span class="ot">&lt;-</span> <span class="fu">tune.svm</span>(</span>
<span id="cb37-11"><a href="#cb37-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_norm,</span>
<span id="cb37-12"><a href="#cb37-12" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
<span id="cb37-13"><a href="#cb37-13" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>,</span>
<span id="cb37-14"><a href="#cb37-14" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> <span class="fu">c</span>(<span class="fl">0.1</span>, <span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">100</span>),</span>
<span id="cb37-15"><a href="#cb37-15" aria-hidden="true" tabindex="-1"></a> <span class="at">gamma =</span> <span class="fu">c</span>(<span class="fl">0.001</span>, <span class="fl">0.01</span>, <span class="fl">0.05</span>, <span class="fl">0.1</span>),</span>
<span id="cb37-16"><a href="#cb37-16" aria-hidden="true" tabindex="-1"></a> <span class="at">tunecontrol =</span> <span class="fu">tune.control</span>(<span class="at">cross =</span> k_cv)</span>
<span id="cb37-17"><a href="#cb37-17" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb37-18"><a href="#cb37-18" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb37-19"><a href="#cb37-19" aria-hidden="true" tabindex="-1"></a> modelo_svm_radial_ajustado <span class="ot">&lt;-</span> ajuste_svm_radial<span class="sc">$</span>best.model</span>
<span id="cb37-20"><a href="#cb37-20" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb37-21"><a href="#cb37-21" aria-hidden="true" tabindex="-1"></a> pred_svm_radial_ajustado <span class="ot">&lt;-</span> <span class="fu">predict</span>(</span>
<span id="cb37-22"><a href="#cb37-22" aria-hidden="true" tabindex="-1"></a> modelo_svm_radial_ajustado,</span>
<span id="cb37-23"><a href="#cb37-23" aria-hidden="true" tabindex="-1"></a> x_teste_norm</span>
<span id="cb37-24"><a href="#cb37-24" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb37-25"><a href="#cb37-25" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb37-26"><a href="#cb37-26" aria-hidden="true" tabindex="-1"></a> cm_svm_radial_ajustado <span class="ot">&lt;-</span> <span class="fu">confusionMatrix</span>(</span>
<span id="cb37-27"><a href="#cb37-27" aria-hidden="true" tabindex="-1"></a> pred_svm_radial_ajustado,</span>
<span id="cb37-28"><a href="#cb37-28" aria-hidden="true" tabindex="-1"></a> y_teste</span>
<span id="cb37-29"><a href="#cb37-29" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb37-30"><a href="#cb37-30" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb37-31"><a href="#cb37-31" aria-hidden="true" tabindex="-1"></a> ajuste_svm_radial<span class="sc">$</span>best.parameters <span class="sc">%&gt;%</span></span>
<span id="cb37-32"><a href="#cb37-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span>
<span id="cb37-33"><a href="#cb37-33" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb37-34"><a href="#cb37-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">warning</span>(<span class="st">"Poucas amostras por classe para validacao cruzada. O ajuste automatico sera ignorado."</span>)</span>
<span id="cb37-35"><a href="#cb37-35" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb37-36"><a href="#cb37-36" aria-hidden="true" tabindex="-1"></a> ajuste_svm_radial <span class="ot">&lt;-</span> <span class="cn">NULL</span></span>
<span id="cb37-37"><a href="#cb37-37" aria-hidden="true" tabindex="-1"></a> modelo_svm_radial_ajustado <span class="ot">&lt;-</span> modelo_svm_radial</span>
<span id="cb37-38"><a href="#cb37-38" aria-hidden="true" tabindex="-1"></a> pred_svm_radial_ajustado <span class="ot">&lt;-</span> pred_svm_radial</span>
<span id="cb37-39"><a href="#cb37-39" aria-hidden="true" tabindex="-1"></a> cm_svm_radial_ajustado <span class="ot">&lt;-</span> cm_svm_radial</span>
<span id="cb37-40"><a href="#cb37-40" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;"></th>
<th style="text-align: right;">gamma</th>
<th style="text-align: right;">cost</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">5</td>
<td style="text-align: right;">0.001</td>
<td style="text-align: right;">1</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>cm_svm_radial_ajustado</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Confusion Matrix and Statistics
Reference
Prediction C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
C1 4 3 0 1 0 0 0 0 0 1 0 0 0
C2 3 3 0 0 0 0 0 0 0 0 0 0 1
C3 0 0 2 2 0 0 0 0 1 0 0 0 0
C4 4 1 0 6 0 0 2 1 0 0 0 0 4
C5 0 0 3 0 9 0 0 0 0 0 0 0 1
C6 0 6 0 0 1 8 1 7 1 2 1 4 0
C7 0 1 0 1 0 2 6 0 0 0 0 6 2
C8 0 0 2 0 0 0 0 6 2 0 0 0 0
C9 2 0 1 0 2 1 0 0 8 1 0 0 2
C10 0 0 1 0 0 1 0 0 1 3 7 0 0
C11 1 0 1 1 1 2 0 1 1 7 6 0 0
C12 0 0 0 0 0 1 5 0 0 0 0 4 1
C13 1 1 5 4 2 0 1 0 1 1 1 1 4
Overall Statistics
Accuracy : 0.3538
95% CI : (0.2869, 0.4254)
No Information Rate : 0.0769
P-Value [Acc &gt; NIR] : &lt; 2.2e-16
Kappa : 0.3
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
Sensitivity 0.26667 0.20000 0.13333 0.40000 0.60000
Specificity 0.97222 0.97778 0.98333 0.93333 0.97778
Pos Pred Value 0.44444 0.42857 0.40000 0.33333 0.69231
Neg Pred Value 0.94086 0.93617 0.93158 0.94915 0.96703
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.02051 0.01538 0.01026 0.03077 0.04615
Detection Prevalence 0.04615 0.03590 0.02564 0.09231 0.06667
Balanced Accuracy 0.61944 0.58889 0.55833 0.66667 0.78889
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
Sensitivity 0.53333 0.40000 0.40000 0.53333 0.20000
Specificity 0.87222 0.93333 0.97778 0.95000 0.94444
Pos Pred Value 0.25806 0.33333 0.60000 0.47059 0.23077
Neg Pred Value 0.95732 0.94915 0.95135 0.96067 0.93407
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.04103 0.03077 0.03077 0.04103 0.01538
Detection Prevalence 0.15897 0.09231 0.05128 0.08718 0.06667
Balanced Accuracy 0.70278 0.66667 0.68889 0.74167 0.57222
Class: C11 Class: C12 Class: C13
Sensitivity 0.40000 0.26667 0.26667
Specificity 0.91667 0.96111 0.90000
Pos Pred Value 0.28571 0.36364 0.18182
Neg Pred Value 0.94828 0.94022 0.93642
Prevalence 0.07692 0.07692 0.07692
Detection Rate 0.03077 0.02051 0.02051
Detection Prevalence 0.10769 0.05641 0.11282
Balanced Accuracy 0.65833 0.61389 0.58333</code></pre>
</div>
</div>
</section>
<section id="modelo-4-pca-svm-radial" class="level1" data-number="13">
<h1 data-number="13"><span class="header-section-number">13</span> Modelo 4: PCA + SVM radial</h1>
<p>O PCA transforma os atributos originais em componentes principais. Aqui, serao mantidos componentes suficientes para explicar pelo menos 95% da variancia, com limite maximo de 30 componentes.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 13. PCA</span></span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb40-4"><a href="#cb40-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-5"><a href="#cb40-5" aria-hidden="true" tabindex="-1"></a>pca <span class="ot">&lt;-</span> <span class="fu">prcomp</span>(</span>
<span id="cb40-6"><a href="#cb40-6" aria-hidden="true" tabindex="-1"></a> x_treino_norm,</span>
<span id="cb40-7"><a href="#cb40-7" aria-hidden="true" tabindex="-1"></a> <span class="at">center =</span> <span class="cn">FALSE</span>,</span>
<span id="cb40-8"><a href="#cb40-8" aria-hidden="true" tabindex="-1"></a> <span class="at">scale. =</span> <span class="cn">FALSE</span></span>
<span id="cb40-9"><a href="#cb40-9" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb40-10"><a href="#cb40-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-11"><a href="#cb40-11" aria-hidden="true" tabindex="-1"></a>variancia <span class="ot">&lt;-</span> pca<span class="sc">$</span>sdev<span class="sc">^</span><span class="dv">2</span></span>
<span id="cb40-12"><a href="#cb40-12" aria-hidden="true" tabindex="-1"></a>variancia_exp <span class="ot">&lt;-</span> variancia <span class="sc">/</span> <span class="fu">sum</span>(variancia)</span>
<span id="cb40-13"><a href="#cb40-13" aria-hidden="true" tabindex="-1"></a>variancia_acum <span class="ot">&lt;-</span> <span class="fu">cumsum</span>(variancia_exp)</span>
<span id="cb40-14"><a href="#cb40-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-15"><a href="#cb40-15" aria-hidden="true" tabindex="-1"></a>n_comp_95 <span class="ot">&lt;-</span> <span class="fu">which</span>(variancia_acum <span class="sc">&gt;=</span> <span class="fl">0.95</span>)[<span class="dv">1</span>]</span>
<span id="cb40-16"><a href="#cb40-16" aria-hidden="true" tabindex="-1"></a>n_comp <span class="ot">&lt;-</span> <span class="fu">min</span>(n_comp_95, <span class="dv">30</span>, <span class="fu">ncol</span>(x_treino_norm))</span>
<span id="cb40-17"><a href="#cb40-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-18"><a href="#cb40-18" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Componentes necessarios para 95% da variancia:"</span>, n_comp_95, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Componentes necessarios para 95% da variancia: 249 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Componentes usados no modelo:"</span>, n_comp, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Componentes usados no modelo: 30 </code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tibble</span>(</span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a> <span class="at">componente =</span> <span class="fu">seq_along</span>(variancia_acum),</span>
<span id="cb44-3"><a href="#cb44-3" aria-hidden="true" tabindex="-1"></a> <span class="at">variancia_acumulada =</span> variancia_acum</span>
<span id="cb44-4"><a href="#cb44-4" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%&gt;%</span></span>
<span id="cb44-5"><a href="#cb44-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> componente, <span class="at">y =</span> variancia_acumulada)) <span class="sc">+</span></span>
<span id="cb44-6"><a href="#cb44-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">color =</span> <span class="st">"#2C7FB8"</span>) <span class="sc">+</span></span>
<span id="cb44-7"><a href="#cb44-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">color =</span> <span class="st">"#2C7FB8"</span>) <span class="sc">+</span></span>
<span id="cb44-8"><a href="#cb44-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_hline</span>(<span class="at">yintercept =</span> <span class="fl">0.95</span>, <span class="at">linetype =</span> <span class="st">"dashed"</span>) <span class="sc">+</span></span>
<span id="cb44-9"><a href="#cb44-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb44-10"><a href="#cb44-10" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Variancia acumulada pelo PCA"</span>,</span>
<span id="cb44-11"><a href="#cb44-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Numero de componentes principais"</span>,</span>
<span id="cb44-12"><a href="#cb44-12" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Variancia acumulada"</span></span>
<span id="cb44-13"><a href="#cb44-13" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb44-14"><a href="#cb44-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>x_treino_pca <span class="ot">&lt;-</span> <span class="fu">as.data.frame</span>(pca<span class="sc">$</span>x[, <span class="dv">1</span><span class="sc">:</span>n_comp, <span class="at">drop =</span> <span class="cn">FALSE</span>])</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a>x_teste_pca <span class="ot">&lt;-</span> <span class="fu">as.data.frame</span>(</span>
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">predict</span>(pca, <span class="at">newdata =</span> x_teste_norm)[, <span class="dv">1</span><span class="sc">:</span>n_comp, <span class="at">drop =</span> <span class="cn">FALSE</span>]</span>
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb45-6"><a href="#cb45-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-7"><a href="#cb45-7" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb45-8"><a href="#cb45-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-9"><a href="#cb45-9" aria-hidden="true" tabindex="-1"></a>modelo_svm_pca <span class="ot">&lt;-</span> <span class="fu">svm</span>(</span>
<span id="cb45-10"><a href="#cb45-10" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_pca,</span>
<span id="cb45-11"><a href="#cb45-11" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
<span id="cb45-12"><a href="#cb45-12" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>,</span>
<span id="cb45-13"><a href="#cb45-13" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> <span class="dv">10</span>,</span>
<span id="cb45-14"><a href="#cb45-14" aria-hidden="true" tabindex="-1"></a> <span class="at">gamma =</span> <span class="fl">0.01</span>,</span>
<span id="cb45-15"><a href="#cb45-15" aria-hidden="true" tabindex="-1"></a> <span class="at">scale =</span> <span class="cn">FALSE</span></span>
<span id="cb45-16"><a href="#cb45-16" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb45-17"><a href="#cb45-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-18"><a href="#cb45-18" aria-hidden="true" tabindex="-1"></a>pred_svm_pca <span class="ot">&lt;-</span> <span class="fu">predict</span>(modelo_svm_pca, x_teste_pca)</span>
<span id="cb45-19"><a href="#cb45-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-20"><a href="#cb45-20" aria-hidden="true" tabindex="-1"></a>cm_svm_pca <span class="ot">&lt;-</span> <span class="fu">confusionMatrix</span>(pred_svm_pca, y_teste)</span>
<span id="cb45-21"><a href="#cb45-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb45-22"><a href="#cb45-22" aria-hidden="true" tabindex="-1"></a>cm_svm_pca</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Confusion Matrix and Statistics
Reference
Prediction C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
C1 6 2 0 0 1 0 0 0 0 1 0 0 0
C2 3 5 0 0 0 0 0 0 0 1 1 0 1
C3 0 0 7 2 1 0 0 0 1 0 0 0 0
C4 1 0 0 6 0 0 1 1 0 0 0 0 4
C5 0 0 2 0 8 0 0 0 0 0 0 0 1
C6 1 3 0 0 0 3 1 6 0 3 2 3 0
C7 0 0 0 1 1 3 7 0 1 0 0 6 3
C8 0 2 1 0 0 4 0 7 2 0 0 0 0
C9 1 0 1 0 2 1 0 0 6 1 0 0 2
C10 0 0 1 0 0 1 0 0 0 5 9 0 0
C11 1 0 0 0 1 1 0 1 0 3 2 0 0
C12 0 0 0 1 0 2 5 0 2 1 1 4 0
C13 2 3 3 5 1 0 1 0 3 0 0 2 4
Overall Statistics
Accuracy : 0.359
95% CI : (0.2917, 0.4306)
No Information Rate : 0.0769
P-Value [Acc &gt; NIR] : &lt; 2.2e-16
Kappa : 0.3056
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
Sensitivity 0.40000 0.33333 0.46667 0.40000 0.53333
Specificity 0.97778 0.96667 0.97778 0.96111 0.98333
Pos Pred Value 0.60000 0.45455 0.63636 0.46154 0.72727
Neg Pred Value 0.95135 0.94565 0.95652 0.95055 0.96196
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.03077 0.02564 0.03590 0.03077 0.04103
Detection Prevalence 0.05128 0.05641 0.05641 0.06667 0.05641
Balanced Accuracy 0.68889 0.65000 0.72222 0.68056 0.75833
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
Sensitivity 0.20000 0.46667 0.46667 0.40000 0.33333
Specificity 0.89444 0.91667 0.95000 0.95556 0.93889
Pos Pred Value 0.13636 0.31818 0.43750 0.42857 0.31250
Neg Pred Value 0.93064 0.95376 0.95531 0.95028 0.94413
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.01538 0.03590 0.03590 0.03077 0.02564
Detection Prevalence 0.11282 0.11282 0.08205 0.07179 0.08205
Balanced Accuracy 0.54722 0.69167 0.70833 0.67778 0.63611
Class: C11 Class: C12 Class: C13
Sensitivity 0.13333 0.26667 0.26667
Specificity 0.96111 0.93333 0.88889
Pos Pred Value 0.22222 0.25000 0.16667
Neg Pred Value 0.93011 0.93855 0.93567
Prevalence 0.07692 0.07692 0.07692
Detection Rate 0.01026 0.02051 0.02051
Detection Prevalence 0.04615 0.08205 0.12308
Balanced Accuracy 0.54722 0.60000 0.57778</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>scores_pca <span class="ot">&lt;-</span> <span class="fu">as_tibble</span>(pca<span class="sc">$</span>x[, <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">drop =</span> <span class="cn">FALSE</span>]) <span class="sc">%&gt;%</span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">classe =</span> y_treino)</span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(scores_pca, <span class="fu">aes</span>(<span class="at">x =</span> PC1, <span class="at">y =</span> PC2, <span class="at">color =</span> classe)) <span class="sc">+</span></span>
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">alpha =</span> <span class="fl">0.8</span>) <span class="sc">+</span></span>
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Visualizacao do conjunto de treino no espaco PCA"</span>,</span>
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"PC1"</span>,</span>
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"PC2"</span>,</span>
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"Classe"</span></span>
<span id="cb47-11"><a href="#cb47-11" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb47-12"><a href="#cb47-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="modelo-5-random-forest" class="level1" data-number="14">
<h1 data-number="14"><span class="header-section-number">14</span> Modelo 5: Random Forest</h1>
<p>O Random Forest sera usado como classificador de comparacao. Ele costuma lidar bem com relacoes nao lineares e tambem permite estimar a importancia das variaveis.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb48"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 14. Random Forest</span></span>
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb48-4"><a href="#cb48-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-5"><a href="#cb48-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb48-6"><a href="#cb48-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-7"><a href="#cb48-7" aria-hidden="true" tabindex="-1"></a>modelo_rf <span class="ot">&lt;-</span> <span class="fu">randomForest</span>(</span>
<span id="cb48-8"><a href="#cb48-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_norm,</span>
<span id="cb48-9"><a href="#cb48-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
<span id="cb48-10"><a href="#cb48-10" aria-hidden="true" tabindex="-1"></a> <span class="at">ntree =</span> <span class="dv">500</span>,</span>
<span id="cb48-11"><a href="#cb48-11" aria-hidden="true" tabindex="-1"></a> <span class="at">importance =</span> <span class="cn">TRUE</span></span>
<span id="cb48-12"><a href="#cb48-12" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb48-13"><a href="#cb48-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-14"><a href="#cb48-14" aria-hidden="true" tabindex="-1"></a>pred_rf <span class="ot">&lt;-</span> <span class="fu">predict</span>(modelo_rf, x_teste_norm)</span>
<span id="cb48-15"><a href="#cb48-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-16"><a href="#cb48-16" aria-hidden="true" tabindex="-1"></a>cm_rf <span class="ot">&lt;-</span> <span class="fu">confusionMatrix</span>(pred_rf, y_teste)</span>
<span id="cb48-17"><a href="#cb48-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-18"><a href="#cb48-18" aria-hidden="true" tabindex="-1"></a>cm_rf</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Confusion Matrix and Statistics
Reference
Prediction C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
C1 5 3 1 1 1 0 0 0 1 1 0 0 1
C2 1 4 0 0 0 0 0 0 0 0 0 0 1
C3 0 0 3 3 0 0 0 0 1 0 0 0 0
C4 5 3 1 9 0 0 3 1 0 0 0 0 4
C5 0 0 4 0 9 0 0 0 0 0 0 0 2
C6 1 5 0 0 3 8 1 6 1 3 2 4 0
C7 0 0 0 1 0 1 4 0 0 0 0 4 2
C8 0 0 3 0 0 2 2 6 2 0 1 0 0
C9 1 0 1 0 2 1 0 0 8 1 0 0 1
C10 1 0 0 0 0 0 0 1 0 6 10 2 0
C11 1 0 0 0 0 0 0 0 1 4 2 0 0
C12 0 0 0 0 0 3 5 1 1 0 0 5 0
C13 0 0 2 1 0 0 0 0 0 0 0 0 4
Overall Statistics
Accuracy : 0.3744
95% CI : (0.3063, 0.4463)
No Information Rate : 0.0769
P-Value [Acc &gt; NIR] : &lt; 2.2e-16
Kappa : 0.3222
Mcnemar's Test P-Value : NA
Statistics by Class:
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
Sensitivity 0.33333 0.26667 0.20000 0.60000 0.60000
Specificity 0.95000 0.98889 0.97778 0.90556 0.96667
Pos Pred Value 0.35714 0.66667 0.42857 0.34615 0.60000
Neg Pred Value 0.94475 0.94180 0.93617 0.96450 0.96667
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.02564 0.02051 0.01538 0.04615 0.04615
Detection Prevalence 0.07179 0.03077 0.03590 0.13333 0.07692
Balanced Accuracy 0.64167 0.62778 0.58889 0.75278 0.78333
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
Sensitivity 0.53333 0.26667 0.40000 0.53333 0.40000
Specificity 0.85556 0.95556 0.94444 0.96111 0.92222
Pos Pred Value 0.23529 0.33333 0.37500 0.53333 0.30000
Neg Pred Value 0.95652 0.93989 0.94972 0.96111 0.94857
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
Detection Rate 0.04103 0.02051 0.03077 0.04103 0.03077
Detection Prevalence 0.17436 0.06154 0.08205 0.07692 0.10256
Balanced Accuracy 0.69444 0.61111 0.67222 0.74722 0.66111
Class: C11 Class: C12 Class: C13
Sensitivity 0.13333 0.33333 0.26667
Specificity 0.96667 0.94444 0.98333
Pos Pred Value 0.25000 0.33333 0.57143
Neg Pred Value 0.93048 0.94444 0.94149
Prevalence 0.07692 0.07692 0.07692
Detection Rate 0.01026 0.02564 0.02051
Detection Prevalence 0.04103 0.07692 0.03590
Balanced Accuracy 0.55000 0.63889 0.62500</code></pre>
</div>
</div>
</section>
<section id="comparacao-dos-modelos" class="level1" data-number="15">
<h1 data-number="15"><span class="header-section-number">15</span> Comparacao dos modelos</h1>
<p>Os modelos serao comparados por acuracia, Kappa, sensibilidade macro, especificidade macro e F1 macro.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb50-2"><a href="#cb50-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 15. Funcao de avaliacao dos modelos</span></span>
<span id="cb50-3"><a href="#cb50-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb50-4"><a href="#cb50-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb50-5"><a href="#cb50-5" aria-hidden="true" tabindex="-1"></a>avaliar_modelo <span class="ot">&lt;-</span> <span class="cf">function</span>(nome, matriz_confusao) {</span>
<span id="cb50-6"><a href="#cb50-6" aria-hidden="true" tabindex="-1"></a> overall <span class="ot">&lt;-</span> matriz_confusao<span class="sc">$</span>overall</span>
<span id="cb50-7"><a href="#cb50-7" aria-hidden="true" tabindex="-1"></a> by_class <span class="ot">&lt;-</span> matriz_confusao<span class="sc">$</span>byClass</span>
<span id="cb50-8"><a href="#cb50-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb50-9"><a href="#cb50-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">is.matrix</span>(by_class)) {</span>
<span id="cb50-10"><a href="#cb50-10" aria-hidden="true" tabindex="-1"></a> sensibilidade_macro <span class="ot">&lt;-</span> <span class="fu">mean</span>(by_class[, <span class="st">"Sensitivity"</span>], <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb50-11"><a href="#cb50-11" aria-hidden="true" tabindex="-1"></a> especificidade_macro <span class="ot">&lt;-</span> <span class="fu">mean</span>(by_class[, <span class="st">"Specificity"</span>], <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb50-12"><a href="#cb50-12" aria-hidden="true" tabindex="-1"></a> f1_macro <span class="ot">&lt;-</span> <span class="fu">mean</span>(by_class[, <span class="st">"F1"</span>], <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb50-13"><a href="#cb50-13" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
<span id="cb50-14"><a href="#cb50-14" aria-hidden="true" tabindex="-1"></a> sensibilidade_macro <span class="ot">&lt;-</span> by_class[<span class="st">"Sensitivity"</span>]</span>
<span id="cb50-15"><a href="#cb50-15" aria-hidden="true" tabindex="-1"></a> especificidade_macro <span class="ot">&lt;-</span> by_class[<span class="st">"Specificity"</span>]</span>
<span id="cb50-16"><a href="#cb50-16" aria-hidden="true" tabindex="-1"></a> f1_macro <span class="ot">&lt;-</span> by_class[<span class="st">"F1"</span>]</span>
<span id="cb50-17"><a href="#cb50-17" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb50-18"><a href="#cb50-18" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb50-19"><a href="#cb50-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">tibble</span>(</span>
<span id="cb50-20"><a href="#cb50-20" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo =</span> nome,</span>
<span id="cb50-21"><a href="#cb50-21" aria-hidden="true" tabindex="-1"></a> <span class="at">acuracia =</span> <span class="fu">as.numeric</span>(overall[<span class="st">"Accuracy"</span>]),</span>
<span id="cb50-22"><a href="#cb50-22" aria-hidden="true" tabindex="-1"></a> <span class="at">kappa =</span> <span class="fu">as.numeric</span>(overall[<span class="st">"Kappa"</span>]),</span>
<span id="cb50-23"><a href="#cb50-23" aria-hidden="true" tabindex="-1"></a> <span class="at">sensibilidade_macro =</span> <span class="fu">as.numeric</span>(sensibilidade_macro),</span>
<span id="cb50-24"><a href="#cb50-24" aria-hidden="true" tabindex="-1"></a> <span class="at">especificidade_macro =</span> <span class="fu">as.numeric</span>(especificidade_macro),</span>
<span id="cb50-25"><a href="#cb50-25" aria-hidden="true" tabindex="-1"></a> <span class="at">f1_macro =</span> <span class="fu">as.numeric</span>(f1_macro)</span>
<span id="cb50-26"><a href="#cb50-26" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb50-27"><a href="#cb50-27" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb50-28"><a href="#cb50-28" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb50-29"><a href="#cb50-29" aria-hidden="true" tabindex="-1"></a>resultados <span class="ot">&lt;-</span> <span class="fu">bind_rows</span>(</span>
<span id="cb50-30"><a href="#cb50-30" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"SVM linear"</span>, cm_svm_linear),</span>
<span id="cb50-31"><a href="#cb50-31" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"SVM radial"</span>, cm_svm_radial),</span>
<span id="cb50-32"><a href="#cb50-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"SVM radial ajustado"</span>, cm_svm_radial_ajustado),</span>
<span id="cb50-33"><a href="#cb50-33" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"PCA + SVM radial"</span>, cm_svm_pca),</span>
<span id="cb50-34"><a href="#cb50-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"Random Forest"</span>, cm_rf)</span>
<span id="cb50-35"><a href="#cb50-35" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%&gt;%</span></span>
<span id="cb50-36"><a href="#cb50-36" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(acuracia))</span>
<span id="cb50-37"><a href="#cb50-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb50-38"><a href="#cb50-38" aria-hidden="true" tabindex="-1"></a>resultados <span class="sc">%&gt;%</span></span>
<span id="cb50-39"><a href="#cb50-39" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">where</span>(is.numeric), <span class="sc">~</span> <span class="fu">round</span>(.x, <span class="dv">4</span>))) <span class="sc">%&gt;%</span></span>
<span id="cb50-40"><a href="#cb50-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<colgroup>
<col style="width: 23%">
<col style="width: 10%">
<col style="width: 8%">
<col style="width: 23%">
<col style="width: 24%">
<col style="width: 10%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">modelo</th>
<th style="text-align: right;">acuracia</th>
<th style="text-align: right;">kappa</th>
<th style="text-align: right;">sensibilidade_macro</th>
<th style="text-align: right;">especificidade_macro</th>
<th style="text-align: right;">f1_macro</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">SVM linear</td>
<td style="text-align: right;">0.4000</td>
<td style="text-align: right;">0.3500</td>
<td style="text-align: right;">0.4000</td>
<td style="text-align: right;">0.9500</td>
<td style="text-align: right;">0.4019</td>
</tr>
<tr class="even">
<td style="text-align: left;">Random Forest</td>
<td style="text-align: right;">0.3744</td>
<td style="text-align: right;">0.3222</td>
<td style="text-align: right;">0.3744</td>
<td style="text-align: right;">0.9479</td>
<td style="text-align: right;">0.3688</td>
</tr>
<tr class="odd">
<td style="text-align: left;">PCA + SVM radial</td>
<td style="text-align: right;">0.3590</td>
<td style="text-align: right;">0.3056</td>
<td style="text-align: right;">0.3590</td>
<td style="text-align: right;">0.9466</td>
<td style="text-align: right;">0.3696</td>
</tr>
<tr class="even">
<td style="text-align: left;">SVM radial ajustado</td>
<td style="text-align: right;">0.3538</td>
<td style="text-align: right;">0.3000</td>
<td style="text-align: right;">0.3538</td>
<td style="text-align: right;">0.9462</td>
<td style="text-align: right;">0.3520</td>
</tr>
<tr class="odd">
<td style="text-align: left;">SVM radial</td>
<td style="text-align: right;">0.1436</td>
<td style="text-align: right;">0.0722</td>
<td style="text-align: right;">0.1436</td>
<td style="text-align: right;">0.9286</td>
<td style="text-align: right;">0.1928</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb51"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(resultados, <span class="fu">aes</span>(<span class="at">x =</span> <span class="fu">reorder</span>(modelo, acuracia), <span class="at">y =</span> acuracia)) <span class="sc">+</span></span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">fill =</span> <span class="st">"#2C7FB8"</span>) <span class="sc">+</span></span>
<span id="cb51-3"><a href="#cb51-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb51-4"><a href="#cb51-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb51-5"><a href="#cb51-5" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Comparacao da acuracia dos modelos"</span>,</span>
<span id="cb51-6"><a href="#cb51-6" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Modelo"</span>,</span>
<span id="cb51-7"><a href="#cb51-7" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Acuracia"</span></span>
<span id="cb51-8"><a href="#cb51-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb51-9"><a href="#cb51-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb52"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a>resultados_long <span class="ot">&lt;-</span> resultados <span class="sc">%&gt;%</span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(</span>
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a> <span class="at">cols =</span> <span class="fu">c</span>(acuracia, kappa, sensibilidade_macro, especificidade_macro, f1_macro),</span>
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a> <span class="at">names_to =</span> <span class="st">"metrica"</span>,</span>
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a> <span class="at">values_to =</span> <span class="st">"valor"</span></span>
<span id="cb52-6"><a href="#cb52-6" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb52-7"><a href="#cb52-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb52-8"><a href="#cb52-8" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(resultados_long, <span class="fu">aes</span>(<span class="at">x =</span> <span class="fu">reorder</span>(modelo, valor), <span class="at">y =</span> valor, <span class="at">fill =</span> metrica)) <span class="sc">+</span></span>
<span id="cb52-9"><a href="#cb52-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb52-10"><a href="#cb52-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb52-11"><a href="#cb52-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span> metrica, <span class="at">scales =</span> <span class="st">"free_x"</span>) <span class="sc">+</span></span>
<span id="cb52-12"><a href="#cb52-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb52-13"><a href="#cb52-13" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Metricas comparativas dos modelos"</span>,</span>
<span id="cb52-14"><a href="#cb52-14" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Modelo"</span>,</span>
<span id="cb52-15"><a href="#cb52-15" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Valor"</span></span>
<span id="cb52-16"><a href="#cb52-16" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb52-17"><a href="#cb52-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-24-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="melhor-modelo" class="level1" data-number="16">
<h1 data-number="16"><span class="header-section-number">16</span> Melhor modelo</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb53"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>melhor_modelo <span class="ot">&lt;-</span> resultados<span class="sc">$</span>modelo[<span class="dv">1</span>]</span>
<span id="cb53-2"><a href="#cb53-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb53-3"><a href="#cb53-3" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Melhor modelo pela acuracia:"</span>, melhor_modelo, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Melhor modelo pela acuracia: SVM linear </code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb55"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a>cm_melhor <span class="ot">&lt;-</span> <span class="cf">switch</span>(</span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a> melhor_modelo,</span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a> <span class="st">"SVM linear"</span> <span class="ot">=</span> cm_svm_linear,</span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"SVM radial"</span> <span class="ot">=</span> cm_svm_radial,</span>
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"SVM radial ajustado"</span> <span class="ot">=</span> cm_svm_radial_ajustado,</span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"PCA + SVM radial"</span> <span class="ot">=</span> cm_svm_pca,</span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"Random Forest"</span> <span class="ot">=</span> cm_rf</span>
<span id="cb55-8"><a href="#cb55-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb55-9"><a href="#cb55-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb55-10"><a href="#cb55-10" aria-hidden="true" tabindex="-1"></a><span class="fu">as.data.frame</span>(cm_melhor<span class="sc">$</span>table) <span class="sc">%&gt;%</span></span>
<span id="cb55-11"><a href="#cb55-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> Reference, <span class="at">y =</span> Prediction, <span class="at">fill =</span> Freq)) <span class="sc">+</span></span>
<span id="cb55-12"><a href="#cb55-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_tile</span>(<span class="at">color =</span> <span class="st">"white"</span>) <span class="sc">+</span></span>
<span id="cb55-13"><a href="#cb55-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(<span class="at">label =</span> Freq), <span class="at">size =</span> <span class="dv">3</span>) <span class="sc">+</span></span>
<span id="cb55-14"><a href="#cb55-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_gradient</span>(<span class="at">low =</span> <span class="st">"#F7FBFF"</span>, <span class="at">high =</span> <span class="st">"#08519C"</span>) <span class="sc">+</span></span>
<span id="cb55-15"><a href="#cb55-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb55-16"><a href="#cb55-16" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="fu">paste</span>(<span class="st">"Matriz de confusao -"</span>, melhor_modelo),</span>
<span id="cb55-17"><a href="#cb55-17" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Classe real"</span>,</span>
<span id="cb55-18"><a href="#cb55-18" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Classe prevista"</span>,</span>
<span id="cb55-19"><a href="#cb55-19" aria-hidden="true" tabindex="-1"></a> <span class="at">fill =</span> <span class="st">"Frequencia"</span></span>
<span id="cb55-20"><a href="#cb55-20" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb55-21"><a href="#cb55-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>() <span class="sc">+</span></span>
<span id="cb55-22"><a href="#cb55-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_text</span>(<span class="at">angle =</span> <span class="dv">45</span>, <span class="at">hjust =</span> <span class="dv">1</span>))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-26-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="importancia-das-variaveis" class="level1" data-number="17">
<h1 data-number="17"><span class="header-section-number">17</span> Importancia das variaveis</h1>
<p>A importancia das variaveis sera estimada pelo Random Forest. Essa etapa ajuda a observar quais atributos contribuiram mais para a classificacao.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb56"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 16. Importancia das variaveis</span></span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb56-4"><a href="#cb56-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a>importancia <span class="ot">&lt;-</span> <span class="fu">importance</span>(modelo_rf)</span>
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a>coluna_importancia <span class="ot">&lt;-</span> <span class="cf">if</span> (<span class="st">"MeanDecreaseGini"</span> <span class="sc">%in%</span> <span class="fu">colnames</span>(importancia)) {</span>
<span id="cb56-8"><a href="#cb56-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"MeanDecreaseGini"</span></span>
<span id="cb56-9"><a href="#cb56-9" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb56-10"><a href="#cb56-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">colnames</span>(importancia)[<span class="dv">1</span>]</span>
<span id="cb56-11"><a href="#cb56-11" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb56-12"><a href="#cb56-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-13"><a href="#cb56-13" aria-hidden="true" tabindex="-1"></a>importancia_df <span class="ot">&lt;-</span> <span class="fu">tibble</span>(</span>
<span id="cb56-14"><a href="#cb56-14" aria-hidden="true" tabindex="-1"></a> <span class="at">variavel =</span> <span class="fu">rownames</span>(importancia),</span>
<span id="cb56-15"><a href="#cb56-15" aria-hidden="true" tabindex="-1"></a> <span class="at">importancia =</span> importancia[, coluna_importancia]</span>
<span id="cb56-16"><a href="#cb56-16" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%&gt;%</span></span>
<span id="cb56-17"><a href="#cb56-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(importancia))</span>
<span id="cb56-18"><a href="#cb56-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb56-19"><a href="#cb56-19" aria-hidden="true" tabindex="-1"></a>importancia_df <span class="sc">%&gt;%</span></span>
<span id="cb56-20"><a href="#cb56-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">slice_head</span>(<span class="at">n =</span> <span class="dv">20</span>) <span class="sc">%&gt;%</span></span>
<span id="cb56-21"><a href="#cb56-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;">variavel</th>
<th style="text-align: right;">importancia</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">feat_0472</td>
<td style="text-align: right;">1.7446133</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0260</td>
<td style="text-align: right;">1.7042444</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_0473</td>
<td style="text-align: right;">1.0239011</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0334</td>
<td style="text-align: right;">1.0189685</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_0607</td>
<td style="text-align: right;">0.9355144</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0201</td>
<td style="text-align: right;">0.9340196</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_0197</td>
<td style="text-align: right;">0.9121311</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0490</td>
<td style="text-align: right;">0.8838067</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_0126</td>
<td style="text-align: right;">0.8725346</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_1198</td>
<td style="text-align: right;">0.8392922</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_0183</td>
<td style="text-align: right;">0.8291562</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0232</td>
<td style="text-align: right;">0.8134642</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_1218</td>
<td style="text-align: right;">0.8038597</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0476</td>
<td style="text-align: right;">0.7979804</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_0850</td>
<td style="text-align: right;">0.7941928</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0912</td>
<td style="text-align: right;">0.7613126</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_0729</td>
<td style="text-align: right;">0.7384886</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_0778</td>
<td style="text-align: right;">0.7236667</td>
</tr>
<tr class="odd">
<td style="text-align: left;">feat_1117</td>
<td style="text-align: right;">0.7209036</td>
</tr>
<tr class="even">
<td style="text-align: left;">feat_1091</td>
<td style="text-align: right;">0.6878679</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb57"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>importancia_df <span class="sc">%&gt;%</span></span>
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">slice_head</span>(<span class="at">n =</span> <span class="dv">20</span>) <span class="sc">%&gt;%</span></span>
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> <span class="fu">reorder</span>(variavel, importancia), <span class="at">y =</span> importancia)) <span class="sc">+</span></span>
<span id="cb57-4"><a href="#cb57-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">fill =</span> <span class="st">"#2C7FB8"</span>) <span class="sc">+</span></span>
<span id="cb57-5"><a href="#cb57-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb57-6"><a href="#cb57-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb57-7"><a href="#cb57-7" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"20 variaveis mais importantes segundo o Random Forest"</span>,</span>
<span id="cb57-8"><a href="#cb57-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Variavel"</span>,</span>
<span id="cb57-9"><a href="#cb57-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Importancia"</span></span>
<span id="cb57-10"><a href="#cb57-10" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb57-11"><a href="#cb57-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-28-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="salvamento-dos-resultados" class="level1" data-number="18">
<h1 data-number="18"><span class="header-section-number">18</span> Salvamento dos resultados</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb58"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb58-2"><a href="#cb58-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 17. Salvar saidas</span></span>
<span id="cb58-3"><a href="#cb58-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb58-4"><a href="#cb58-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-5"><a href="#cb58-5" aria-hidden="true" tabindex="-1"></a><span class="fu">dir.create</span>(pasta_saida, <span class="at">recursive =</span> <span class="cn">TRUE</span>, <span class="at">showWarnings =</span> <span class="cn">FALSE</span>)</span>
<span id="cb58-6"><a href="#cb58-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-7"><a href="#cb58-7" aria-hidden="true" tabindex="-1"></a><span class="fu">write_csv</span>(dados, <span class="fu">file.path</span>(pasta_saida, <span class="st">"features_rgb.csv"</span>))</span>
<span id="cb58-8"><a href="#cb58-8" aria-hidden="true" tabindex="-1"></a><span class="fu">write_csv</span>(resultados, <span class="fu">file.path</span>(pasta_saida, <span class="st">"resultados_modelos_rgb.csv"</span>))</span>
<span id="cb58-9"><a href="#cb58-9" aria-hidden="true" tabindex="-1"></a><span class="fu">write_csv</span>(importancia_df, <span class="fu">file.path</span>(pasta_saida, <span class="st">"importancia_variaveis_rgb.csv"</span>))</span>
<span id="cb58-10"><a href="#cb58-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-11"><a href="#cb58-11" aria-hidden="true" tabindex="-1"></a><span class="fu">saveRDS</span>(</span>
<span id="cb58-12"><a href="#cb58-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">list</span>(</span>
<span id="cb58-13"><a href="#cb58-13" aria-hidden="true" tabindex="-1"></a> <span class="at">preproc =</span> preproc,</span>
<span id="cb58-14"><a href="#cb58-14" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_linear =</span> modelo_svm_linear,</span>
<span id="cb58-15"><a href="#cb58-15" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_radial =</span> modelo_svm_radial,</span>
<span id="cb58-16"><a href="#cb58-16" aria-hidden="true" tabindex="-1"></a> <span class="at">ajuste_svm_radial =</span> ajuste_svm_radial,</span>
<span id="cb58-17"><a href="#cb58-17" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_radial_ajustado =</span> modelo_svm_radial_ajustado,</span>
<span id="cb58-18"><a href="#cb58-18" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_pca =</span> modelo_svm_pca,</span>
<span id="cb58-19"><a href="#cb58-19" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_rf =</span> modelo_rf,</span>
<span id="cb58-20"><a href="#cb58-20" aria-hidden="true" tabindex="-1"></a> <span class="at">pca =</span> pca,</span>
<span id="cb58-21"><a href="#cb58-21" aria-hidden="true" tabindex="-1"></a> <span class="at">resultados =</span> resultados,</span>
<span id="cb58-22"><a href="#cb58-22" aria-hidden="true" tabindex="-1"></a> <span class="at">extrator_cnn =</span> <span class="st">"MobileNetV2 (ImageNet, sem topo, pooling=avg, 1280 features)"</span></span>
<span id="cb58-23"><a href="#cb58-23" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb58-24"><a href="#cb58-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">file.path</span>(pasta_saida, <span class="st">"modelos_rgb.rds"</span>)</span>
<span id="cb58-25"><a href="#cb58-25" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb58-26"><a href="#cb58-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb58-27"><a href="#cb58-27" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Arquivos salvos em:"</span>, pasta_saida, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Arquivos salvos em: outputs/parcial </code></pre>
</div>
</div>
</section>
<section id="validacao-cruzada-k-fold" class="level1" data-number="19">
<h1 data-number="19"><span class="header-section-number">19</span> Validacao Cruzada K-Fold</h1>
<p>Na abordagem anterior (holdout), o conjunto de dados foi dividido uma unica vez em treino (70%) e teste (30%). Isso pode gerar uma estimativa otimista ou pessimista do desempenho real, dependendo de como a divisao ocorreu.</p>
<p>A validacao cruzada k-fold resolve esse problema dividindo o dataset em <code>k</code> partes iguais e estratificadas. Em cada rodada, uma parte diferente e usada como teste e as restantes como treino. O desempenho final e a media das <code>k</code> rodadas, o que aproveita todas as observacoes para avaliacao e produz uma estimativa mais estavel.</p>
<p>Nesta etapa, sera usado <code>k=5</code>. Para manter a avaliacao independente do holdout, o SVM radial ajustado tera <code>cost</code> e <code>gamma</code> reotimizados dentro de cada fold, usando apenas os dados de treino daquele fold.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb60"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 18. Validacao cruzada k-fold (k=5, estratificada)</span></span>
<span id="cb60-3"><a href="#cb60-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb60-4"><a href="#cb60-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-5"><a href="#cb60-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb60-6"><a href="#cb60-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-7"><a href="#cb60-7" aria-hidden="true" tabindex="-1"></a>k_folds <span class="ot">&lt;-</span> <span class="dv">5</span>L</span>
<span id="cb60-8"><a href="#cb60-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-9"><a href="#cb60-9" aria-hidden="true" tabindex="-1"></a>folds <span class="ot">&lt;-</span> <span class="fu">createFolds</span>(dados<span class="sc">$</span>classe, <span class="at">k =</span> k_folds, <span class="at">list =</span> <span class="cn">TRUE</span>, <span class="at">returnTrain =</span> <span class="cn">FALSE</span>)</span>
<span id="cb60-10"><a href="#cb60-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-11"><a href="#cb60-11" aria-hidden="true" tabindex="-1"></a>grade_cost_kfold <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">100</span>)</span>
<span id="cb60-12"><a href="#cb60-12" aria-hidden="true" tabindex="-1"></a>grade_gamma_kfold <span class="ot">&lt;-</span> <span class="fu">c</span>(<span class="fl">0.001</span>, <span class="fl">0.01</span>, <span class="fl">0.05</span>, <span class="fl">0.1</span>)</span>
<span id="cb60-13"><a href="#cb60-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-14"><a href="#cb60-14" aria-hidden="true" tabindex="-1"></a>metricas_folds <span class="ot">&lt;-</span> <span class="fu">vector</span>(<span class="st">"list"</span>, k_folds)</span>
<span id="cb60-15"><a href="#cb60-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-16"><a href="#cb60-16" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span> (fold_i <span class="cf">in</span> <span class="fu">seq_len</span>(k_folds)) {</span>
<span id="cb60-17"><a href="#cb60-17" aria-hidden="true" tabindex="-1"></a> idx_teste <span class="ot">&lt;-</span> folds[[fold_i]]</span>
<span id="cb60-18"><a href="#cb60-18" aria-hidden="true" tabindex="-1"></a> idx_treino <span class="ot">&lt;-</span> <span class="fu">setdiff</span>(<span class="fu">seq_len</span>(<span class="fu">nrow</span>(dados)), idx_teste)</span>
<span id="cb60-19"><a href="#cb60-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-20"><a href="#cb60-20" aria-hidden="true" tabindex="-1"></a> fold_treino <span class="ot">&lt;-</span> dados[idx_treino, ]</span>
<span id="cb60-21"><a href="#cb60-21" aria-hidden="true" tabindex="-1"></a> fold_teste <span class="ot">&lt;-</span> dados[idx_teste, ]</span>
<span id="cb60-22"><a href="#cb60-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-23"><a href="#cb60-23" aria-hidden="true" tabindex="-1"></a> x_fold_tr <span class="ot">&lt;-</span> fold_treino <span class="sc">%&gt;%</span> <span class="fu">select</span>(<span class="sc">-</span><span class="fu">all_of</span>(colunas_nao_preditoras))</span>
<span id="cb60-24"><a href="#cb60-24" aria-hidden="true" tabindex="-1"></a> x_fold_te <span class="ot">&lt;-</span> fold_teste <span class="sc">%&gt;%</span> <span class="fu">select</span>(<span class="sc">-</span><span class="fu">all_of</span>(colunas_nao_preditoras))</span>
<span id="cb60-25"><a href="#cb60-25" aria-hidden="true" tabindex="-1"></a> y_fold_tr <span class="ot">&lt;-</span> <span class="fu">droplevels</span>(fold_treino<span class="sc">$</span>classe)</span>
<span id="cb60-26"><a href="#cb60-26" aria-hidden="true" tabindex="-1"></a> y_fold_te <span class="ot">&lt;-</span> <span class="fu">factor</span>(fold_teste<span class="sc">$</span>classe, <span class="at">levels =</span> <span class="fu">levels</span>(y_fold_tr))</span>
<span id="cb60-27"><a href="#cb60-27" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-28"><a href="#cb60-28" aria-hidden="true" tabindex="-1"></a> nzv_fold <span class="ot">&lt;-</span> <span class="fu">nearZeroVar</span>(x_fold_tr)</span>
<span id="cb60-29"><a href="#cb60-29" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(nzv_fold) <span class="sc">&gt;</span> <span class="dv">0</span>) {</span>
<span id="cb60-30"><a href="#cb60-30" aria-hidden="true" tabindex="-1"></a> x_fold_tr <span class="ot">&lt;-</span> x_fold_tr[, <span class="sc">-</span>nzv_fold, drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
<span id="cb60-31"><a href="#cb60-31" aria-hidden="true" tabindex="-1"></a> x_fold_te <span class="ot">&lt;-</span> x_fold_te[, <span class="fu">colnames</span>(x_fold_tr), drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
<span id="cb60-32"><a href="#cb60-32" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb60-33"><a href="#cb60-33" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-34"><a href="#cb60-34" aria-hidden="true" tabindex="-1"></a> pp_fold <span class="ot">&lt;-</span> <span class="fu">preProcess</span>(x_fold_tr, <span class="at">method =</span> <span class="fu">c</span>(<span class="st">"center"</span>, <span class="st">"scale"</span>))</span>
<span id="cb60-35"><a href="#cb60-35" aria-hidden="true" tabindex="-1"></a> x_fold_tr_n <span class="ot">&lt;-</span> <span class="fu">predict</span>(pp_fold, x_fold_tr)</span>
<span id="cb60-36"><a href="#cb60-36" aria-hidden="true" tabindex="-1"></a> x_fold_te_n <span class="ot">&lt;-</span> <span class="fu">predict</span>(pp_fold, x_fold_te)</span>
<span id="cb60-37"><a href="#cb60-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-38"><a href="#cb60-38" aria-hidden="true" tabindex="-1"></a> m1 <span class="ot">&lt;-</span> <span class="fu">svm</span>(<span class="at">x =</span> x_fold_tr_n, <span class="at">y =</span> y_fold_tr,</span>
<span id="cb60-39"><a href="#cb60-39" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"linear"</span>, <span class="at">cost =</span> <span class="dv">1</span>, <span class="at">scale =</span> <span class="cn">FALSE</span>)</span>
<span id="cb60-40"><a href="#cb60-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-41"><a href="#cb60-41" aria-hidden="true" tabindex="-1"></a> m2 <span class="ot">&lt;-</span> <span class="fu">svm</span>(<span class="at">x =</span> x_fold_tr_n, <span class="at">y =</span> y_fold_tr,</span>
<span id="cb60-42"><a href="#cb60-42" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>, <span class="at">cost =</span> <span class="dv">10</span>, <span class="at">gamma =</span> <span class="fl">0.01</span>, <span class="at">scale =</span> <span class="cn">FALSE</span>)</span>
<span id="cb60-43"><a href="#cb60-43" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-44"><a href="#cb60-44" aria-hidden="true" tabindex="-1"></a> k_cv_fold <span class="ot">&lt;-</span> <span class="fu">min</span>(<span class="dv">5</span>L, <span class="fu">min</span>(<span class="fu">table</span>(y_fold_tr)))</span>
<span id="cb60-45"><a href="#cb60-45" aria-hidden="true" tabindex="-1"></a> ajuste_fold <span class="ot">&lt;-</span> <span class="fu">tune.svm</span>(</span>
<span id="cb60-46"><a href="#cb60-46" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_fold_tr_n,</span>
<span id="cb60-47"><a href="#cb60-47" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_fold_tr,</span>
<span id="cb60-48"><a href="#cb60-48" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>,</span>
<span id="cb60-49"><a href="#cb60-49" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> grade_cost_kfold,</span>
<span id="cb60-50"><a href="#cb60-50" aria-hidden="true" tabindex="-1"></a> <span class="at">gamma =</span> grade_gamma_kfold,</span>
<span id="cb60-51"><a href="#cb60-51" aria-hidden="true" tabindex="-1"></a> <span class="at">tunecontrol =</span> <span class="fu">tune.control</span>(<span class="at">cross =</span> k_cv_fold)</span>
<span id="cb60-52"><a href="#cb60-52" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb60-53"><a href="#cb60-53" aria-hidden="true" tabindex="-1"></a> m3 <span class="ot">&lt;-</span> ajuste_fold<span class="sc">$</span>best.model</span>
<span id="cb60-54"><a href="#cb60-54" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-55"><a href="#cb60-55" aria-hidden="true" tabindex="-1"></a> pca_fold <span class="ot">&lt;-</span> <span class="fu">prcomp</span>(x_fold_tr_n, <span class="at">center =</span> <span class="cn">FALSE</span>, <span class="at">scale. =</span> <span class="cn">FALSE</span>)</span>
<span id="cb60-56"><a href="#cb60-56" aria-hidden="true" tabindex="-1"></a> var_acum_f <span class="ot">&lt;-</span> <span class="fu">cumsum</span>(pca_fold<span class="sc">$</span>sdev<span class="sc">^</span><span class="dv">2</span> <span class="sc">/</span> <span class="fu">sum</span>(pca_fold<span class="sc">$</span>sdev<span class="sc">^</span><span class="dv">2</span>))</span>
<span id="cb60-57"><a href="#cb60-57" aria-hidden="true" tabindex="-1"></a> n_comp_f <span class="ot">&lt;-</span> <span class="fu">min</span>(<span class="fu">which</span>(var_acum_f <span class="sc">&gt;=</span> <span class="fl">0.95</span>)[<span class="dv">1</span>], <span class="dv">30</span>L, <span class="fu">ncol</span>(x_fold_tr_n))</span>
<span id="cb60-58"><a href="#cb60-58" aria-hidden="true" tabindex="-1"></a> x_pca_tr <span class="ot">&lt;-</span> <span class="fu">as.data.frame</span>(pca_fold<span class="sc">$</span>x[, <span class="dv">1</span><span class="sc">:</span>n_comp_f, <span class="at">drop =</span> <span class="cn">FALSE</span>])</span>
<span id="cb60-59"><a href="#cb60-59" aria-hidden="true" tabindex="-1"></a> x_pca_te <span class="ot">&lt;-</span> <span class="fu">as.data.frame</span>(<span class="fu">predict</span>(pca_fold, x_fold_te_n)[, <span class="dv">1</span><span class="sc">:</span>n_comp_f, <span class="at">drop =</span> <span class="cn">FALSE</span>])</span>
<span id="cb60-60"><a href="#cb60-60" aria-hidden="true" tabindex="-1"></a> m4 <span class="ot">&lt;-</span> <span class="fu">svm</span>(<span class="at">x =</span> x_pca_tr, <span class="at">y =</span> y_fold_tr,</span>
<span id="cb60-61"><a href="#cb60-61" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>, <span class="at">cost =</span> <span class="dv">10</span>, <span class="at">gamma =</span> <span class="fl">0.01</span>, <span class="at">scale =</span> <span class="cn">FALSE</span>)</span>
<span id="cb60-62"><a href="#cb60-62" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-63"><a href="#cb60-63" aria-hidden="true" tabindex="-1"></a> m5 <span class="ot">&lt;-</span> <span class="fu">randomForest</span>(<span class="at">x =</span> x_fold_tr_n, <span class="at">y =</span> y_fold_tr, <span class="at">ntree =</span> <span class="dv">200</span>L)</span>
<span id="cb60-64"><a href="#cb60-64" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-65"><a href="#cb60-65" aria-hidden="true" tabindex="-1"></a> metricas_folds[[fold_i]] <span class="ot">&lt;-</span> <span class="fu">bind_rows</span>(</span>
<span id="cb60-66"><a href="#cb60-66" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"SVM linear"</span>, <span class="fu">confusionMatrix</span>(<span class="fu">predict</span>(m1, x_fold_te_n), y_fold_te)),</span>
<span id="cb60-67"><a href="#cb60-67" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"SVM radial"</span>, <span class="fu">confusionMatrix</span>(<span class="fu">predict</span>(m2, x_fold_te_n), y_fold_te)),</span>
<span id="cb60-68"><a href="#cb60-68" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"SVM radial ajustado"</span>, <span class="fu">confusionMatrix</span>(<span class="fu">predict</span>(m3, x_fold_te_n), y_fold_te)),</span>
<span id="cb60-69"><a href="#cb60-69" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"PCA + SVM radial"</span>, <span class="fu">confusionMatrix</span>(<span class="fu">predict</span>(m4, x_pca_te), y_fold_te)),</span>
<span id="cb60-70"><a href="#cb60-70" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"Random Forest"</span>, <span class="fu">confusionMatrix</span>(<span class="fu">predict</span>(m5, x_fold_te_n), y_fold_te))</span>
<span id="cb60-71"><a href="#cb60-71" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="at">fold =</span> fold_i)</span>
<span id="cb60-72"><a href="#cb60-72" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb60-73"><a href="#cb60-73" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="fu">sprintf</span>(</span>
<span id="cb60-74"><a href="#cb60-74" aria-hidden="true" tabindex="-1"></a> <span class="st">"Fold %d/%d concluido. SVM ajustado: cost = %s | gamma = %s</span><span class="sc">\n</span><span class="st">"</span>,</span>
<span id="cb60-75"><a href="#cb60-75" aria-hidden="true" tabindex="-1"></a> fold_i, k_folds,</span>
<span id="cb60-76"><a href="#cb60-76" aria-hidden="true" tabindex="-1"></a> ajuste_fold<span class="sc">$</span>best.parameters<span class="sc">$</span>cost,</span>
<span id="cb60-77"><a href="#cb60-77" aria-hidden="true" tabindex="-1"></a> ajuste_fold<span class="sc">$</span>best.parameters<span class="sc">$</span>gamma</span>
<span id="cb60-78"><a href="#cb60-78" aria-hidden="true" tabindex="-1"></a> ))</span>
<span id="cb60-79"><a href="#cb60-79" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Fold 1/5 concluido. SVM ajustado: cost = 10 | gamma = 0.001
Fold 2/5 concluido. SVM ajustado: cost = 10 | gamma = 0.001
Fold 3/5 concluido. SVM ajustado: cost = 10 | gamma = 0.001
Fold 4/5 concluido. SVM ajustado: cost = 10 | gamma = 0.001
Fold 5/5 concluido. SVM ajustado: cost = 10 | gamma = 0.001</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb62"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a>resultados_kfold <span class="ot">&lt;-</span> <span class="fu">bind_rows</span>(metricas_folds) <span class="sc">%&gt;%</span></span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(modelo) <span class="sc">%&gt;%</span></span>
<span id="cb62-3"><a href="#cb62-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="fu">across</span>(<span class="fu">c</span>(acuracia, kappa, sensibilidade_macro, especificidade_macro, f1_macro), mean),</span>
<span id="cb62-4"><a href="#cb62-4" aria-hidden="true" tabindex="-1"></a> <span class="at">.groups =</span> <span class="st">"drop"</span>) <span class="sc">%&gt;%</span></span>
<span id="cb62-5"><a href="#cb62-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(acuracia))</span>
<span id="cb62-6"><a href="#cb62-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-7"><a href="#cb62-7" aria-hidden="true" tabindex="-1"></a><span class="fu">write_csv</span>(resultados_kfold, <span class="fu">file.path</span>(pasta_saida, <span class="st">"resultados_kfold_rgb.csv"</span>))</span>
<span id="cb62-8"><a href="#cb62-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb62-9"><a href="#cb62-9" aria-hidden="true" tabindex="-1"></a>resultados_kfold <span class="sc">%&gt;%</span></span>
<span id="cb62-10"><a href="#cb62-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">where</span>(is.numeric), <span class="sc">~</span> <span class="fu">round</span>(.x, <span class="dv">4</span>))) <span class="sc">%&gt;%</span></span>
<span id="cb62-11"><a href="#cb62-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>(<span class="at">caption =</span> <span class="st">"Acuracia media - K-Fold (k=5)"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Acuracia media - K-Fold (k=5)</caption>
<colgroup>
<col style="width: 23%">
<col style="width: 10%">
<col style="width: 8%">
<col style="width: 23%">
<col style="width: 24%">
<col style="width: 10%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">modelo</th>
<th style="text-align: right;">acuracia</th>
<th style="text-align: right;">kappa</th>
<th style="text-align: right;">sensibilidade_macro</th>
<th style="text-align: right;">especificidade_macro</th>
<th style="text-align: right;">f1_macro</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">SVM linear</td>
<td style="text-align: right;">0.4015</td>
<td style="text-align: right;">0.3517</td>
<td style="text-align: right;">0.4015</td>
<td style="text-align: right;">0.9501</td>
<td style="text-align: right;">0.4093</td>
</tr>
<tr class="even">
<td style="text-align: left;">SVM radial ajustado</td>
<td style="text-align: right;">0.3800</td>
<td style="text-align: right;">0.3283</td>
<td style="text-align: right;">0.3800</td>
<td style="text-align: right;">0.9483</td>
<td style="text-align: right;">0.3905</td>
</tr>
<tr class="odd">
<td style="text-align: left;">PCA + SVM radial</td>
<td style="text-align: right;">0.3677</td>
<td style="text-align: right;">0.3150</td>
<td style="text-align: right;">0.3677</td>
<td style="text-align: right;">0.9473</td>
<td style="text-align: right;">0.3784</td>
</tr>
<tr class="even">
<td style="text-align: left;">Random Forest</td>
<td style="text-align: right;">0.3569</td>
<td style="text-align: right;">0.3033</td>
<td style="text-align: right;">0.3569</td>
<td style="text-align: right;">0.9464</td>
<td style="text-align: right;">0.3806</td>
</tr>
<tr class="odd">
<td style="text-align: left;">SVM radial</td>
<td style="text-align: right;">0.1585</td>
<td style="text-align: right;">0.0883</td>
<td style="text-align: right;">0.1585</td>
<td style="text-align: right;">0.9299</td>
<td style="text-align: right;">0.2233</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb63"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(resultados_kfold, <span class="fu">aes</span>(<span class="at">x =</span> <span class="fu">reorder</span>(modelo, acuracia), <span class="at">y =</span> acuracia)) <span class="sc">+</span></span>
<span id="cb63-2"><a href="#cb63-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">fill =</span> <span class="st">"#E06C00"</span>) <span class="sc">+</span></span>
<span id="cb63-3"><a href="#cb63-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb63-4"><a href="#cb63-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb63-5"><a href="#cb63-5" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Acuracia media - Validacao cruzada 5-fold"</span>,</span>
<span id="cb63-6"><a href="#cb63-6" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Modelo"</span>,</span>
<span id="cb63-7"><a href="#cb63-7" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Acuracia media"</span></span>
<span id="cb63-8"><a href="#cb63-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb63-9"><a href="#cb63-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-32-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="comparacao-holdout-vs-k-fold" class="level1" data-number="20">
<h1 data-number="20"><span class="header-section-number">20</span> Comparacao: Holdout vs K-Fold</h1>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb64"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 19. Comparacao direta entre as duas estrategias</span></span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
<span id="cb64-4"><a href="#cb64-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-5"><a href="#cb64-5" aria-hidden="true" tabindex="-1"></a>comparacao <span class="ot">&lt;-</span> <span class="fu">bind_rows</span>(</span>
<span id="cb64-6"><a href="#cb64-6" aria-hidden="true" tabindex="-1"></a> resultados <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="at">estrategia =</span> <span class="st">"Holdout 70/30"</span>),</span>
<span id="cb64-7"><a href="#cb64-7" aria-hidden="true" tabindex="-1"></a> resultados_kfold <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="at">estrategia =</span> <span class="st">"K-Fold (k=5)"</span>)</span>
<span id="cb64-8"><a href="#cb64-8" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%&gt;%</span></span>
<span id="cb64-9"><a href="#cb64-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(estrategia, modelo, acuracia, kappa, f1_macro) <span class="sc">%&gt;%</span></span>
<span id="cb64-10"><a href="#cb64-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(estrategia, <span class="fu">desc</span>(acuracia))</span>
<span id="cb64-11"><a href="#cb64-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-12"><a href="#cb64-12" aria-hidden="true" tabindex="-1"></a>comparacao <span class="sc">%&gt;%</span></span>
<span id="cb64-13"><a href="#cb64-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">where</span>(is.numeric), <span class="sc">~</span> <span class="fu">round</span>(.x, <span class="dv">4</span>))) <span class="sc">%&gt;%</span></span>
<span id="cb64-14"><a href="#cb64-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>(<span class="at">caption =</span> <span class="st">"Comparacao entre Holdout 70/30 e Validacao Cruzada K-Fold"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<caption>Comparacao entre Holdout 70/30 e Validacao Cruzada K-Fold</caption>
<thead>
<tr class="header">
<th style="text-align: left;">estrategia</th>
<th style="text-align: left;">modelo</th>
<th style="text-align: right;">acuracia</th>
<th style="text-align: right;">kappa</th>
<th style="text-align: right;">f1_macro</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Holdout 70/30</td>
<td style="text-align: left;">SVM linear</td>
<td style="text-align: right;">0.4000</td>
<td style="text-align: right;">0.3500</td>
<td style="text-align: right;">0.4019</td>
</tr>
<tr class="even">
<td style="text-align: left;">Holdout 70/30</td>
<td style="text-align: left;">Random Forest</td>
<td style="text-align: right;">0.3744</td>
<td style="text-align: right;">0.3222</td>
<td style="text-align: right;">0.3688</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holdout 70/30</td>
<td style="text-align: left;">PCA + SVM radial</td>
<td style="text-align: right;">0.3590</td>
<td style="text-align: right;">0.3056</td>
<td style="text-align: right;">0.3696</td>
</tr>
<tr class="even">
<td style="text-align: left;">Holdout 70/30</td>
<td style="text-align: left;">SVM radial ajustado</td>
<td style="text-align: right;">0.3538</td>
<td style="text-align: right;">0.3000</td>
<td style="text-align: right;">0.3520</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Holdout 70/30</td>
<td style="text-align: left;">SVM radial</td>
<td style="text-align: right;">0.1436</td>
<td style="text-align: right;">0.0722</td>
<td style="text-align: right;">0.1928</td>
</tr>
<tr class="even">
<td style="text-align: left;">K-Fold (k=5)</td>
<td style="text-align: left;">SVM linear</td>
<td style="text-align: right;">0.4015</td>
<td style="text-align: right;">0.3517</td>
<td style="text-align: right;">0.4093</td>
</tr>
<tr class="odd">
<td style="text-align: left;">K-Fold (k=5)</td>
<td style="text-align: left;">SVM radial ajustado</td>
<td style="text-align: right;">0.3800</td>
<td style="text-align: right;">0.3283</td>
<td style="text-align: right;">0.3905</td>
</tr>
<tr class="even">
<td style="text-align: left;">K-Fold (k=5)</td>
<td style="text-align: left;">PCA + SVM radial</td>
<td style="text-align: right;">0.3677</td>
<td style="text-align: right;">0.3150</td>
<td style="text-align: right;">0.3784</td>
</tr>
<tr class="odd">
<td style="text-align: left;">K-Fold (k=5)</td>
<td style="text-align: left;">Random Forest</td>
<td style="text-align: right;">0.3569</td>
<td style="text-align: right;">0.3033</td>
<td style="text-align: right;">0.3806</td>
</tr>
<tr class="even">
<td style="text-align: left;">K-Fold (k=5)</td>
<td style="text-align: left;">SVM radial</td>
<td style="text-align: right;">0.1585</td>
<td style="text-align: right;">0.0883</td>
<td style="text-align: right;">0.2233</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb65"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(comparacao, <span class="fu">aes</span>(<span class="at">x =</span> <span class="fu">reorder</span>(modelo, acuracia), <span class="at">y =</span> acuracia, <span class="at">fill =</span> estrategia)) <span class="sc">+</span></span>
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>(<span class="at">position =</span> <span class="st">"dodge"</span>) <span class="sc">+</span></span>
<span id="cb65-3"><a href="#cb65-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb65-4"><a href="#cb65-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"Holdout 70/30"</span> <span class="ot">=</span> <span class="st">"#2C7FB8"</span>, <span class="st">"K-Fold (k=5)"</span> <span class="ot">=</span> <span class="st">"#E06C00"</span>)) <span class="sc">+</span></span>
<span id="cb65-5"><a href="#cb65-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
<span id="cb65-6"><a href="#cb65-6" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Acuracia: Holdout 70/30 vs K-Fold (k=5)"</span>,</span>
<span id="cb65-7"><a href="#cb65-7" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Modelo"</span>,</span>
<span id="cb65-8"><a href="#cb65-8" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Acuracia"</span>,</span>
<span id="cb65-9"><a href="#cb65-9" aria-hidden="true" tabindex="-1"></a> <span class="at">fill =</span> <span class="st">"Estrategia"</span></span>
<span id="cb65-10"><a href="#cb65-10" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb65-11"><a href="#cb65-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_minimal</span>()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="trabalho_svm_rgb_files/figure-html/unnamed-chunk-34-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb66"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a>melhor_holdout <span class="ot">&lt;-</span> <span class="fu">max</span>(resultados<span class="sc">$</span>acuracia)</span>
<span id="cb66-2"><a href="#cb66-2" aria-hidden="true" tabindex="-1"></a>melhor_kf <span class="ot">&lt;-</span> <span class="fu">max</span>(resultados_kfold<span class="sc">$</span>acuracia)</span>
<span id="cb66-3"><a href="#cb66-3" aria-hidden="true" tabindex="-1"></a>melhor_metodo <span class="ot">&lt;-</span> <span class="cf">if</span> (melhor_kf <span class="sc">&gt;=</span> melhor_holdout) <span class="st">"K-Fold (k=5)"</span> <span class="cf">else</span> <span class="st">"Holdout 70/30"</span></span>
<span id="cb66-4"><a href="#cb66-4" aria-hidden="true" tabindex="-1"></a>resultados_finais <span class="ot">&lt;-</span> <span class="cf">if</span> (melhor_kf <span class="sc">&gt;=</span> melhor_holdout) resultados_kfold <span class="cf">else</span> resultados</span>
<span id="cb66-5"><a href="#cb66-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb66-6"><a href="#cb66-6" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Acuracia maxima - Holdout:"</span>, <span class="fu">round</span>(melhor_holdout, <span class="dv">4</span>), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Acuracia maxima - Holdout: 0.4 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb68"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Acuracia maxima - K-Fold: "</span>, <span class="fu">round</span>(melhor_kf, <span class="dv">4</span>), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Acuracia maxima - K-Fold: 0.4015 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb70"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Metodo adotado como referencia:"</span>, melhor_metodo, <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>Metodo adotado como referencia: K-Fold (k=5) </code></pre>
</div>
</div>
</section>
<section id="discussao" class="level1" data-number="21">
<h1 data-number="21"><span class="header-section-number">21</span> Discussao</h1>
<p>Os resultados permitem comparar diferentes classificadores aplicados sobre atributos extraidos por <em>transfer learning</em> com a rede MobileNetV2, e tambem avaliar o impacto da estrategia de avaliacao adotada.</p>
<p>A extracao de atributos por CNN pre-treinada substitui o calculo manual de medias, desvios e histogramas de cor por um vetor de 1280 atributos que codificam padroes visuais de alto nivel aprendidos em mais de um milhao de imagens do ImageNet. Essa representacao tende a ser muito mais discriminativa para problemas de classificacao de cenas.</p>
<p>O SVM linear busca fronteiras lineares nesse espaco de 1280 dimensoes. O SVM radial permite fronteiras nao lineares, sendo mais flexivel. O PCA reduz a dimensionalidade antes do SVM, eliminando redundancias entre as 1280 features da CNN. O Random Forest serve como referencia nao-linear e permite identificar quais atributos foram mais relevantes.</p>
<p><strong>Sobre a estrategia de avaliacao:</strong> na primeira abordagem, o dataset foi dividido uma unica vez em 70% treino e 30% teste (holdout). Embora simples, esse metodo pode gerar estimativas instáveis com datasets de tamanho moderado, pois o resultado depende de como essa divisao ocorreu. Para verificar se os resultados eram representativos, o experimento foi repetido com validacao cruzada k-fold com k=5: o dataset e dividido em 5 partes estratificadas e, em cada rodada, uma parte diferente e usada como teste. O desempenho final e a media das 5 rodadas.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb72"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (melhor_kf <span class="sc">&gt;=</span> melhor_holdout) {</span>
<span id="cb72-2"><a href="#cb72-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"O k-fold produziu acuracia igual ou superior ao holdout, sendo adotado como resultado principal.</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb72-3"><a href="#cb72-3" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb72-4"><a href="#cb72-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="st">"O holdout produziu acuracia superior ao k-fold. Isso pode indicar que a divisao holdout foi favoravel.</span><span class="sc">\n</span><span class="st">"</span>,</span>
<span id="cb72-5"><a href="#cb72-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"O k-fold e metodologicamente mais robusto, mas neste caso o holdout resultou em estimativa mais otimista.</span><span class="sc">\n</span><span class="st">"</span>)</span>
<span id="cb72-6"><a href="#cb72-6" aria-hidden="true" tabindex="-1"></a>}</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>O k-fold produziu acuracia igual ou superior ao holdout, sendo adotado como resultado principal.</code></pre>
</div>
</div>
</section>
<section id="conclusao" class="level1" data-number="22">
<h1 data-number="22"><span class="header-section-number">22</span> Conclusao</h1>
<p>Este trabalho implementou uma pipeline de classificacao de imagens RGB baseada em <em>transfer learning</em>, reducao de dimensionalidade e classificadores supervisionados, avaliada por duas estrategias distintas.</p>
<p>A estrategia de usar a MobileNetV2 pre-treinada como extrator de atributos, seguida de SVM ou Random Forest, combina o poder de representacao das redes neurais com a interpretabilidade dos classificadores classicos, sem necessidade de treinar a CNN do zero.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb74"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb74-1"><a href="#cb74-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="fu">sprintf</span>(</span>
<span id="cb74-2"><a href="#cb74-2" aria-hidden="true" tabindex="-1"></a> <span class="st">"O melhor resultado foi obtido com '%s' usando a estrategia '%s', com acuracia de %.1f%%.</span><span class="sc">\n</span><span class="st">"</span>,</span>
<span id="cb74-3"><a href="#cb74-3" aria-hidden="true" tabindex="-1"></a> resultados_finais<span class="sc">$</span>modelo[<span class="dv">1</span>],</span>
<span id="cb74-4"><a href="#cb74-4" aria-hidden="true" tabindex="-1"></a> melhor_metodo,</span>
<span id="cb74-5"><a href="#cb74-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">max</span>(resultados_finais<span class="sc">$</span>acuracia) <span class="sc">*</span> <span class="dv">100</span></span>
<span id="cb74-6"><a href="#cb74-6" aria-hidden="true" tabindex="-1"></a>))</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
<div class="cell-output cell-output-stdout">
<pre><code>O melhor resultado foi obtido com 'SVM linear' usando a estrategia 'K-Fold (k=5)', com acuracia de 40.2%.</code></pre>
</div>
</div>
<p>Como expansao futura, o trabalho pode ser repetido com outros extratores CNN presentes no material da disciplina (EfficientNetB0, ResNet50, VGG19) e com a incorporacao das imagens TIR ao vetor de atributos, aproveitando a informacao termica disponivel no dataset.</p>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const onCopySuccess = function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
}
const getTextToCopy = function(trigger) {
const outerScaffold = trigger.parentElement.cloneNode(true);
const codeEl = outerScaffold.querySelector('code');
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
text: getTextToCopy
});
clipboard.on('success', onCopySuccess);
if (window.document.getElementById('quarto-embedded-source-code-modal')) {
const clipboardModal = new window.ClipboardJS('.code-copy-button[data-in-quarto-modal]', {
text: getTextToCopy,
container: window.document.getElementById('quarto-embedded-source-code-modal')
});
clipboardModal.on('success', onCopySuccess);
}
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var mailtoRegex = new RegExp(/^mailto:/);
var filterRegex = new RegExp('/' + window.location.host + '/');
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool):not(.about-link)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// undo the damage that might have been done by quarto-nav.js in the case of
// links that we want to consider external
if (link.dataset.originalHref !== undefined) {
link.href = link.dataset.originalHref;
}
}
}
function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
const config = {
allowHTML: true,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start',
};
if (contentFn) {
config.content = contentFn;
}
if (onTriggerFn) {
config.onTrigger = onTriggerFn;
}
if (onUntriggerFn) {
config.onUntrigger = onUntriggerFn;
}
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
if (note) {
return note.innerHTML;
} else {
return "";
}
});
}
const xrefs = window.document.querySelectorAll('a.quarto-xref');
const processXRef = (id, note) => {
// Strip column container classes
const stripColumnClz = (el) => {
el.classList.remove("page-full", "page-columns");
if (el.children) {
for (const child of el.children) {
stripColumnClz(child);
}
}
}
stripColumnClz(note)
if (id === null || id.startsWith('sec-')) {
// Special case sections, only their first couple elements
const container = document.createElement("div");
if (note.children && note.children.length > 2) {
container.appendChild(note.children[0].cloneNode(true));
for (let i = 1; i < note.children.length; i++) {
const child = note.children[i];
if (child.tagName === "P" && child.innerText === "") {
continue;
} else {
container.appendChild(child.cloneNode(true));
break;
}
}
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(container);
}
return container.innerHTML
} else {
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(note);
}
return note.innerHTML;
}
} else {
// Remove any anchor links if they are present
const anchorLink = note.querySelector('a.anchorjs-link');
if (anchorLink) {
anchorLink.remove();
}
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(note);
}
if (note.classList.contains("callout")) {
return note.outerHTML;
} else {
return note.innerHTML;
}
}
}
for (var i=0; i<xrefs.length; i++) {
const xref = xrefs[i];
tippyHover(xref, undefined, function(instance) {
instance.disable();
let url = xref.getAttribute('href');
let hash = undefined;
if (url.startsWith('#')) {
hash = url;
} else {
try { hash = new URL(url).hash; } catch {}
}
if (hash) {
const id = hash.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
if (note !== null) {
try {
const html = processXRef(id, note.cloneNode(true));
instance.setContent(html);
} finally {
instance.enable();
instance.show();
}
} else {
// See if we can fetch this
fetch(url.split('#')[0])
.then(res => res.text())
.then(html => {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(html, "text/html");
const note = htmlDoc.getElementById(id);
if (note !== null) {
const html = processXRef(id, note);
instance.setContent(html);
}
}).finally(() => {
instance.enable();
instance.show();
});
}
} else {
// See if we can fetch a full url (with no hash to target)
// This is a special case and we should probably do some content thinning / targeting
fetch(url)
.then(res => res.text())
.then(html => {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(html, "text/html");
const note = htmlDoc.querySelector('main.content');
if (note !== null) {
// This should only happen for chapter cross references
// (since there is no id in the URL)
// remove the first header
if (note.children.length > 0 && note.children[0].tagName === "HEADER") {
note.children[0].remove();
}
const html = processXRef(null, note);
instance.setContent(html);
}
}).finally(() => {
instance.enable();
instance.show();
});
}
}, function(instance) {
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;
}
if (top !== null && height !== null && parent !== null) {
// cook up a div (if necessary) and position it
let div = window.document.getElementById("code-annotation-line-highlight");
if (div === null) {
div = window.document.createElement("div");
div.setAttribute("id", "code-annotation-line-highlight");
div.style.position = 'absolute';
parent.appendChild(div);
}
div.style.top = top - 2 + "px";
div.style.height = height + 4 + "px";
div.style.left = 0;
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
if (gutterDiv === null) {
gutterDiv = window.document.createElement("div");
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
gutterDiv.style.position = 'absolute';
const codeCell = window.document.getElementById(targetCell);
const gutter = codeCell.querySelector('.code-annotation-gutter');
gutter.appendChild(gutterDiv);
}
gutterDiv.style.top = top - 2 + "px";
gutterDiv.style.height = height + 4 + "px";
}
selectedAnnoteEl = annoteEl;
}
};
const unselectCodeLines = () => {
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
elementsIds.forEach((elId) => {
const div = window.document.getElementById(elId);
if (div) {
div.remove();
}
});
selectedAnnoteEl = undefined;
};
// Handle positioning of the toggle
window.addEventListener(
"resize",
throttle(() => {
elRect = undefined;
if (selectedAnnoteEl) {
selectCodeLines(selectedAnnoteEl);
}
}, 10)
);
function throttle(fn, ms) {
let throttle = false;
let timer;
return (...args) => {
if(!throttle) { // first call gets through
fn.apply(this, args);
throttle = true;
} else { // all the others get throttled
if(timer) clearTimeout(timer); // cancel #2
timer = setTimeout(() => {
fn.apply(this, args);
timer = throttle = false;
}, ms);
}
};
}
// Attach click handler to the DT
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
for (const annoteDlNode of annoteDls) {
annoteDlNode.addEventListener('click', (event) => {
const clickedEl = event.target;
if (clickedEl !== selectedAnnoteEl) {
unselectCodeLines();
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
if (activeEl) {
activeEl.classList.remove('code-annotation-active');
}
selectCodeLines(clickedEl);
clickedEl.classList.add('code-annotation-active');
} else {
// Unselect the line
unselectCodeLines();
clickedEl.classList.remove('code-annotation-active');
}
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>