Skip to content

Instantly share code, notes, and snippets.

@PatrickRWright
Last active June 18, 2020 08:19
Show Gist options
  • Save PatrickRWright/2708e5871ca4ac6dcae40b576a6ca842 to your computer and use it in GitHub Desktop.
Save PatrickRWright/2708e5871ca4ac6dcae40b576a6ca842 to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Transfer info from `getModelInfo()` to a `data.frame`"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# libs\n",
"library(caret)\n",
"library(stringr)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<caption>A data.frame: 6 × 3</caption>\n",
"<thead>\n",
"\t<tr><th></th><th scope=col>model_name</th><th scope=col>lib_dep</th><th scope=col>method_type</th></tr>\n",
"\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><th scope=row>1</th><td>ada </td><td>ada,plyr </td><td>Classification</td></tr>\n",
"\t<tr><th scope=row>2</th><td>AdaBag </td><td>adabag,plyr </td><td>Classification</td></tr>\n",
"\t<tr><th scope=row>3</th><td>AdaBoost.M1</td><td>adabag,plyr </td><td>Classification</td></tr>\n",
"\t<tr><th scope=row>4</th><td>adaboost </td><td>fastAdaboost</td><td>Classification</td></tr>\n",
"\t<tr><th scope=row>5</th><td>amdai </td><td>adaptDA </td><td>Classification</td></tr>\n",
"\t<tr><th scope=row>6</th><td>ANFIS </td><td>frbs </td><td>Regression </td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"A data.frame: 6 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & model\\_name & lib\\_dep & method\\_type\\\\\n",
" & <fct> & <fct> & <fct>\\\\\n",
"\\hline\n",
"\t1 & ada & ada,plyr & Classification\\\\\n",
"\t2 & AdaBag & adabag,plyr & Classification\\\\\n",
"\t3 & AdaBoost.M1 & adabag,plyr & Classification\\\\\n",
"\t4 & adaboost & fastAdaboost & Classification\\\\\n",
"\t5 & amdai & adaptDA & Classification\\\\\n",
"\t6 & ANFIS & frbs & Regression \\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A data.frame: 6 × 3\n",
"\n",
"| <!--/--> | model_name &lt;fct&gt; | lib_dep &lt;fct&gt; | method_type &lt;fct&gt; |\n",
"|---|---|---|---|\n",
"| 1 | ada | ada,plyr | Classification |\n",
"| 2 | AdaBag | adabag,plyr | Classification |\n",
"| 3 | AdaBoost.M1 | adabag,plyr | Classification |\n",
"| 4 | adaboost | fastAdaboost | Classification |\n",
"| 5 | amdai | adaptDA | Classification |\n",
"| 6 | ANFIS | frbs | Regression |\n",
"\n"
],
"text/plain": [
" model_name lib_dep method_type \n",
"1 ada ada,plyr Classification\n",
"2 AdaBag adabag,plyr Classification\n",
"3 AdaBoost.M1 adabag,plyr Classification\n",
"4 adaboost fastAdaboost Classification\n",
"5 amdai adaptDA Classification\n",
"6 ANFIS frbs Regression "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# init\n",
"lib_dep <- c()\n",
"method_type <- c()\n",
"\n",
"# retrieve all data an model name, dependencies and method types\n",
"model_name <- names(getModelInfo())\n",
"for (i in getModelInfo()) { \n",
" lib_dep <- c(lib_dep, paste(i$library, collapse = \",\"))\n",
" method_type <- c(method_type, paste(i$type, collapse = \",\"))\n",
"}\n",
"# merge into a data.frame\n",
"all_caret <- as.data.frame(cbind(model_name, lib_dep, method_type))\n",
"head(all_caret)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
" Classification Classification,Regression Regression \n",
" 102 21 48 \n",
"Regression,Classification \n",
" 67 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# show amount of method types\n",
"table(all_caret$method_type)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<caption>A data.frame: 6 × 3</caption>\n",
"<thead>\n",
"\t<tr><th></th><th scope=col>model_name</th><th scope=col>lib_dep</th><th scope=col>method_type</th></tr>\n",
"\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><th scope=row>6</th><td>ANFIS </td><td>frbs </td><td>Regression </td></tr>\n",
"\t<tr><th scope=row>7</th><td>avNNet </td><td>nnet </td><td>Classification,Regression</td></tr>\n",
"\t<tr><th scope=row>10</th><td>bag </td><td>caret</td><td>Regression,Classification</td></tr>\n",
"\t<tr><th scope=row>11</th><td>bagEarth </td><td>earth</td><td>Regression,Classification</td></tr>\n",
"\t<tr><th scope=row>12</th><td>bagEarthGCV</td><td>earth</td><td>Regression,Classification</td></tr>\n",
"\t<tr><th scope=row>15</th><td>bam </td><td>mgcv </td><td>Regression,Classification</td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"A data.frame: 6 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & model\\_name & lib\\_dep & method\\_type\\\\\n",
" & <fct> & <fct> & <fct>\\\\\n",
"\\hline\n",
"\t6 & ANFIS & frbs & Regression \\\\\n",
"\t7 & avNNet & nnet & Classification,Regression\\\\\n",
"\t10 & bag & caret & Regression,Classification\\\\\n",
"\t11 & bagEarth & earth & Regression,Classification\\\\\n",
"\t12 & bagEarthGCV & earth & Regression,Classification\\\\\n",
"\t15 & bam & mgcv & Regression,Classification\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A data.frame: 6 × 3\n",
"\n",
"| <!--/--> | model_name &lt;fct&gt; | lib_dep &lt;fct&gt; | method_type &lt;fct&gt; |\n",
"|---|---|---|---|\n",
"| 6 | ANFIS | frbs | Regression |\n",
"| 7 | avNNet | nnet | Classification,Regression |\n",
"| 10 | bag | caret | Regression,Classification |\n",
"| 11 | bagEarth | earth | Regression,Classification |\n",
"| 12 | bagEarthGCV | earth | Regression,Classification |\n",
"| 15 | bam | mgcv | Regression,Classification |\n",
"\n"
],
"text/plain": [
" model_name lib_dep method_type \n",
"6 ANFIS frbs Regression \n",
"7 avNNet nnet Classification,Regression\n",
"10 bag caret Regression,Classification\n",
"11 bagEarth earth Regression,Classification\n",
"12 bagEarthGCV earth Regression,Classification\n",
"15 bam mgcv Regression,Classification"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# only regression\n",
"caret_regression <- all_caret[grep(x = all_caret$method_type,\n",
" pattern = \"Regression\"), ]\n",
"head(caret_regression)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<caption>A data.frame: 6 × 3</caption>\n",
"<thead>\n",
"\t<tr><th></th><th scope=col>model_name</th><th scope=col>lib_dep</th><th scope=col>method_type</th></tr>\n",
"\t<tr><th></th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th><th scope=col>&lt;fct&gt;</th></tr>\n",
"</thead>\n",
"<tbody>\n",
"\t<tr><th scope=row>1</th><td>ada </td><td>ada,plyr </td><td>Classification </td></tr>\n",
"\t<tr><th scope=row>2</th><td>AdaBag </td><td>adabag,plyr </td><td>Classification </td></tr>\n",
"\t<tr><th scope=row>3</th><td>AdaBoost.M1</td><td>adabag,plyr </td><td>Classification </td></tr>\n",
"\t<tr><th scope=row>4</th><td>adaboost </td><td>fastAdaboost</td><td>Classification </td></tr>\n",
"\t<tr><th scope=row>5</th><td>amdai </td><td>adaptDA </td><td>Classification </td></tr>\n",
"\t<tr><th scope=row>7</th><td>avNNet </td><td>nnet </td><td>Classification,Regression</td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"A data.frame: 6 × 3\n",
"\\begin{tabular}{r|lll}\n",
" & model\\_name & lib\\_dep & method\\_type\\\\\n",
" & <fct> & <fct> & <fct>\\\\\n",
"\\hline\n",
"\t1 & ada & ada,plyr & Classification \\\\\n",
"\t2 & AdaBag & adabag,plyr & Classification \\\\\n",
"\t3 & AdaBoost.M1 & adabag,plyr & Classification \\\\\n",
"\t4 & adaboost & fastAdaboost & Classification \\\\\n",
"\t5 & amdai & adaptDA & Classification \\\\\n",
"\t7 & avNNet & nnet & Classification,Regression\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A data.frame: 6 × 3\n",
"\n",
"| <!--/--> | model_name &lt;fct&gt; | lib_dep &lt;fct&gt; | method_type &lt;fct&gt; |\n",
"|---|---|---|---|\n",
"| 1 | ada | ada,plyr | Classification |\n",
"| 2 | AdaBag | adabag,plyr | Classification |\n",
"| 3 | AdaBoost.M1 | adabag,plyr | Classification |\n",
"| 4 | adaboost | fastAdaboost | Classification |\n",
"| 5 | amdai | adaptDA | Classification |\n",
"| 7 | avNNet | nnet | Classification,Regression |\n",
"\n"
],
"text/plain": [
" model_name lib_dep method_type \n",
"1 ada ada,plyr Classification \n",
"2 AdaBag adabag,plyr Classification \n",
"3 AdaBoost.M1 adabag,plyr Classification \n",
"4 adaboost fastAdaboost Classification \n",
"5 amdai adaptDA Classification \n",
"7 avNNet nnet Classification,Regression"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# only classification\n",
"caret_classification <- all_caret[grep(x = all_caret$method_type,\n",
" pattern = \"Classification\"), ]\n",
"head(caret_classification)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# install dependencies\n",
"all_libs <- unique(str_split(paste(all_caret$lib_dep, collapse = \",\"), pattern = \",\")[[1]])\n",
"install.packages(all_libs[1:43], dependencies = TRUE)\n",
"# Matrix needs to be installed separately\n",
"install.packages(all_libs[44], dependencies = TRUE)\n",
"install.packages(all_libs[44:116], dependencies = TRUE)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"R version 3.5.1 (2018-07-02)\n",
"Platform: x86_64-conda_cos6-linux-gnu (64-bit)\n",
"Running under: Debian GNU/Linux 10 (buster)\n",
"\n",
"Matrix products: default\n",
"BLAS/LAPACK: /home/jupyterlab/conda/envs/r/lib/R/lib/libRlapack.so\n",
"\n",
"locale:\n",
" [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 \n",
" [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8 \n",
" [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C \n",
"[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C \n",
"\n",
"attached base packages:\n",
"[1] stats graphics grDevices utils datasets methods base \n",
"\n",
"other attached packages:\n",
"[1] stringr_1.4.0 caret_6.0-86 ggplot2_3.3.0 lattice_0.20-41\n",
"\n",
"loaded via a namespace (and not attached):\n",
" [1] pbdZMQ_0.3-3 tidyselect_1.0.0 repr_1.1.0 \n",
" [4] purrr_0.3.4 reshape2_1.4.4 splines_3.5.1 \n",
" [7] colorspace_1.4-1 vctrs_0.2.4 generics_0.0.2 \n",
"[10] stats4_3.5.1 htmltools_0.4.0 base64enc_0.1-3 \n",
"[13] survival_3.1-12 prodlim_2019.11.13 rlang_0.4.5 \n",
"[16] ModelMetrics_1.2.2.2 pillar_1.4.3 glue_1.4.0 \n",
"[19] withr_2.2.0 uuid_0.1-4 foreach_1.5.0 \n",
"[22] lifecycle_0.2.0 plyr_1.8.6 lava_1.6.7 \n",
"[25] timeDate_3043.102 munsell_0.5.0 gtable_0.3.0 \n",
"[28] recipes_0.1.12 codetools_0.2-16 evaluate_0.14 \n",
"[31] class_7.3-17 IRdisplay_0.7.0 Rcpp_1.0.4.6 \n",
"[34] scales_1.1.0 IRkernel_0.8.12 ipred_0.9-9 \n",
"[37] jsonlite_1.6.1 digest_0.6.25 stringi_1.4.6 \n",
"[40] dplyr_0.8.5 grid_3.5.1 tools_3.5.1 \n",
"[43] magrittr_1.5 tibble_3.0.1 crayon_1.3.4 \n",
"[46] pkgconfig_2.0.3 ellipsis_0.3.0 MASS_7.3-51.6 \n",
"[49] Matrix_1.2-18 data.table_1.12.8 pROC_1.16.2 \n",
"[52] lubridate_1.7.8 gower_0.2.1 assertthat_0.2.1 \n",
"[55] iterators_1.0.12 R6_2.4.1 rpart_4.1-15 \n",
"[58] nnet_7.3-14 nlme_3.1-147 compiler_3.5.1 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"sessionInfo()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "conda-env-r-r"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment