-
Notifications
You must be signed in to change notification settings - Fork 1
/
readme.qmd
327 lines (261 loc) · 9.53 KB
/
readme.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
---
title: "{maize} package"
format: gfm
knitr:
opts_chunk:
fig.path: "man/figures/README-"
---
# maize <img src="man/figures/logo.png" align="right" height="139" alt="" />
## what is {maize}?
{maize} is an extension library for kernels & support vector machines in tidymodels! The package consists of additional kernel bindings that are not available in the {parsnip} or {recipes} package. Many of the kernels are ported from {kernlab}, additional kernels have been added directly to maize transposed from [Python](https://github.com/gmum/pykernels/blob/master/pykernels/regular.py) and [Julia](https://juliagaussianprocesses.github.io/KernelFunctions.jl/stable/kernels/) packages.
{parnsip} has three kernels available: linear, radial basis function, & polynomial. {maize} extends to further kernels, other engines, and adds steps for {recipes}:
### maize engines
```{r}
#| echo: false
# parsnip bindings --
maize_models <- data.frame(
extension = "{parsnip}",
maize = c("svm_laplace", "svm_tanh", "svm_bessel", "svm_anova_rbf",
"svm_spline","svm_cossim", "svm_cauchy", "svm_tanimoto",
"svm_sorenson", "svm_tstudent", "svm_fourier", "svm_wavelet",
"svm_string"),
engine = rep("kernlab::ksvm", 13),
mode = c(rep("regression & classification", 12), "classification")
)
# recipes bindings --
maize_recipes <- data.frame(
extension = "{recipes}",
maize = c("step_kpca_laplace", "step_kpca_tanh",
"step_kha_laplace", "step_kha_tanh"),
engine = c(rep("kernlab::kpca", 2),
rep("kernlab::kha", 2)),
mode = "transformation steps"
)
# gtUtils fun ---
library(BobRossColors)
bars <- BobRossColors::all_palettes |>
dplyr::filter(painting_title == "wilderness_trail") |>
dplyr::pull(divergent)
# ~ maize gr8 tbl wilderness trail ~ --
maize_models |>
rbind(maize_recipes) |>
gt::gt() |>
gt::tab_header(title = "{maize} bindings",
subtitle = "more to come!") |>
# theming the tbl
gtUtils::gt_theme_ncaa() |>
gtUtils::gt_border_bars_top(bars[1:5]) |>
gtUtils::gt_border_bars_bottom(bars[6:10]) |>
# colored/bolded cells:
gt::tab_style(
style = list(
gt::cell_fill(color = bars[4]),
gt::cell_text(weight = "bold")
),
locations = gt::cells_body(
columns = extension,
rows = extension == "{parsnip}"
)
) |>
gt::tab_style(
style = list(
gt::cell_fill(color = bars[8]),
gt::cell_text(weight = "bold")
),
locations = gt::cells_body(
columns = extension,
rows = extension == "{recipes}"
)
) |>
#gt::as_raw_html()
gt::gtsave("man/figures/gtmaize.png")
```
<img src="man/figures/gtmaize.png" align="center" alt="" />
## Example Overview
### corn kernels and svm kernels
say we have three types of corn with different size kernels which create different height corn. we could (1) create a support vector machine that can predict the height of the corn given the kernel type and kernel size, we could also (2) predict the corn type given the kernel size and corn height.
#### corn kernel dataset
```{r}
#| message: false
#| warning: false
library(parsnip)
library(maize)
library(ggplot2)
# use this later to create a classification field
# -----------------------------------------------
kernel_min <- corn_data$kernel_size |> min()
kernel_max <- corn_data$kernel_size |> max()
kernel_vec <- seq(kernel_min, kernel_max, by = 1)
height_min <- corn_data$height |> min()
height_max <- corn_data$height |> max()
height_vec <- seq(height_min, height_max, by = 1)
corn_grid <- expand.grid(kernel_size = kernel_vec, height = height_vec)
# -----------------------------------------------
maize::corn_data |>
ggplot() +
geom_point(aes(x = kernel_size, y = height, color = type)) +
theme_minimal() +
labs(title = 'corn kernel data')
```
## Regression
### regression for corn kernel height given a specialty svm kernel
```{r}
set.seed(31415)
corn_train <- corn_data |> dplyr::sample_frac(.80)
corn_test <- corn_data |> dplyr::anti_join(corn_train)
# model params --
svm_reg_spec <-
svm_bessel(cost = 1, margin = 0.1) |>
set_mode("regression") |>
set_engine("kernlab")
# fit --
svm_reg_fit <- svm_reg_spec |> fit(height ~ ., data = corn_train)
# predictions --
preds <- predict(svm_reg_fit, corn_test)
# plot --
corn_test |>
cbind(preds) |>
ggplot() +
geom_point(aes(x = kernel_size, y = height, color = type), shape = 1, size = 2) +
geom_point(aes(x = kernel_size, y = .pred, color = type), shape = 2, size = 3) +
theme_minimal() +
labs(title = 'Bessel Kernel SVM',
subtitle = "corn height prediction")
```
## Classification
### classification of corn kernel type given a specialty svm kernel
```{r}
# model params --
svm_cls_spec <-
svm_laplace(cost = 1, margin = 0.1, laplace_sigma = 10) |>
set_mode("classification") |>
set_engine("kernlab")
# fit --
svm_cls_fit <- svm_cls_spec |> fit(type ~ ., data = corn_train)
# predictions --
preds <- predict(svm_cls_fit, corn_grid, "class")
pred_grid <- corn_grid |> cbind(preds)
# plot --
corn_test |>
ggplot() +
geom_tile(inherit.aes = FALSE,
data = pred_grid,
aes(x = kernel_size, y = height, fill = .pred_class),
alpha = .8) +
geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) +
theme_minimal() +
labs(subtitle = "Laplacian Kernel") +
scale_fill_viridis_d() +
scale_color_manual(values = c("violet", "cyan", "orange"))
```
```{r}
# model params --
svm_cls_spec <-
svm_cossim(cost = 1, margin = 0.1) |>
set_mode("classification") |>
set_engine("kernlab")
# fit --
svm_cls_fit <- svm_cls_spec |> fit(type ~ ., data = corn_train)
# predictions --
preds <- predict(svm_cls_fit, corn_grid, "class")
pred_grid <- corn_grid |> cbind(preds)
# plot --
corn_test |>
ggplot() +
geom_tile(inherit.aes = FALSE,
data = pred_grid,
aes(x = kernel_size, y = height, fill = .pred_class),
alpha = .8) +
geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) +
theme_minimal() +
labs(subtitle = "Cossim Similarity Kernel") +
scale_fill_viridis_d() +
scale_color_manual(values = c("violet", "cyan", "orange"))
```
```{r}
# model params --
svm_cls_spec <-
svm_anova_rbf(cost = 1, margin = 0.1) |>
set_mode("classification") |>
set_engine("kernlab")
# fit --
svm_cls_fit <- svm_cls_spec |> fit(type ~ ., data = corn_train)
# predictions --
preds <- predict(svm_cls_fit, corn_grid, "class")
pred_grid <- corn_grid |> cbind(preds)
# plot --
corn_test |>
ggplot() +
geom_tile(inherit.aes = FALSE,
data = pred_grid,
aes(x = kernel_size, y = height, fill = .pred_class),
alpha = .8) +
geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) +
theme_minimal() +
labs(subtitle = "ANOVA RBF Kernel") +
scale_fill_viridis_d() +
scale_color_manual(values = c("violet", "cyan", "orange"))
```
```{r}
# model params --
svm_cls_spec <-
svm_tanimoto(cost = 1, margin = 0.1) |>
set_mode("classification") |>
set_engine("kernlab")
# fit --
svm_cls_fit <- svm_cls_spec |> fit(type ~ ., data = corn_train)
# predictions --
preds <- predict(svm_cls_fit, corn_grid, "class")
pred_grid <- corn_grid |> cbind(preds)
# plot --
corn_test |>
ggplot() +
geom_tile(inherit.aes = FALSE,
data = pred_grid,
aes(x = kernel_size, y = height, fill = .pred_class),
alpha = .8) +
geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) +
theme_minimal() +
labs(subtitle = "Tanimoto Kernel") +
scale_fill_viridis_d() +
scale_color_manual(values = c("violet", "cyan", "orange"))
```
```{r}
# model params --
svm_cls_spec <-
svm_tstudent(cost = 1, margin = 0.1, degree = 3) |>
set_mode("classification") |>
set_engine("kernlab")
# fit --
svm_cls_fit <- svm_cls_spec |> fit(type ~ ., data = corn_train)
# predictions --
preds <- predict(svm_cls_fit, corn_grid, "class")
pred_grid <- corn_grid |> cbind(preds)
# plot --
corn_test |>
ggplot() +
geom_tile(inherit.aes = FALSE,
data = pred_grid,
aes(x = kernel_size, y = height, fill = .pred_class),
alpha = .8) +
geom_point(aes(x = kernel_size, y = height, color = type, shape = type), size = 3) +
theme_minimal() +
labs(subtitle = "T-Student Kernel") +
scale_fill_viridis_d() +
scale_color_manual(values = c("violet", "cyan", "orange"))
```
## future enhancements
additional kernels are always welcome & added periodically. The more novel, the better, this is my hope for {maize}. Other enhancements include adding *lssvm*, *rvm* and/or *kqr* engine bindings to parsnip.
## references
this package is based off the initial parsnip code, kernlab documents, and tidymodels' developer guides. The {maize} package is a corny pun of {parsnip} & {kernels}, aw-shucks!
##### cheatsheets on how to register a model
- https://www.tidymodels.org/learn/develop/models/
- https://github.com/tidymodels/parsnip/blob/main/R/svm_linear.R
- https://github.com/tidymodels/parsnip/blob/main/R/svm_rbf.R
- https://github.com/tidymodels/parsnip/blob/main/R/svm_rbf_data.R
##### make function ideas, zzz.R,check_args,
- https://github.com/tidymodels/poissonreg/tree/main/R
- https://github.com/tidymodels/bonsai/tree/main/R
##### kernel parameters (kpar)
- https://github.com/cran/kernlab/blob/master/R/kernels.R
- https://www.rdocumentation.org/packages/kernlab/versions/0.9-32/topics/ksvm