2332 lines
182 KiB
HTML
2332 lines
182 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 (Dataset Completo)</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_completo_files/libs/clipboard/clipboard.min.js"></script>
|
|
<script src="trabalho_svm_rgb_completo_files/libs/quarto-html/quarto.js" type="module"></script>
|
|
<script src="trabalho_svm_rgb_completo_files/libs/quarto-html/tabsets/tabsets.js" type="module"></script>
|
|
<script src="trabalho_svm_rgb_completo_files/libs/quarto-html/popper.min.js"></script>
|
|
<script src="trabalho_svm_rgb_completo_files/libs/quarto-html/tippy.umd.min.js"></script>
|
|
<script src="trabalho_svm_rgb_completo_files/libs/quarto-html/anchor.min.js"></script>
|
|
<link href="trabalho_svm_rgb_completo_files/libs/quarto-html/tippy.css" rel="stylesheet">
|
|
<link href="trabalho_svm_rgb_completo_files/libs/quarto-html/quarto-syntax-highlighting-7f8f88aac4f3542376d5c11b86a4c14d.css" rel="stylesheet" id="quarto-text-highlighting-styles">
|
|
<script src="trabalho_svm_rgb_completo_files/libs/bootstrap/bootstrap.min.js"></script>
|
|
<link href="trabalho_svm_rgb_completo_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
|
|
<link href="trabalho_svm_rgb_completo_files/libs/bootstrap/bootstrap-67270d1595a62d14f123c043d4fb2f18.min.css" rel="stylesheet" append-hash="true" id="quarto-bootstrap" data-mode="light">
|
|
<link href="trabalho_svm_rgb_completo_files/libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet">
|
|
|
|
<script src="trabalho_svm_rgb_completo_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-completa-sem-limite-por-classe" id="toc-amostra-completa-sem-limite-por-classe" class="nav-link" data-scroll-target="#amostra-completa-sem-limite-por-classe"><span class="header-section-number">5</span> Amostra completa (sem limite por classe)</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="#importancia-das-variaveis" id="toc-importancia-das-variaveis" class="nav-link" data-scroll-target="#importancia-das-variaveis"><span class="header-section-number">16</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">17</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">18</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">19</span> Comparacao: Holdout vs K-Fold</a></li>
|
|
<li><a href="#conclusao" id="toc-conclusao" class="nav-link" data-scroll-target="#conclusao"><span class="header-section-number">20</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 (Dataset Completo)</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><strong>Diferenca em relacao ao trabalho original:</strong> esta versao utiliza TODAS as imagens disponiveis por classe (sem limite de 50), para avaliar o impacto do tamanho do dataset no desempenho dos modelos.</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>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"><-</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"><-</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">></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"><-</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>
|
|
<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"><-</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>
|
|
<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"><-</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"><-</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"><-</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"><-</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"><-</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"><-</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"><-</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"><-</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"><-</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">>=</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"><-</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">%>%</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">%>%</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"><-</span> metadados_completo <span class="sc">%>%</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">%>%</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">%>%</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">%>%</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-completa-sem-limite-por-classe" class="level1" data-number="5">
|
|
<h1 data-number="5"><span class="header-section-number">5</span> Amostra completa (sem limite por classe)</h1>
|
|
<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. Usar TODAS as imagens disponíveis 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><span class="co"># Alteracao em relacao ao original: max_imagens_por_classe = 9999</span></span>
|
|
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a><span class="co"># para usar todas as imagens disponiveis</span></span>
|
|
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a>max_imagens_por_classe <span class="ot"><-</span> <span class="dv">9999</span></span>
|
|
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a>metadados <span class="ot"><-</span> metadados_completo <span class="sc">%>%</span></span>
|
|
<span id="cb8-12"><a href="#cb8-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(classe) <span class="sc">%>%</span></span>
|
|
<span id="cb8-13"><a href="#cb8-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_modify</span>(<span class="sc">~</span> {</span>
|
|
<span id="cb8-14"><a href="#cb8-14" aria-hidden="true" tabindex="-1"></a> qtd <span class="ot"><-</span> <span class="fu">min</span>(max_imagens_por_classe, <span class="fu">nrow</span>(.x))</span>
|
|
<span id="cb8-15"><a href="#cb8-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">slice_sample</span>(.x, <span class="at">n =</span> qtd)</span>
|
|
<span id="cb8-16"><a href="#cb8-16" aria-hidden="true" tabindex="-1"></a> }) <span class="sc">%>%</span></span>
|
|
<span id="cb8-17"><a href="#cb8-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">ungroup</span>() <span class="sc">%>%</span></span>
|
|
<span id="cb8-18"><a href="#cb8-18" 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-19"><a href="#cb8-19" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb8-20"><a href="#cb8-20" 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: 1248 </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">%>%</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">%>%</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">%>%</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;">96</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C2</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C3</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C4</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C5</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C6</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C7</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C8</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C9</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C10</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C11</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C12</td>
|
|
<td style="text-align: right;">96</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C13</td>
|
|
<td style="text-align: right;">96</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 (dataset completo)"</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_completo_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>
|
|
<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"><-</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>
|
|
<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 (com cache separado do original)</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"><-</span> <span class="st">"outputs/completo"</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><span class="co"># Nomes diferentes para nao sobrescrever o cache do trabalho original</span></span>
|
|
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a>arquivo_cache_features <span class="ot"><-</span> <span class="fu">file.path</span>(pasta_saida, <span class="st">"features_rgb_mobilenetv2_completo.rds"</span>)</span>
|
|
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a>arquivo_cache_parcial <span class="ot"><-</span> <span class="fu">file.path</span>(pasta_saida, <span class="st">"features_rgb_mobilenetv2_completo_temp.rds"</span>)</span>
|
|
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a>tamanho_imagem <span class="ot"><-</span> <span class="dv">224</span>L</span>
|
|
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a>tamanho_lote <span class="ot"><-</span> <span class="dv">16</span>L</span>
|
|
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a>extrair_features_batch <span class="ot"><-</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-16"><a href="#cb18-16" aria-hidden="true" tabindex="-1"></a> n <span class="ot"><-</span> <span class="fu">length</span>(arquivos)</span>
|
|
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true" tabindex="-1"></a> resultados <span class="ot"><-</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-18"><a href="#cb18-18" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-19"><a href="#cb18-19" 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-20"><a href="#cb18-20" aria-hidden="true" tabindex="-1"></a> fim <span class="ot"><-</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-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a> bloco <span class="ot"><-</span> arquivos[inicio<span class="sc">:</span>fim]</span>
|
|
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a> tam_bloco <span class="ot"><-</span> <span class="fu">length</span>(bloco)</span>
|
|
<span id="cb18-23"><a href="#cb18-23" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a> batch_array <span class="ot"><-</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-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-26"><a href="#cb18-26" 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-27"><a href="#cb18-27" aria-hidden="true" tabindex="-1"></a> img <span class="ot"><-</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-28"><a href="#cb18-28" aria-hidden="true" tabindex="-1"></a> batch_array[j, , , ] <span class="ot"><-</span> <span class="fu">image_to_array</span>(img)</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></span>
|
|
<span id="cb18-31"><a href="#cb18-31" aria-hidden="true" tabindex="-1"></a> batch_prep <span class="ot"><-</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-32"><a href="#cb18-32" aria-hidden="true" tabindex="-1"></a> features <span class="ot"><-</span> <span class="fu">predict</span>(modelo, batch_prep, <span class="at">verbose =</span> <span class="dv">0</span>L)</span>
|
|
<span id="cb18-33"><a href="#cb18-33" aria-hidden="true" tabindex="-1"></a> resultados[inicio<span class="sc">:</span>fim, ] <span class="ot"><-</span> features</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></span>
|
|
<span id="cb18-36"><a href="#cb18-36" aria-hidden="true" tabindex="-1"></a> resultados</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>
|
|
<span id="cb18-39"><a href="#cb18-39" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">file.exists</span>(arquivo_cache_features)) {</span>
|
|
<span id="cb18-40"><a href="#cb18-40" aria-hidden="true" tabindex="-1"></a> dados <span class="ot"><-</span> <span class="fu">readRDS</span>(arquivo_cache_features)</span>
|
|
<span id="cb18-41"><a href="#cb18-41" 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-42"><a href="#cb18-42" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
|
|
<span id="cb18-43"><a href="#cb18-43" 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-44"><a href="#cb18-44" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-45"><a href="#cb18-45" aria-hidden="true" tabindex="-1"></a> erros_idx <span class="ot"><-</span> <span class="fu">integer</span>(<span class="dv">0</span>)</span>
|
|
<span id="cb18-46"><a href="#cb18-46" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-47"><a href="#cb18-47" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">file.exists</span>(arquivo_cache_parcial)) {</span>
|
|
<span id="cb18-48"><a href="#cb18-48" aria-hidden="true" tabindex="-1"></a> features_matrix <span class="ot"><-</span> <span class="fu">readRDS</span>(arquivo_cache_parcial)</span>
|
|
<span id="cb18-49"><a href="#cb18-49" aria-hidden="true" tabindex="-1"></a> linhas_prontas <span class="ot"><-</span> <span class="fu">which</span>(<span class="fu">complete.cases</span>(features_matrix))</span>
|
|
<span id="cb18-50"><a href="#cb18-50" 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-51"><a href="#cb18-51" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
|
|
<span id="cb18-52"><a href="#cb18-52" aria-hidden="true" tabindex="-1"></a> n_features <span class="ot"><-</span> <span class="fu">as.integer</span>(modelo_base<span class="sc">$</span>output_shape[[<span class="dv">2</span>]])</span>
|
|
<span id="cb18-53"><a href="#cb18-53" aria-hidden="true" tabindex="-1"></a> features_matrix <span class="ot"><-</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-54"><a href="#cb18-54" aria-hidden="true" tabindex="-1"></a> linhas_prontas <span class="ot"><-</span> <span class="fu">integer</span>(<span class="dv">0</span>)</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></span>
|
|
<span id="cb18-57"><a href="#cb18-57" aria-hidden="true" tabindex="-1"></a> linhas_pendentes <span class="ot"><-</span> <span class="fu">setdiff</span>(<span class="fu">seq_len</span>(<span class="fu">nrow</span>(metadados)), linhas_prontas)</span>
|
|
<span id="cb18-58"><a href="#cb18-58" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-59"><a href="#cb18-59" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(linhas_pendentes) <span class="sc">></span> <span class="dv">0</span>) {</span>
|
|
<span id="cb18-60"><a href="#cb18-60" 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-61"><a href="#cb18-61" aria-hidden="true" tabindex="-1"></a> fim <span class="ot"><-</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-62"><a href="#cb18-62" aria-hidden="true" tabindex="-1"></a> idxs <span class="ot"><-</span> linhas_pendentes[inicio<span class="sc">:</span>fim]</span>
|
|
<span id="cb18-63"><a href="#cb18-63" aria-hidden="true" tabindex="-1"></a> arquivos_bloco <span class="ot"><-</span> metadados<span class="sc">$</span>arquivo[idxs]</span>
|
|
<span id="cb18-64"><a href="#cb18-64" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-65"><a href="#cb18-65" aria-hidden="true" tabindex="-1"></a> resultado <span class="ot"><-</span> <span class="fu">tryCatch</span>(</span>
|
|
<span id="cb18-66"><a href="#cb18-66" 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-67"><a href="#cb18-67" aria-hidden="true" tabindex="-1"></a> <span class="at">error =</span> <span class="cf">function</span>(e) {</span>
|
|
<span id="cb18-68"><a href="#cb18-68" 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-69"><a href="#cb18-69" aria-hidden="true" tabindex="-1"></a> erros_idx <span class="ot"><<-</span> <span class="fu">c</span>(erros_idx, idxs)</span>
|
|
<span id="cb18-70"><a href="#cb18-70" aria-hidden="true" tabindex="-1"></a> <span class="cn">NULL</span></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>
|
|
<span id="cb18-74"><a href="#cb18-74" 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-75"><a href="#cb18-75" aria-hidden="true" tabindex="-1"></a> features_matrix[idxs, ] <span class="ot"><-</span> resultado</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>
|
|
<span id="cb18-78"><a href="#cb18-78" 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-79"><a href="#cb18-79" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-80"><a href="#cb18-80" 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-81"><a href="#cb18-81" aria-hidden="true" tabindex="-1"></a> <span class="fu">saveRDS</span>(features_matrix, arquivo_cache_parcial)</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>
|
|
<span id="cb18-84"><a href="#cb18-84" 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-85"><a href="#cb18-85" aria-hidden="true" tabindex="-1"></a> <span class="fu">saveRDS</span>(features_matrix, arquivo_cache_parcial)</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></span>
|
|
<span id="cb18-88"><a href="#cb18-88" aria-hidden="true" tabindex="-1"></a> imagens_ok <span class="ot"><-</span> <span class="fu">complete.cases</span>(features_matrix)</span>
|
|
<span id="cb18-89"><a href="#cb18-89" aria-hidden="true" tabindex="-1"></a> n_features <span class="ot"><-</span> <span class="fu">ncol</span>(features_matrix)</span>
|
|
<span id="cb18-90"><a href="#cb18-90" aria-hidden="true" tabindex="-1"></a> nomes_feat <span class="ot"><-</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-91"><a href="#cb18-91" aria-hidden="true" tabindex="-1"></a> <span class="fu">colnames</span>(features_matrix) <span class="ot"><-</span> nomes_feat</span>
|
|
<span id="cb18-92"><a href="#cb18-92" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-93"><a href="#cb18-93" aria-hidden="true" tabindex="-1"></a> dados <span class="ot"><-</span> <span class="fu">bind_cols</span>(</span>
|
|
<span id="cb18-94"><a href="#cb18-94" aria-hidden="true" tabindex="-1"></a> metadados[imagens_ok, ],</span>
|
|
<span id="cb18-95"><a href="#cb18-95" aria-hidden="true" tabindex="-1"></a> <span class="fu">as_tibble</span>(features_matrix[imagens_ok, ])</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>
|
|
<span id="cb18-98"><a href="#cb18-98" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(erros_idx) <span class="sc">></span> <span class="dv">0</span>) {</span>
|
|
<span id="cb18-99"><a href="#cb18-99" aria-hidden="true" tabindex="-1"></a> erros_df <span class="ot"><-</span> metadados[erros_idx, ] <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">erro =</span> <span class="st">"falha na extracao CNN"</span>)</span>
|
|
<span id="cb18-100"><a href="#cb18-100" aria-hidden="true" tabindex="-1"></a> arquivo_erros <span class="ot"><-</span> <span class="fu">file.path</span>(pasta_saida, <span class="st">"erros_processamento_rgb_completo.csv"</span>)</span>
|
|
<span id="cb18-101"><a href="#cb18-101" aria-hidden="true" tabindex="-1"></a> <span class="fu">write_csv</span>(erros_df, arquivo_erros)</span>
|
|
<span id="cb18-102"><a href="#cb18-102" 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-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>
|
|
<span id="cb18-105"><a href="#cb18-105" aria-hidden="true" tabindex="-1"></a> <span class="fu">saveRDS</span>(dados, arquivo_cache_features)</span>
|
|
<span id="cb18-106"><a href="#cb18-106" 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-107"><a href="#cb18-107" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb18-108"><a href="#cb18-108" 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-109"><a href="#cb18-109" 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/completo/features_rgb_mobilenetv2_completo.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: 1248 </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>
|
|
</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>
|
|
<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><span class="co"># ============================================================</span></span>
|
|
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 8. Separacao treino/teste</span></span>
|
|
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
|
|
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb24-7"><a href="#cb24-7" aria-hidden="true" tabindex="-1"></a>idx_treino <span class="ot"><-</span> <span class="fu">createDataPartition</span>(</span>
|
|
<span id="cb24-8"><a href="#cb24-8" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> dados<span class="sc">$</span>classe,</span>
|
|
<span id="cb24-9"><a href="#cb24-9" aria-hidden="true" tabindex="-1"></a> <span class="at">p =</span> <span class="fl">0.70</span>,</span>
|
|
<span id="cb24-10"><a href="#cb24-10" aria-hidden="true" tabindex="-1"></a> <span class="at">list =</span> <span class="cn">FALSE</span></span>
|
|
<span id="cb24-11"><a href="#cb24-11" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb24-12"><a href="#cb24-12" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb24-13"><a href="#cb24-13" aria-hidden="true" tabindex="-1"></a>treino <span class="ot"><-</span> dados[idx_treino, ]</span>
|
|
<span id="cb24-14"><a href="#cb24-14" aria-hidden="true" tabindex="-1"></a>teste <span class="ot"><-</span> dados[<span class="sc">-</span>idx_treino, ]</span>
|
|
<span id="cb24-15"><a href="#cb24-15" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb24-16"><a href="#cb24-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: 884 </code></pre>
|
|
</div>
|
|
<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="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: 364 </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>treino <span class="sc">%>%</span></span>
|
|
<span id="cb28-2"><a href="#cb28-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">%>%</span></span>
|
|
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">full_join</span>(</span>
|
|
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> teste <span class="sc">%>%</span> <span class="fu">count</span>(classe, <span class="at">name =</span> <span class="st">"teste"</span>),</span>
|
|
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="st">"classe"</span></span>
|
|
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span></span>
|
|
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(classe) <span class="sc">%>%</span></span>
|
|
<span id="cb28-8"><a href="#cb28-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;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C2</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C3</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C4</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C5</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C6</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C7</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C8</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C9</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C10</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C11</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">C12</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">C13</td>
|
|
<td style="text-align: right;">68</td>
|
|
<td style="text-align: right;">28</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>
|
|
<div class="cell">
|
|
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 9. Preparar matrizes X e vetor y</span></span>
|
|
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a>colunas_nao_preditoras <span class="ot"><-</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="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a>x_treino <span class="ot"><-</span> treino <span class="sc">%>%</span></span>
|
|
<span id="cb29-8"><a href="#cb29-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="cb29-9"><a href="#cb29-9" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-10"><a href="#cb29-10" aria-hidden="true" tabindex="-1"></a>x_teste <span class="ot"><-</span> teste <span class="sc">%>%</span></span>
|
|
<span id="cb29-11"><a href="#cb29-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="cb29-12"><a href="#cb29-12" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-13"><a href="#cb29-13" aria-hidden="true" tabindex="-1"></a>y_treino <span class="ot"><-</span> <span class="fu">droplevels</span>(treino<span class="sc">$</span>classe)</span>
|
|
<span id="cb29-14"><a href="#cb29-14" aria-hidden="true" tabindex="-1"></a>y_teste <span class="ot"><-</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="cb29-15"><a href="#cb29-15" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-16"><a href="#cb29-16" aria-hidden="true" tabindex="-1"></a>variaveis_nzv <span class="ot"><-</span> <span class="fu">nearZeroVar</span>(x_treino)</span>
|
|
<span id="cb29-17"><a href="#cb29-17" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-18"><a href="#cb29-18" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">length</span>(variaveis_nzv) <span class="sc">></span> <span class="dv">0</span>) {</span>
|
|
<span id="cb29-19"><a href="#cb29-19" aria-hidden="true" tabindex="-1"></a> x_treino <span class="ot"><-</span> x_treino[, <span class="sc">-</span>variaveis_nzv, drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
|
|
<span id="cb29-20"><a href="#cb29-20" aria-hidden="true" tabindex="-1"></a> x_teste <span class="ot"><-</span> x_teste[, <span class="fu">colnames</span>(x_treino), drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
|
|
<span id="cb29-21"><a href="#cb29-21" aria-hidden="true" tabindex="-1"></a>}</span>
|
|
<span id="cb29-22"><a href="#cb29-22" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-23"><a href="#cb29-23" aria-hidden="true" tabindex="-1"></a>preproc <span class="ot"><-</span> <span class="fu">preProcess</span>(</span>
|
|
<span id="cb29-24"><a href="#cb29-24" aria-hidden="true" tabindex="-1"></a> x_treino,</span>
|
|
<span id="cb29-25"><a href="#cb29-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="cb29-26"><a href="#cb29-26" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb29-27"><a href="#cb29-27" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-28"><a href="#cb29-28" aria-hidden="true" tabindex="-1"></a>x_treino_norm <span class="ot"><-</span> <span class="fu">predict</span>(preproc, x_treino)</span>
|
|
<span id="cb29-29"><a href="#cb29-29" aria-hidden="true" tabindex="-1"></a>x_teste_norm <span class="ot"><-</span> <span class="fu">predict</span>(preproc, x_teste)</span>
|
|
<span id="cb29-30"><a href="#cb29-30" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb29-31"><a href="#cb29-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: 1262 </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>
|
|
<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"># 10. SVM linear</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><span class="fu">set.seed</span>(<span class="dv">123</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>modelo_svm_linear <span class="ot"><-</span> <span class="fu">svm</span>(</span>
|
|
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_norm,</span>
|
|
<span id="cb31-9"><a href="#cb31-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
|
|
<span id="cb31-10"><a href="#cb31-10" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"linear"</span>,</span>
|
|
<span id="cb31-11"><a href="#cb31-11" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> <span class="dv">1</span>,</span>
|
|
<span id="cb31-12"><a href="#cb31-12" aria-hidden="true" tabindex="-1"></a> <span class="at">scale =</span> <span class="cn">FALSE</span></span>
|
|
<span id="cb31-13"><a href="#cb31-13" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb31-14"><a href="#cb31-14" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb31-15"><a href="#cb31-15" aria-hidden="true" tabindex="-1"></a>pred_svm_linear <span class="ot"><-</span> <span class="fu">predict</span>(modelo_svm_linear, x_teste_norm)</span>
|
|
<span id="cb31-16"><a href="#cb31-16" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb31-17"><a href="#cb31-17" aria-hidden="true" tabindex="-1"></a>cm_svm_linear <span class="ot"><-</span> <span class="fu">confusionMatrix</span>(pred_svm_linear, y_teste)</span>
|
|
<span id="cb31-18"><a href="#cb31-18" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb31-19"><a href="#cb31-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 12 4 1 1 1 3 1 0 8 1 1 1 1
|
|
C2 9 14 0 0 0 5 0 0 0 0 0 0 1
|
|
C3 0 1 10 1 0 0 0 2 4 0 0 0 5
|
|
C4 2 1 4 23 0 0 1 0 0 0 0 0 9
|
|
C5 0 0 5 0 21 0 0 0 0 0 0 0 2
|
|
C6 1 7 0 0 0 11 1 7 0 2 0 4 1
|
|
C7 0 0 2 1 0 1 11 7 0 0 0 8 2
|
|
C8 0 0 0 0 1 4 5 7 0 1 1 5 0
|
|
C9 2 1 3 0 0 1 0 1 13 0 0 3 1
|
|
C10 1 0 0 0 0 0 0 0 2 1 22 0 0
|
|
C11 1 0 1 1 0 1 0 0 1 22 3 0 0
|
|
C12 0 0 0 0 0 0 7 3 0 0 0 5 1
|
|
C13 0 0 2 1 5 2 2 1 0 1 1 2 5
|
|
|
|
Overall Statistics
|
|
|
|
Accuracy : 0.3736
|
|
95% CI : (0.3238, 0.4256)
|
|
No Information Rate : 0.0769
|
|
P-Value [Acc > NIR] : < 2.2e-16
|
|
|
|
Kappa : 0.3214
|
|
|
|
Mcnemar's Test P-Value : NA
|
|
|
|
Statistics by Class:
|
|
|
|
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
|
|
Sensitivity 0.42857 0.50000 0.35714 0.82143 0.75000
|
|
Specificity 0.93155 0.95536 0.96131 0.94940 0.97917
|
|
Pos Pred Value 0.34286 0.48276 0.43478 0.57500 0.75000
|
|
Neg Pred Value 0.95137 0.95821 0.94721 0.98457 0.97917
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
|
|
Detection Rate 0.03297 0.03846 0.02747 0.06319 0.05769
|
|
Detection Prevalence 0.09615 0.07967 0.06319 0.10989 0.07692
|
|
Balanced Accuracy 0.68006 0.72768 0.65923 0.88542 0.86458
|
|
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
|
|
Sensitivity 0.39286 0.39286 0.25000 0.46429 0.035714
|
|
Specificity 0.93155 0.93750 0.94940 0.96429 0.925595
|
|
Pos Pred Value 0.32353 0.34375 0.29167 0.52000 0.038462
|
|
Neg Pred Value 0.94848 0.94880 0.93824 0.95575 0.920118
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.076923
|
|
Detection Rate 0.03022 0.03022 0.01923 0.03571 0.002747
|
|
Detection Prevalence 0.09341 0.08791 0.06593 0.06868 0.071429
|
|
Balanced Accuracy 0.66220 0.66518 0.59970 0.71429 0.480655
|
|
Class: C11 Class: C12 Class: C13
|
|
Sensitivity 0.107143 0.17857 0.17857
|
|
Specificity 0.919643 0.96726 0.94940
|
|
Pos Pred Value 0.100000 0.31250 0.22727
|
|
Neg Pred Value 0.925150 0.93391 0.93275
|
|
Prevalence 0.076923 0.07692 0.07692
|
|
Detection Rate 0.008242 0.01374 0.01374
|
|
Detection Prevalence 0.082418 0.04396 0.06044
|
|
Balanced Accuracy 0.513393 0.57292 0.56399</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>
|
|
<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"># 11. SVM radial com parametros fixos</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_radial <span class="ot"><-</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">"radial"</span>,</span>
|
|
<span id="cb33-11"><a href="#cb33-11" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> <span class="dv">10</span>,</span>
|
|
<span id="cb33-12"><a href="#cb33-12" aria-hidden="true" tabindex="-1"></a> <span class="at">gamma =</span> <span class="fl">0.01</span>,</span>
|
|
<span id="cb33-13"><a href="#cb33-13" aria-hidden="true" tabindex="-1"></a> <span class="at">scale =</span> <span class="cn">FALSE</span></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></span>
|
|
<span id="cb33-16"><a href="#cb33-16" aria-hidden="true" tabindex="-1"></a>pred_svm_radial <span class="ot"><-</span> <span class="fu">predict</span>(modelo_svm_radial, x_teste_norm)</span>
|
|
<span id="cb33-17"><a href="#cb33-17" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb33-18"><a href="#cb33-18" aria-hidden="true" tabindex="-1"></a>cm_svm_radial <span class="ot"><-</span> <span class="fu">confusionMatrix</span>(pred_svm_radial, y_teste)</span>
|
|
<span id="cb33-19"><a href="#cb33-19" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb33-20"><a href="#cb33-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 4 4 0 0 0 2 0 0 5 2 2 0 0
|
|
C2 7 2 0 0 0 5 0 0 0 0 0 0 1
|
|
C3 0 0 2 1 0 0 0 2 4 0 0 0 2
|
|
C4 1 0 4 5 0 0 1 0 0 0 0 0 3
|
|
C5 0 0 3 0 8 0 0 0 0 0 0 0 2
|
|
C6 1 6 0 0 0 8 0 6 0 2 0 1 1
|
|
C7 0 0 0 0 0 1 0 4 0 0 0 6 1
|
|
C8 0 0 0 0 0 1 6 2 0 1 1 6 0
|
|
C9 0 0 3 0 0 0 2 3 4 0 0 2 0
|
|
C10 1 0 0 0 0 0 0 0 0 1 22 0 0
|
|
C11 1 0 1 0 0 1 0 0 1 19 0 0 0
|
|
C12 0 0 0 0 0 1 6 3 0 0 0 1 1
|
|
C13 13 16 15 22 20 9 13 8 14 3 3 12 17
|
|
|
|
Overall Statistics
|
|
|
|
Accuracy : 0.1484
|
|
95% CI : (0.1135, 0.1891)
|
|
No Information Rate : 0.0769
|
|
P-Value [Acc > NIR] : 3.007e-06
|
|
|
|
Kappa : 0.0774
|
|
|
|
Mcnemar's Test P-Value : NA
|
|
|
|
Statistics by Class:
|
|
|
|
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
|
|
Sensitivity 0.14286 0.071429 0.071429 0.17857 0.28571
|
|
Specificity 0.95536 0.961310 0.973214 0.97321 0.98512
|
|
Pos Pred Value 0.21053 0.133333 0.181818 0.35714 0.61538
|
|
Neg Pred Value 0.93043 0.925501 0.926346 0.93429 0.94302
|
|
Prevalence 0.07692 0.076923 0.076923 0.07692 0.07692
|
|
Detection Rate 0.01099 0.005495 0.005495 0.01374 0.02198
|
|
Detection Prevalence 0.05220 0.041209 0.030220 0.03846 0.03571
|
|
Balanced Accuracy 0.54911 0.516369 0.522321 0.57589 0.63542
|
|
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
|
|
Sensitivity 0.28571 0.00000 0.071429 0.14286 0.035714
|
|
Specificity 0.94940 0.96429 0.955357 0.97024 0.931548
|
|
Pos Pred Value 0.32000 0.00000 0.117647 0.28571 0.041667
|
|
Neg Pred Value 0.94100 0.92045 0.925072 0.93143 0.920588
|
|
Prevalence 0.07692 0.07692 0.076923 0.07692 0.076923
|
|
Detection Rate 0.02198 0.00000 0.005495 0.01099 0.002747
|
|
Detection Prevalence 0.06868 0.03297 0.046703 0.03846 0.065934
|
|
Balanced Accuracy 0.61756 0.48214 0.513393 0.55655 0.483631
|
|
Class: C11 Class: C12 Class: C13
|
|
Sensitivity 0.00000 0.035714 0.60714
|
|
Specificity 0.93155 0.967262 0.55952
|
|
Pos Pred Value 0.00000 0.083333 0.10303
|
|
Neg Pred Value 0.91789 0.923295 0.94472
|
|
Prevalence 0.07692 0.076923 0.07692
|
|
Detection Rate 0.00000 0.002747 0.04670
|
|
Detection Prevalence 0.06319 0.032967 0.45330
|
|
Balanced Accuracy 0.46577 0.501488 0.58333</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>
|
|
<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"># 12. Ajuste simples de hiperparametros do SVM radial</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>k_cv <span class="ot"><-</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="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (k_cv <span class="sc">>=</span> <span class="dv">2</span>) {</span>
|
|
<span id="cb35-10"><a href="#cb35-10" aria-hidden="true" tabindex="-1"></a> ajuste_svm_radial <span class="ot"><-</span> <span class="fu">tune.svm</span>(</span>
|
|
<span id="cb35-11"><a href="#cb35-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_norm,</span>
|
|
<span id="cb35-12"><a href="#cb35-12" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
|
|
<span id="cb35-13"><a href="#cb35-13" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>,</span>
|
|
<span id="cb35-14"><a href="#cb35-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="cb35-15"><a href="#cb35-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="cb35-16"><a href="#cb35-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="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></span>
|
|
<span id="cb35-19"><a href="#cb35-19" aria-hidden="true" tabindex="-1"></a> modelo_svm_radial_ajustado <span class="ot"><-</span> ajuste_svm_radial<span class="sc">$</span>best.model</span>
|
|
<span id="cb35-20"><a href="#cb35-20" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb35-21"><a href="#cb35-21" aria-hidden="true" tabindex="-1"></a> pred_svm_radial_ajustado <span class="ot"><-</span> <span class="fu">predict</span>(</span>
|
|
<span id="cb35-22"><a href="#cb35-22" aria-hidden="true" tabindex="-1"></a> modelo_svm_radial_ajustado,</span>
|
|
<span id="cb35-23"><a href="#cb35-23" aria-hidden="true" tabindex="-1"></a> x_teste_norm</span>
|
|
<span id="cb35-24"><a href="#cb35-24" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb35-25"><a href="#cb35-25" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb35-26"><a href="#cb35-26" aria-hidden="true" tabindex="-1"></a> cm_svm_radial_ajustado <span class="ot"><-</span> <span class="fu">confusionMatrix</span>(</span>
|
|
<span id="cb35-27"><a href="#cb35-27" aria-hidden="true" tabindex="-1"></a> pred_svm_radial_ajustado,</span>
|
|
<span id="cb35-28"><a href="#cb35-28" aria-hidden="true" tabindex="-1"></a> y_teste</span>
|
|
<span id="cb35-29"><a href="#cb35-29" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb35-30"><a href="#cb35-30" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb35-31"><a href="#cb35-31" aria-hidden="true" tabindex="-1"></a> ajuste_svm_radial<span class="sc">$</span>best.parameters <span class="sc">%>%</span></span>
|
|
<span id="cb35-32"><a href="#cb35-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>()</span>
|
|
<span id="cb35-33"><a href="#cb35-33" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
|
|
<span id="cb35-34"><a href="#cb35-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">warning</span>(<span class="st">"Poucas amostras por classe para validacao cruzada."</span>)</span>
|
|
<span id="cb35-35"><a href="#cb35-35" aria-hidden="true" tabindex="-1"></a> ajuste_svm_radial <span class="ot"><-</span> <span class="cn">NULL</span></span>
|
|
<span id="cb35-36"><a href="#cb35-36" aria-hidden="true" tabindex="-1"></a> modelo_svm_radial_ajustado <span class="ot"><-</span> modelo_svm_radial</span>
|
|
<span id="cb35-37"><a href="#cb35-37" aria-hidden="true" tabindex="-1"></a> pred_svm_radial_ajustado <span class="ot"><-</span> pred_svm_radial</span>
|
|
<span id="cb35-38"><a href="#cb35-38" aria-hidden="true" tabindex="-1"></a> cm_svm_radial_ajustado <span class="ot"><-</span> cm_svm_radial</span>
|
|
<span id="cb35-39"><a href="#cb35-39" 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;">9</td>
|
|
<td style="text-align: right;">0.001</td>
|
|
<td style="text-align: right;">10</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<div class="cell">
|
|
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-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 12 4 0 2 0 3 0 0 8 1 1 1 1
|
|
C2 9 14 0 0 0 5 0 0 0 0 0 0 1
|
|
C3 0 1 10 1 0 0 0 2 4 0 0 0 3
|
|
C4 2 1 5 20 0 0 2 0 0 0 0 0 8
|
|
C5 0 0 5 0 18 0 0 0 0 0 0 0 1
|
|
C6 1 7 0 0 0 10 0 8 0 1 0 4 2
|
|
C7 0 0 1 2 0 1 9 5 0 0 0 8 2
|
|
C8 0 0 0 0 1 4 7 8 0 1 1 5 1
|
|
C9 1 1 3 0 0 2 0 1 15 0 0 2 0
|
|
C10 1 0 0 0 0 0 0 0 0 1 22 0 0
|
|
C11 2 0 1 1 0 1 0 0 1 23 3 0 0
|
|
C12 0 0 1 1 0 0 7 3 0 0 0 5 1
|
|
C13 0 0 2 1 9 2 3 1 0 1 1 3 8
|
|
|
|
Overall Statistics
|
|
|
|
Accuracy : 0.3654
|
|
95% CI : (0.3158, 0.4172)
|
|
No Information Rate : 0.0769
|
|
P-Value [Acc > NIR] : < 2.2e-16
|
|
|
|
Kappa : 0.3125
|
|
|
|
Mcnemar's Test P-Value : NA
|
|
|
|
Statistics by Class:
|
|
|
|
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
|
|
Sensitivity 0.42857 0.50000 0.35714 0.71429 0.64286
|
|
Specificity 0.93750 0.95536 0.96726 0.94643 0.98214
|
|
Pos Pred Value 0.36364 0.48276 0.47619 0.52632 0.75000
|
|
Neg Pred Value 0.95166 0.95821 0.94752 0.97546 0.97059
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
|
|
Detection Rate 0.03297 0.03846 0.02747 0.05495 0.04945
|
|
Detection Prevalence 0.09066 0.07967 0.05769 0.10440 0.06593
|
|
Balanced Accuracy 0.68304 0.72768 0.66220 0.83036 0.81250
|
|
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
|
|
Sensitivity 0.35714 0.32143 0.28571 0.53571 0.035714
|
|
Specificity 0.93155 0.94345 0.94048 0.97024 0.931548
|
|
Pos Pred Value 0.30303 0.32143 0.28571 0.60000 0.041667
|
|
Neg Pred Value 0.94562 0.94345 0.94048 0.96165 0.920588
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.076923
|
|
Detection Rate 0.02747 0.02473 0.02198 0.04121 0.002747
|
|
Detection Prevalence 0.09066 0.07692 0.07692 0.06868 0.065934
|
|
Balanced Accuracy 0.64435 0.63244 0.61310 0.75298 0.483631
|
|
Class: C11 Class: C12 Class: C13
|
|
Sensitivity 0.107143 0.17857 0.28571
|
|
Specificity 0.913690 0.96131 0.93155
|
|
Pos Pred Value 0.093750 0.27778 0.25806
|
|
Neg Pred Value 0.924699 0.93353 0.93994
|
|
Prevalence 0.076923 0.07692 0.07692
|
|
Detection Rate 0.008242 0.01374 0.02198
|
|
Detection Prevalence 0.087912 0.04945 0.08516
|
|
Balanced Accuracy 0.510417 0.56994 0.60863</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>
|
|
<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><span class="co"># ============================================================</span></span>
|
|
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 13. PCA</span></span>
|
|
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a>pca <span class="ot"><-</span> <span class="fu">prcomp</span>(</span>
|
|
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a> x_treino_norm,</span>
|
|
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a> <span class="at">center =</span> <span class="cn">FALSE</span>,</span>
|
|
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a> <span class="at">scale. =</span> <span class="cn">FALSE</span></span>
|
|
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb38-11"><a href="#cb38-11" aria-hidden="true" tabindex="-1"></a>variancia <span class="ot"><-</span> pca<span class="sc">$</span>sdev<span class="sc">^</span><span class="dv">2</span></span>
|
|
<span id="cb38-12"><a href="#cb38-12" aria-hidden="true" tabindex="-1"></a>variancia_exp <span class="ot"><-</span> variancia <span class="sc">/</span> <span class="fu">sum</span>(variancia)</span>
|
|
<span id="cb38-13"><a href="#cb38-13" aria-hidden="true" tabindex="-1"></a>variancia_acum <span class="ot"><-</span> <span class="fu">cumsum</span>(variancia_exp)</span>
|
|
<span id="cb38-14"><a href="#cb38-14" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb38-15"><a href="#cb38-15" aria-hidden="true" tabindex="-1"></a>n_comp_95 <span class="ot"><-</span> <span class="fu">which</span>(variancia_acum <span class="sc">>=</span> <span class="fl">0.95</span>)[<span class="dv">1</span>]</span>
|
|
<span id="cb38-16"><a href="#cb38-16" aria-hidden="true" tabindex="-1"></a>n_comp <span class="ot"><-</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="cb38-17"><a href="#cb38-17" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb38-18"><a href="#cb38-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: 364 </code></pre>
|
|
</div>
|
|
<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="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="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">tibble</span>(</span>
|
|
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a> <span class="at">componente =</span> <span class="fu">seq_along</span>(variancia_acum),</span>
|
|
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a> <span class="at">variancia_acumulada =</span> variancia_acum</span>
|
|
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%>%</span></span>
|
|
<span id="cb42-5"><a href="#cb42-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="cb42-6"><a href="#cb42-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="cb42-7"><a href="#cb42-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="cb42-8"><a href="#cb42-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="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
|
|
<span id="cb42-10"><a href="#cb42-10" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Variancia acumulada pelo PCA (dataset completo)"</span>,</span>
|
|
<span id="cb42-11"><a href="#cb42-11" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Numero de componentes principais"</span>,</span>
|
|
<span id="cb42-12"><a href="#cb42-12" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Variancia acumulada"</span></span>
|
|
<span id="cb42-13"><a href="#cb42-13" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
|
|
<span id="cb42-14"><a href="#cb42-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_completo_files/figure-html/unnamed-chunk-16-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="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>x_treino_pca <span class="ot"><-</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="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a>x_teste_pca <span class="ot"><-</span> <span class="fu">as.data.frame</span>(</span>
|
|
<span id="cb43-4"><a href="#cb43-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="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb43-7"><a href="#cb43-7" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
|
|
<span id="cb43-8"><a href="#cb43-8" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb43-9"><a href="#cb43-9" aria-hidden="true" tabindex="-1"></a>modelo_svm_pca <span class="ot"><-</span> <span class="fu">svm</span>(</span>
|
|
<span id="cb43-10"><a href="#cb43-10" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_pca,</span>
|
|
<span id="cb43-11"><a href="#cb43-11" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
|
|
<span id="cb43-12"><a href="#cb43-12" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>,</span>
|
|
<span id="cb43-13"><a href="#cb43-13" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> <span class="dv">10</span>,</span>
|
|
<span id="cb43-14"><a href="#cb43-14" aria-hidden="true" tabindex="-1"></a> <span class="at">gamma =</span> <span class="fl">0.01</span>,</span>
|
|
<span id="cb43-15"><a href="#cb43-15" aria-hidden="true" tabindex="-1"></a> <span class="at">scale =</span> <span class="cn">FALSE</span></span>
|
|
<span id="cb43-16"><a href="#cb43-16" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb43-17"><a href="#cb43-17" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb43-18"><a href="#cb43-18" aria-hidden="true" tabindex="-1"></a>pred_svm_pca <span class="ot"><-</span> <span class="fu">predict</span>(modelo_svm_pca, x_teste_pca)</span>
|
|
<span id="cb43-19"><a href="#cb43-19" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb43-20"><a href="#cb43-20" aria-hidden="true" tabindex="-1"></a>cm_svm_pca <span class="ot"><-</span> <span class="fu">confusionMatrix</span>(pred_svm_pca, y_teste)</span>
|
|
<span id="cb43-21"><a href="#cb43-21" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb43-22"><a href="#cb43-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 9 4 0 2 0 5 1 0 6 1 1 0 2
|
|
C2 10 10 0 0 0 5 0 0 0 0 0 0 2
|
|
C3 0 0 9 1 0 0 0 3 4 0 0 0 2
|
|
C4 2 1 5 15 0 0 1 0 0 0 0 1 6
|
|
C5 0 1 5 0 14 0 0 0 0 0 0 0 1
|
|
C6 1 6 0 0 0 9 1 7 0 1 0 1 0
|
|
C7 1 0 0 4 0 1 9 5 0 0 0 8 3
|
|
C8 0 0 0 0 0 1 6 5 0 1 0 5 1
|
|
C9 1 1 3 0 0 1 1 3 13 0 0 3 0
|
|
C10 1 0 0 0 0 0 0 0 1 3 24 0 0
|
|
C11 1 1 1 1 1 3 0 0 1 20 1 0 0
|
|
C12 1 0 0 1 0 1 6 4 0 0 0 7 2
|
|
C13 1 4 5 4 13 2 3 1 3 2 2 3 9
|
|
|
|
Overall Statistics
|
|
|
|
Accuracy : 0.3104
|
|
95% CI : (0.2632, 0.3607)
|
|
No Information Rate : 0.0769
|
|
P-Value [Acc > NIR] : < 2.2e-16
|
|
|
|
Kappa : 0.253
|
|
|
|
Mcnemar's Test P-Value : NA
|
|
|
|
Statistics by Class:
|
|
|
|
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
|
|
Sensitivity 0.32143 0.35714 0.32143 0.53571 0.50000
|
|
Specificity 0.93452 0.94940 0.97024 0.95238 0.97917
|
|
Pos Pred Value 0.29032 0.37037 0.47368 0.48387 0.66667
|
|
Neg Pred Value 0.94294 0.94659 0.94493 0.96096 0.95918
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
|
|
Detection Rate 0.02473 0.02747 0.02473 0.04121 0.03846
|
|
Detection Prevalence 0.08516 0.07418 0.05220 0.08516 0.05769
|
|
Balanced Accuracy 0.62798 0.65327 0.64583 0.74405 0.73958
|
|
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
|
|
Sensitivity 0.32143 0.32143 0.17857 0.46429 0.107143
|
|
Specificity 0.94940 0.93452 0.95833 0.96131 0.922619
|
|
Pos Pred Value 0.34615 0.29032 0.26316 0.50000 0.103448
|
|
Neg Pred Value 0.94379 0.94294 0.93333 0.95562 0.925373
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.076923
|
|
Detection Rate 0.02473 0.02473 0.01374 0.03571 0.008242
|
|
Detection Prevalence 0.07143 0.08516 0.05220 0.07143 0.079670
|
|
Balanced Accuracy 0.63542 0.62798 0.56845 0.71280 0.514881
|
|
Class: C11 Class: C12 Class: C13
|
|
Sensitivity 0.035714 0.25000 0.32143
|
|
Specificity 0.913690 0.95536 0.87202
|
|
Pos Pred Value 0.033333 0.31818 0.17308
|
|
Neg Pred Value 0.919162 0.93860 0.93910
|
|
Prevalence 0.076923 0.07692 0.07692
|
|
Detection Rate 0.002747 0.01923 0.02473
|
|
Detection Prevalence 0.082418 0.06044 0.14286
|
|
Balanced Accuracy 0.474702 0.60268 0.59673</code></pre>
|
|
</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>
|
|
<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><span class="co"># ============================================================</span></span>
|
|
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 14. Random Forest</span></span>
|
|
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb45-4"><a href="#cb45-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb45-5"><a href="#cb45-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</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>modelo_rf <span class="ot"><-</span> <span class="fu">randomForest</span>(</span>
|
|
<span id="cb45-8"><a href="#cb45-8" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_treino_norm,</span>
|
|
<span id="cb45-9"><a href="#cb45-9" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_treino,</span>
|
|
<span id="cb45-10"><a href="#cb45-10" aria-hidden="true" tabindex="-1"></a> <span class="at">ntree =</span> <span class="dv">500</span>,</span>
|
|
<span id="cb45-11"><a href="#cb45-11" aria-hidden="true" tabindex="-1"></a> <span class="at">importance =</span> <span class="cn">TRUE</span></span>
|
|
<span id="cb45-12"><a href="#cb45-12" aria-hidden="true" tabindex="-1"></a>)</span>
|
|
<span id="cb45-13"><a href="#cb45-13" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb45-14"><a href="#cb45-14" aria-hidden="true" tabindex="-1"></a>pred_rf <span class="ot"><-</span> <span class="fu">predict</span>(modelo_rf, x_teste_norm)</span>
|
|
<span id="cb45-15"><a href="#cb45-15" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb45-16"><a href="#cb45-16" aria-hidden="true" tabindex="-1"></a>cm_rf <span class="ot"><-</span> <span class="fu">confusionMatrix</span>(pred_rf, y_teste)</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>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 9 4 1 3 0 3 0 0 8 2 2 0 3
|
|
C2 9 10 0 0 0 5 0 0 0 0 0 0 1
|
|
C3 0 1 10 1 0 0 0 2 4 0 0 0 2
|
|
C4 5 2 5 18 0 0 2 0 0 0 0 0 6
|
|
C5 0 0 5 0 22 0 0 0 0 0 0 0 4
|
|
C6 1 6 0 0 0 11 0 9 0 1 0 4 2
|
|
C7 0 0 1 3 0 1 7 4 0 0 0 7 3
|
|
C8 0 1 0 0 0 1 8 7 1 1 0 4 1
|
|
C9 1 3 1 0 0 0 0 0 13 0 0 2 0
|
|
C10 1 0 2 1 0 1 0 1 0 3 24 0 0
|
|
C11 2 1 1 0 1 2 0 0 2 20 1 0 0
|
|
C12 0 0 0 1 1 2 8 4 0 0 0 8 1
|
|
C13 0 0 2 1 4 2 3 1 0 1 1 3 5
|
|
|
|
Overall Statistics
|
|
|
|
Accuracy : 0.3407
|
|
95% CI : (0.2921, 0.3919)
|
|
No Information Rate : 0.0769
|
|
P-Value [Acc > NIR] : < 2.2e-16
|
|
|
|
Kappa : 0.2857
|
|
|
|
Mcnemar's Test P-Value : NA
|
|
|
|
Statistics by Class:
|
|
|
|
Class: C1 Class: C2 Class: C3 Class: C4 Class: C5
|
|
Sensitivity 0.32143 0.35714 0.35714 0.64286 0.78571
|
|
Specificity 0.92262 0.95536 0.97024 0.94048 0.97321
|
|
Pos Pred Value 0.25714 0.40000 0.50000 0.47368 0.70968
|
|
Neg Pred Value 0.94225 0.94690 0.94767 0.96933 0.98198
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.07692
|
|
Detection Rate 0.02473 0.02747 0.02747 0.04945 0.06044
|
|
Detection Prevalence 0.09615 0.06868 0.05495 0.10440 0.08516
|
|
Balanced Accuracy 0.62202 0.65625 0.66369 0.79167 0.87946
|
|
Class: C6 Class: C7 Class: C8 Class: C9 Class: C10
|
|
Sensitivity 0.39286 0.25000 0.25000 0.46429 0.107143
|
|
Specificity 0.93155 0.94345 0.94940 0.97917 0.910714
|
|
Pos Pred Value 0.32353 0.26923 0.29167 0.65000 0.090909
|
|
Neg Pred Value 0.94848 0.93787 0.93824 0.95640 0.924471
|
|
Prevalence 0.07692 0.07692 0.07692 0.07692 0.076923
|
|
Detection Rate 0.03022 0.01923 0.01923 0.03571 0.008242
|
|
Detection Prevalence 0.09341 0.07143 0.06593 0.05495 0.090659
|
|
Balanced Accuracy 0.66220 0.59673 0.59970 0.72173 0.508929
|
|
Class: C11 Class: C12 Class: C13
|
|
Sensitivity 0.035714 0.28571 0.17857
|
|
Specificity 0.913690 0.94940 0.94643
|
|
Pos Pred Value 0.033333 0.32000 0.21739
|
|
Neg Pred Value 0.919162 0.94100 0.93255
|
|
Prevalence 0.076923 0.07692 0.07692
|
|
Detection Rate 0.002747 0.02198 0.01374
|
|
Detection Prevalence 0.082418 0.06868 0.06319
|
|
Balanced Accuracy 0.474702 0.61756 0.56250</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>
|
|
<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><span class="co"># ============================================================</span></span>
|
|
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 15. Funcao de avaliacao dos modelos</span></span>
|
|
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a>avaliar_modelo <span class="ot"><-</span> <span class="cf">function</span>(nome, matriz_confusao) {</span>
|
|
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a> overall <span class="ot"><-</span> matriz_confusao<span class="sc">$</span>overall</span>
|
|
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a> by_class <span class="ot"><-</span> matriz_confusao<span class="sc">$</span>byClass</span>
|
|
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">is.matrix</span>(by_class)) {</span>
|
|
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a> sensibilidade_macro <span class="ot"><-</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="cb47-11"><a href="#cb47-11" aria-hidden="true" tabindex="-1"></a> especificidade_macro <span class="ot"><-</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="cb47-12"><a href="#cb47-12" aria-hidden="true" tabindex="-1"></a> f1_macro <span class="ot"><-</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="cb47-13"><a href="#cb47-13" aria-hidden="true" tabindex="-1"></a> } <span class="cf">else</span> {</span>
|
|
<span id="cb47-14"><a href="#cb47-14" aria-hidden="true" tabindex="-1"></a> sensibilidade_macro <span class="ot"><-</span> by_class[<span class="st">"Sensitivity"</span>]</span>
|
|
<span id="cb47-15"><a href="#cb47-15" aria-hidden="true" tabindex="-1"></a> especificidade_macro <span class="ot"><-</span> by_class[<span class="st">"Specificity"</span>]</span>
|
|
<span id="cb47-16"><a href="#cb47-16" aria-hidden="true" tabindex="-1"></a> f1_macro <span class="ot"><-</span> by_class[<span class="st">"F1"</span>]</span>
|
|
<span id="cb47-17"><a href="#cb47-17" aria-hidden="true" tabindex="-1"></a> }</span>
|
|
<span id="cb47-18"><a href="#cb47-18" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb47-19"><a href="#cb47-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">tibble</span>(</span>
|
|
<span id="cb47-20"><a href="#cb47-20" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo =</span> nome,</span>
|
|
<span id="cb47-21"><a href="#cb47-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="cb47-22"><a href="#cb47-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="cb47-23"><a href="#cb47-23" aria-hidden="true" tabindex="-1"></a> <span class="at">sensibilidade_macro =</span> <span class="fu">as.numeric</span>(sensibilidade_macro),</span>
|
|
<span id="cb47-24"><a href="#cb47-24" aria-hidden="true" tabindex="-1"></a> <span class="at">especificidade_macro =</span> <span class="fu">as.numeric</span>(especificidade_macro),</span>
|
|
<span id="cb47-25"><a href="#cb47-25" aria-hidden="true" tabindex="-1"></a> <span class="at">f1_macro =</span> <span class="fu">as.numeric</span>(f1_macro)</span>
|
|
<span id="cb47-26"><a href="#cb47-26" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb47-27"><a href="#cb47-27" aria-hidden="true" tabindex="-1"></a>}</span>
|
|
<span id="cb47-28"><a href="#cb47-28" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb47-29"><a href="#cb47-29" aria-hidden="true" tabindex="-1"></a>resultados <span class="ot"><-</span> <span class="fu">bind_rows</span>(</span>
|
|
<span id="cb47-30"><a href="#cb47-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="cb47-31"><a href="#cb47-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="cb47-32"><a href="#cb47-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="cb47-33"><a href="#cb47-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="cb47-34"><a href="#cb47-34" aria-hidden="true" tabindex="-1"></a> <span class="fu">avaliar_modelo</span>(<span class="st">"Random Forest"</span>, cm_rf)</span>
|
|
<span id="cb47-35"><a href="#cb47-35" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%>%</span></span>
|
|
<span id="cb47-36"><a href="#cb47-36" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(acuracia))</span>
|
|
<span id="cb47-37"><a href="#cb47-37" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb47-38"><a href="#cb47-38" aria-hidden="true" tabindex="-1"></a>resultados <span class="sc">%>%</span></span>
|
|
<span id="cb47-39"><a href="#cb47-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">%>%</span></span>
|
|
<span id="cb47-40"><a href="#cb47-40" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>(<span class="at">caption =</span> <span class="st">"Resultados - Dataset Completo (Holdout 70/30)"</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>Resultados - Dataset Completo (Holdout 70/30)</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.3736</td>
|
|
<td style="text-align: right;">0.3214</td>
|
|
<td style="text-align: right;">0.3736</td>
|
|
<td style="text-align: right;">0.9478</td>
|
|
<td style="text-align: right;">0.3646</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">SVM radial ajustado</td>
|
|
<td style="text-align: right;">0.3654</td>
|
|
<td style="text-align: right;">0.3125</td>
|
|
<td style="text-align: right;">0.3654</td>
|
|
<td style="text-align: right;">0.9471</td>
|
|
<td style="text-align: right;">0.3630</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">Random Forest</td>
|
|
<td style="text-align: right;">0.3407</td>
|
|
<td style="text-align: right;">0.2857</td>
|
|
<td style="text-align: right;">0.3407</td>
|
|
<td style="text-align: right;">0.9451</td>
|
|
<td style="text-align: right;">0.3405</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">PCA + SVM radial</td>
|
|
<td style="text-align: right;">0.3104</td>
|
|
<td style="text-align: right;">0.2530</td>
|
|
<td style="text-align: right;">0.3104</td>
|
|
<td style="text-align: right;">0.9425</td>
|
|
<td style="text-align: right;">0.3161</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">SVM radial</td>
|
|
<td style="text-align: right;">0.1484</td>
|
|
<td style="text-align: right;">0.0774</td>
|
|
<td style="text-align: right;">0.1484</td>
|
|
<td style="text-align: right;">0.9290</td>
|
|
<td style="text-align: right;">0.1673</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<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="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="cb48-2"><a href="#cb48-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="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
|
|
<span id="cb48-4"><a href="#cb48-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
|
|
<span id="cb48-5"><a href="#cb48-5" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Comparacao da acuracia dos modelos (dataset completo)"</span>,</span>
|
|
<span id="cb48-6"><a href="#cb48-6" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Modelo"</span>,</span>
|
|
<span id="cb48-7"><a href="#cb48-7" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Acuracia"</span></span>
|
|
<span id="cb48-8"><a href="#cb48-8" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
|
|
<span id="cb48-9"><a href="#cb48-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_completo_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
|
|
</figure>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<section id="importancia-das-variaveis" class="level1" data-number="16">
|
|
<h1 data-number="16"><span class="header-section-number">16</span> Importancia das variaveis</h1>
|
|
<div class="cell">
|
|
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb49-2"><a href="#cb49-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 16. Importancia das variaveis</span></span>
|
|
<span id="cb49-3"><a href="#cb49-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb49-4"><a href="#cb49-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb49-5"><a href="#cb49-5" aria-hidden="true" tabindex="-1"></a>importancia <span class="ot"><-</span> <span class="fu">importance</span>(modelo_rf)</span>
|
|
<span id="cb49-6"><a href="#cb49-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb49-7"><a href="#cb49-7" aria-hidden="true" tabindex="-1"></a>coluna_importancia <span class="ot"><-</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="cb49-8"><a href="#cb49-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"MeanDecreaseGini"</span></span>
|
|
<span id="cb49-9"><a href="#cb49-9" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
|
|
<span id="cb49-10"><a href="#cb49-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">colnames</span>(importancia)[<span class="dv">1</span>]</span>
|
|
<span id="cb49-11"><a href="#cb49-11" aria-hidden="true" tabindex="-1"></a>}</span>
|
|
<span id="cb49-12"><a href="#cb49-12" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb49-13"><a href="#cb49-13" aria-hidden="true" tabindex="-1"></a>importancia_df <span class="ot"><-</span> <span class="fu">tibble</span>(</span>
|
|
<span id="cb49-14"><a href="#cb49-14" aria-hidden="true" tabindex="-1"></a> <span class="at">variavel =</span> <span class="fu">rownames</span>(importancia),</span>
|
|
<span id="cb49-15"><a href="#cb49-15" aria-hidden="true" tabindex="-1"></a> <span class="at">importancia =</span> importancia[, coluna_importancia]</span>
|
|
<span id="cb49-16"><a href="#cb49-16" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%>%</span></span>
|
|
<span id="cb49-17"><a href="#cb49-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(importancia))</span>
|
|
<span id="cb49-18"><a href="#cb49-18" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb49-19"><a href="#cb49-19" aria-hidden="true" tabindex="-1"></a>importancia_df <span class="sc">%>%</span></span>
|
|
<span id="cb49-20"><a href="#cb49-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">%>%</span></span>
|
|
<span id="cb49-21"><a href="#cb49-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;">2.569848</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_1003</td>
|
|
<td style="text-align: right;">1.884104</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0321</td>
|
|
<td style="text-align: right;">1.852070</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0850</td>
|
|
<td style="text-align: right;">1.828746</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_1091</td>
|
|
<td style="text-align: right;">1.736351</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0476</td>
|
|
<td style="text-align: right;">1.654010</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0689</td>
|
|
<td style="text-align: right;">1.572648</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0914</td>
|
|
<td style="text-align: right;">1.548601</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0562</td>
|
|
<td style="text-align: right;">1.521096</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0260</td>
|
|
<td style="text-align: right;">1.492961</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0971</td>
|
|
<td style="text-align: right;">1.416875</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_1262</td>
|
|
<td style="text-align: right;">1.345804</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0778</td>
|
|
<td style="text-align: right;">1.340887</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0473</td>
|
|
<td style="text-align: right;">1.325315</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0065</td>
|
|
<td style="text-align: right;">1.320592</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0231</td>
|
|
<td style="text-align: right;">1.298018</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0780</td>
|
|
<td style="text-align: right;">1.296183</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0278</td>
|
|
<td style="text-align: right;">1.287194</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">feat_0190</td>
|
|
<td style="text-align: right;">1.277953</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">feat_0094</td>
|
|
<td style="text-align: right;">1.270837</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<section id="salvamento-dos-resultados" class="level1" data-number="17">
|
|
<h1 data-number="17"><span class="header-section-number">17</span> Salvamento dos resultados</h1>
|
|
<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"># 17. Salvar saidas (nomes diferentes do original)</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><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="cb50-6"><a href="#cb50-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb50-7"><a href="#cb50-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_completo.csv"</span>))</span>
|
|
<span id="cb50-8"><a href="#cb50-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_completo.csv"</span>))</span>
|
|
<span id="cb50-9"><a href="#cb50-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_completo.csv"</span>))</span>
|
|
<span id="cb50-10"><a href="#cb50-10" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb50-11"><a href="#cb50-11" aria-hidden="true" tabindex="-1"></a><span class="fu">saveRDS</span>(</span>
|
|
<span id="cb50-12"><a href="#cb50-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">list</span>(</span>
|
|
<span id="cb50-13"><a href="#cb50-13" aria-hidden="true" tabindex="-1"></a> <span class="at">preproc =</span> preproc,</span>
|
|
<span id="cb50-14"><a href="#cb50-14" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_linear =</span> modelo_svm_linear,</span>
|
|
<span id="cb50-15"><a href="#cb50-15" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_radial =</span> modelo_svm_radial,</span>
|
|
<span id="cb50-16"><a href="#cb50-16" aria-hidden="true" tabindex="-1"></a> <span class="at">ajuste_svm_radial =</span> ajuste_svm_radial,</span>
|
|
<span id="cb50-17"><a href="#cb50-17" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_radial_ajustado =</span> modelo_svm_radial_ajustado,</span>
|
|
<span id="cb50-18"><a href="#cb50-18" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_svm_pca =</span> modelo_svm_pca,</span>
|
|
<span id="cb50-19"><a href="#cb50-19" aria-hidden="true" tabindex="-1"></a> <span class="at">modelo_rf =</span> modelo_rf,</span>
|
|
<span id="cb50-20"><a href="#cb50-20" aria-hidden="true" tabindex="-1"></a> <span class="at">pca =</span> pca,</span>
|
|
<span id="cb50-21"><a href="#cb50-21" aria-hidden="true" tabindex="-1"></a> <span class="at">resultados =</span> resultados,</span>
|
|
<span id="cb50-22"><a href="#cb50-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="cb50-23"><a href="#cb50-23" aria-hidden="true" tabindex="-1"></a> ),</span>
|
|
<span id="cb50-24"><a href="#cb50-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">file.path</span>(pasta_saida, <span class="st">"modelos_rgb_completo.rds"</span>)</span>
|
|
<span id="cb50-25"><a href="#cb50-25" aria-hidden="true" tabindex="-1"></a>)</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 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/completo </code></pre>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<section id="validacao-cruzada-k-fold" class="level1" data-number="18">
|
|
<h1 data-number="18"><span class="header-section-number">18</span> Validacao Cruzada K-Fold</h1>
|
|
<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><span class="co"># ============================================================</span></span>
|
|
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 18. Validacao cruzada k-fold (k=5, estratificada)</span></span>
|
|
<span id="cb52-3"><a href="#cb52-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb52-4"><a href="#cb52-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-5"><a href="#cb52-5" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</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>k_folds <span class="ot"><-</span> <span class="dv">5</span>L</span>
|
|
<span id="cb52-8"><a href="#cb52-8" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-9"><a href="#cb52-9" aria-hidden="true" tabindex="-1"></a>folds <span class="ot"><-</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="cb52-10"><a href="#cb52-10" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-11"><a href="#cb52-11" aria-hidden="true" tabindex="-1"></a>grade_cost_kfold <span class="ot"><-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">100</span>)</span>
|
|
<span id="cb52-12"><a href="#cb52-12" aria-hidden="true" tabindex="-1"></a>grade_gamma_kfold <span class="ot"><-</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="cb52-13"><a href="#cb52-13" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-14"><a href="#cb52-14" aria-hidden="true" tabindex="-1"></a>metricas_folds <span class="ot"><-</span> <span class="fu">vector</span>(<span class="st">"list"</span>, k_folds)</span>
|
|
<span id="cb52-15"><a href="#cb52-15" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-16"><a href="#cb52-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="cb52-17"><a href="#cb52-17" aria-hidden="true" tabindex="-1"></a> idx_teste <span class="ot"><-</span> folds[[fold_i]]</span>
|
|
<span id="cb52-18"><a href="#cb52-18" aria-hidden="true" tabindex="-1"></a> idx_treino <span class="ot"><-</span> <span class="fu">setdiff</span>(<span class="fu">seq_len</span>(<span class="fu">nrow</span>(dados)), idx_teste)</span>
|
|
<span id="cb52-19"><a href="#cb52-19" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-20"><a href="#cb52-20" aria-hidden="true" tabindex="-1"></a> fold_treino <span class="ot"><-</span> dados[idx_treino, ]</span>
|
|
<span id="cb52-21"><a href="#cb52-21" aria-hidden="true" tabindex="-1"></a> fold_teste <span class="ot"><-</span> dados[idx_teste, ]</span>
|
|
<span id="cb52-22"><a href="#cb52-22" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-23"><a href="#cb52-23" aria-hidden="true" tabindex="-1"></a> x_fold_tr <span class="ot"><-</span> fold_treino <span class="sc">%>%</span> <span class="fu">select</span>(<span class="sc">-</span><span class="fu">all_of</span>(colunas_nao_preditoras))</span>
|
|
<span id="cb52-24"><a href="#cb52-24" aria-hidden="true" tabindex="-1"></a> x_fold_te <span class="ot"><-</span> fold_teste <span class="sc">%>%</span> <span class="fu">select</span>(<span class="sc">-</span><span class="fu">all_of</span>(colunas_nao_preditoras))</span>
|
|
<span id="cb52-25"><a href="#cb52-25" aria-hidden="true" tabindex="-1"></a> y_fold_tr <span class="ot"><-</span> <span class="fu">droplevels</span>(fold_treino<span class="sc">$</span>classe)</span>
|
|
<span id="cb52-26"><a href="#cb52-26" aria-hidden="true" tabindex="-1"></a> y_fold_te <span class="ot"><-</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="cb52-27"><a href="#cb52-27" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-28"><a href="#cb52-28" aria-hidden="true" tabindex="-1"></a> nzv_fold <span class="ot"><-</span> <span class="fu">nearZeroVar</span>(x_fold_tr)</span>
|
|
<span id="cb52-29"><a href="#cb52-29" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(nzv_fold) <span class="sc">></span> <span class="dv">0</span>) {</span>
|
|
<span id="cb52-30"><a href="#cb52-30" aria-hidden="true" tabindex="-1"></a> x_fold_tr <span class="ot"><-</span> x_fold_tr[, <span class="sc">-</span>nzv_fold, drop <span class="ot">=</span> <span class="cn">FALSE</span>]</span>
|
|
<span id="cb52-31"><a href="#cb52-31" aria-hidden="true" tabindex="-1"></a> x_fold_te <span class="ot"><-</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="cb52-32"><a href="#cb52-32" aria-hidden="true" tabindex="-1"></a> }</span>
|
|
<span id="cb52-33"><a href="#cb52-33" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-34"><a href="#cb52-34" aria-hidden="true" tabindex="-1"></a> pp_fold <span class="ot"><-</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="cb52-35"><a href="#cb52-35" aria-hidden="true" tabindex="-1"></a> x_fold_tr_n <span class="ot"><-</span> <span class="fu">predict</span>(pp_fold, x_fold_tr)</span>
|
|
<span id="cb52-36"><a href="#cb52-36" aria-hidden="true" tabindex="-1"></a> x_fold_te_n <span class="ot"><-</span> <span class="fu">predict</span>(pp_fold, x_fold_te)</span>
|
|
<span id="cb52-37"><a href="#cb52-37" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-38"><a href="#cb52-38" aria-hidden="true" tabindex="-1"></a> m1 <span class="ot"><-</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="cb52-39"><a href="#cb52-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="cb52-40"><a href="#cb52-40" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-41"><a href="#cb52-41" aria-hidden="true" tabindex="-1"></a> m2 <span class="ot"><-</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="cb52-42"><a href="#cb52-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="cb52-43"><a href="#cb52-43" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-44"><a href="#cb52-44" aria-hidden="true" tabindex="-1"></a> k_cv_fold <span class="ot"><-</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="cb52-45"><a href="#cb52-45" aria-hidden="true" tabindex="-1"></a> ajuste_fold <span class="ot"><-</span> <span class="fu">tune.svm</span>(</span>
|
|
<span id="cb52-46"><a href="#cb52-46" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> x_fold_tr_n,</span>
|
|
<span id="cb52-47"><a href="#cb52-47" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> y_fold_tr,</span>
|
|
<span id="cb52-48"><a href="#cb52-48" aria-hidden="true" tabindex="-1"></a> <span class="at">kernel =</span> <span class="st">"radial"</span>,</span>
|
|
<span id="cb52-49"><a href="#cb52-49" aria-hidden="true" tabindex="-1"></a> <span class="at">cost =</span> grade_cost_kfold,</span>
|
|
<span id="cb52-50"><a href="#cb52-50" aria-hidden="true" tabindex="-1"></a> <span class="at">gamma =</span> grade_gamma_kfold,</span>
|
|
<span id="cb52-51"><a href="#cb52-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="cb52-52"><a href="#cb52-52" aria-hidden="true" tabindex="-1"></a> )</span>
|
|
<span id="cb52-53"><a href="#cb52-53" aria-hidden="true" tabindex="-1"></a> m3 <span class="ot"><-</span> ajuste_fold<span class="sc">$</span>best.model</span>
|
|
<span id="cb52-54"><a href="#cb52-54" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-55"><a href="#cb52-55" aria-hidden="true" tabindex="-1"></a> pca_fold <span class="ot"><-</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="cb52-56"><a href="#cb52-56" aria-hidden="true" tabindex="-1"></a> var_acum_f <span class="ot"><-</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="cb52-57"><a href="#cb52-57" aria-hidden="true" tabindex="-1"></a> n_comp_f <span class="ot"><-</span> <span class="fu">min</span>(<span class="fu">which</span>(var_acum_f <span class="sc">>=</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="cb52-58"><a href="#cb52-58" aria-hidden="true" tabindex="-1"></a> x_pca_tr <span class="ot"><-</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="cb52-59"><a href="#cb52-59" aria-hidden="true" tabindex="-1"></a> x_pca_te <span class="ot"><-</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="cb52-60"><a href="#cb52-60" aria-hidden="true" tabindex="-1"></a> m4 <span class="ot"><-</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="cb52-61"><a href="#cb52-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="cb52-62"><a href="#cb52-62" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-63"><a href="#cb52-63" aria-hidden="true" tabindex="-1"></a> m5 <span class="ot"><-</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="cb52-64"><a href="#cb52-64" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-65"><a href="#cb52-65" aria-hidden="true" tabindex="-1"></a> metricas_folds[[fold_i]] <span class="ot"><-</span> <span class="fu">bind_rows</span>(</span>
|
|
<span id="cb52-66"><a href="#cb52-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="cb52-67"><a href="#cb52-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="cb52-68"><a href="#cb52-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="cb52-69"><a href="#cb52-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="cb52-70"><a href="#cb52-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="cb52-71"><a href="#cb52-71" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">fold =</span> fold_i)</span>
|
|
<span id="cb52-72"><a href="#cb52-72" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb52-73"><a href="#cb52-73" aria-hidden="true" tabindex="-1"></a> <span class="fu">cat</span>(<span class="fu">sprintf</span>(</span>
|
|
<span id="cb52-74"><a href="#cb52-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="cb52-75"><a href="#cb52-75" aria-hidden="true" tabindex="-1"></a> fold_i, k_folds,</span>
|
|
<span id="cb52-76"><a href="#cb52-76" aria-hidden="true" tabindex="-1"></a> ajuste_fold<span class="sc">$</span>best.parameters<span class="sc">$</span>cost,</span>
|
|
<span id="cb52-77"><a href="#cb52-77" aria-hidden="true" tabindex="-1"></a> ajuste_fold<span class="sc">$</span>best.parameters<span class="sc">$</span>gamma</span>
|
|
<span id="cb52-78"><a href="#cb52-78" aria-hidden="true" tabindex="-1"></a> ))</span>
|
|
<span id="cb52-79"><a href="#cb52-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="cb54"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>resultados_kfold <span class="ot"><-</span> <span class="fu">bind_rows</span>(metricas_folds) <span class="sc">%>%</span></span>
|
|
<span id="cb54-2"><a href="#cb54-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(modelo) <span class="sc">%>%</span></span>
|
|
<span id="cb54-3"><a href="#cb54-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="cb54-4"><a href="#cb54-4" aria-hidden="true" tabindex="-1"></a> <span class="at">.groups =</span> <span class="st">"drop"</span>) <span class="sc">%>%</span></span>
|
|
<span id="cb54-5"><a href="#cb54-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(acuracia))</span>
|
|
<span id="cb54-6"><a href="#cb54-6" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb54-7"><a href="#cb54-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_completo.csv"</span>))</span>
|
|
<span id="cb54-8"><a href="#cb54-8" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb54-9"><a href="#cb54-9" aria-hidden="true" tabindex="-1"></a>resultados_kfold <span class="sc">%>%</span></span>
|
|
<span id="cb54-10"><a href="#cb54-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">%>%</span></span>
|
|
<span id="cb54-11"><a href="#cb54-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) - Dataset Completo"</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) - Dataset Completo</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.3630</td>
|
|
<td style="text-align: right;">0.3100</td>
|
|
<td style="text-align: right;">0.3634</td>
|
|
<td style="text-align: right;">0.9469</td>
|
|
<td style="text-align: right;">0.3835</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">SVM radial ajustado</td>
|
|
<td style="text-align: right;">0.3565</td>
|
|
<td style="text-align: right;">0.3029</td>
|
|
<td style="text-align: right;">0.3568</td>
|
|
<td style="text-align: right;">0.9464</td>
|
|
<td style="text-align: right;">0.3721</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">Random Forest</td>
|
|
<td style="text-align: right;">0.3285</td>
|
|
<td style="text-align: right;">0.2726</td>
|
|
<td style="text-align: right;">0.3288</td>
|
|
<td style="text-align: right;">0.9440</td>
|
|
<td style="text-align: right;">0.3406</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">PCA + SVM radial</td>
|
|
<td style="text-align: right;">0.3236</td>
|
|
<td style="text-align: right;">0.2674</td>
|
|
<td style="text-align: right;">0.3239</td>
|
|
<td style="text-align: right;">0.9436</td>
|
|
<td style="text-align: right;">0.3593</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">SVM radial</td>
|
|
<td style="text-align: right;">0.1419</td>
|
|
<td style="text-align: right;">0.0709</td>
|
|
<td style="text-align: right;">0.1433</td>
|
|
<td style="text-align: right;">0.9285</td>
|
|
<td style="text-align: right;">0.1743</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<section id="comparacao-holdout-vs-k-fold" class="level1" data-number="19">
|
|
<h1 data-number="19"><span class="header-section-number">19</span> Comparacao: Holdout vs K-Fold</h1>
|
|
<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><span class="co"># ============================================================</span></span>
|
|
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a><span class="co"># 19. Comparacao direta entre as duas estrategias</span></span>
|
|
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a><span class="co"># ============================================================</span></span>
|
|
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a>comparacao <span class="ot"><-</span> <span class="fu">bind_rows</span>(</span>
|
|
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a> resultados <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">estrategia =</span> <span class="st">"Holdout 70/30"</span>),</span>
|
|
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a> resultados_kfold <span class="sc">%>%</span> <span class="fu">mutate</span>(<span class="at">estrategia =</span> <span class="st">"K-Fold (k=5)"</span>)</span>
|
|
<span id="cb55-8"><a href="#cb55-8" aria-hidden="true" tabindex="-1"></a>) <span class="sc">%>%</span></span>
|
|
<span id="cb55-9"><a href="#cb55-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(estrategia, modelo, acuracia, kappa, f1_macro) <span class="sc">%>%</span></span>
|
|
<span id="cb55-10"><a href="#cb55-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(estrategia, <span class="fu">desc</span>(acuracia))</span>
|
|
<span id="cb55-11"><a href="#cb55-11" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb55-12"><a href="#cb55-12" aria-hidden="true" tabindex="-1"></a>comparacao <span class="sc">%>%</span></span>
|
|
<span id="cb55-13"><a href="#cb55-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">%>%</span></span>
|
|
<span id="cb55-14"><a href="#cb55-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 K-Fold - Dataset Completo"</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 K-Fold - Dataset Completo</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.3736</td>
|
|
<td style="text-align: right;">0.3214</td>
|
|
<td style="text-align: right;">0.3646</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.3654</td>
|
|
<td style="text-align: right;">0.3125</td>
|
|
<td style="text-align: right;">0.3630</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<td style="text-align: left;">Holdout 70/30</td>
|
|
<td style="text-align: left;">Random Forest</td>
|
|
<td style="text-align: right;">0.3407</td>
|
|
<td style="text-align: right;">0.2857</td>
|
|
<td style="text-align: right;">0.3405</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">Holdout 70/30</td>
|
|
<td style="text-align: left;">PCA + SVM radial</td>
|
|
<td style="text-align: right;">0.3104</td>
|
|
<td style="text-align: right;">0.2530</td>
|
|
<td style="text-align: right;">0.3161</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.1484</td>
|
|
<td style="text-align: right;">0.0774</td>
|
|
<td style="text-align: right;">0.1673</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.3630</td>
|
|
<td style="text-align: right;">0.3100</td>
|
|
<td style="text-align: right;">0.3835</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.3565</td>
|
|
<td style="text-align: right;">0.3029</td>
|
|
<td style="text-align: right;">0.3721</td>
|
|
</tr>
|
|
<tr class="even">
|
|
<td style="text-align: left;">K-Fold (k=5)</td>
|
|
<td style="text-align: left;">Random Forest</td>
|
|
<td style="text-align: right;">0.3285</td>
|
|
<td style="text-align: right;">0.2726</td>
|
|
<td style="text-align: right;">0.3406</td>
|
|
</tr>
|
|
<tr class="odd">
|
|
<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.3236</td>
|
|
<td style="text-align: right;">0.2674</td>
|
|
<td style="text-align: right;">0.3593</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.1419</td>
|
|
<td style="text-align: right;">0.0709</td>
|
|
<td style="text-align: right;">0.1743</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
<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="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="cb56-2"><a href="#cb56-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="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
|
|
<span id="cb56-4"><a href="#cb56-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="cb56-5"><a href="#cb56-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(</span>
|
|
<span id="cb56-6"><a href="#cb56-6" aria-hidden="true" tabindex="-1"></a> <span class="at">title =</span> <span class="st">"Acuracia: Holdout 70/30 vs K-Fold (k=5) - Dataset Completo"</span>,</span>
|
|
<span id="cb56-7"><a href="#cb56-7" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Modelo"</span>,</span>
|
|
<span id="cb56-8"><a href="#cb56-8" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Acuracia"</span>,</span>
|
|
<span id="cb56-9"><a href="#cb56-9" aria-hidden="true" tabindex="-1"></a> <span class="at">fill =</span> <span class="st">"Estrategia"</span></span>
|
|
<span id="cb56-10"><a href="#cb56-10" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
|
|
<span id="cb56-11"><a href="#cb56-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_completo_files/figure-html/unnamed-chunk-26-1.png" class="img-fluid figure-img" width="672"></p>
|
|
</figure>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
<section id="conclusao" class="level1" data-number="20">
|
|
<h1 data-number="20"><span class="header-section-number">20</span> Conclusao</h1>
|
|
<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>melhor_holdout <span class="ot"><-</span> <span class="fu">max</span>(resultados<span class="sc">$</span>acuracia)</span>
|
|
<span id="cb57-2"><a href="#cb57-2" aria-hidden="true" tabindex="-1"></a>melhor_kf <span class="ot"><-</span> <span class="fu">max</span>(resultados_kfold<span class="sc">$</span>acuracia)</span>
|
|
<span id="cb57-3"><a href="#cb57-3" aria-hidden="true" tabindex="-1"></a></span>
|
|
<span id="cb57-4"><a href="#cb57-4" 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.3736 </code></pre>
|
|
</div>
|
|
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb59"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1"><a href="#cb59-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.363 </code></pre>
|
|
</div>
|
|
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb61"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cat</span>(<span class="st">"Melhor modelo (holdout):"</span>, resultados<span class="sc">$</span>modelo[<span class="dv">1</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>Melhor modelo (holdout): SVM linear </code></pre>
|
|
</div>
|
|
<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">cat</span>(<span class="st">"Melhor modelo (k-fold):"</span>, resultados_kfold<span class="sc">$</span>modelo[<span class="dv">1</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>Melhor modelo (k-fold): SVM linear </code></pre>
|
|
</div>
|
|
</div>
|
|
</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> |