| Title: | Twin Support Vector Machines |
|---|---|
| Description: | Provides twin support vector machine classifiers and visualization tools for small to moderate classification problems. Includes one-vs-one multi-class classification and a standard support vector machine baseline for comparison. |
| Authors: | Shamika Tissera [aut, cre] |
| Maintainer: | Shamika Tissera <[email protected]> |
| License: | GPL-3 |
| Version: | 0.0.2 |
| Built: | 2026-06-10 02:50:49 UTC |
| Source: | https://github.com/cran/twinsvm |
Twin support vector machine classifiers with a standard C-SVC baseline.
Maintainer: Shamika Tissera [email protected]
Authors:
Shamika Tissera [email protected]
Jayadeva, Khemchandani, R., and Chandra, S. (2007). Twin support vector machines for pattern classification. IEEE Transactions on Pattern Analysis and Machine Intelligence, 29(5), 905-910.
Kumar, M. A. and Gopal, M. (2009). Least squares twin support vector machines for pattern classification. Expert Systems with Applications, 36(4), 7535-7543.
Extract Twin-SVM Coefficients
## S3 method for class 'tsvm' coef(object, ...)## S3 method for class 'tsvm' coef(object, ...)
object |
A fitted |
... |
Unused. |
A list with the two plane coefficients.
dat <- gen_moons(30) fit <- tsvm(dat$x, dat$y) coef(fit)dat <- gen_moons(30) fit <- tsvm(dat$x, dat$y) coef(fit)
Fits least-squares twin SVM, original QP twin SVM, and the standard C-SVC SVM baseline on the same two-dimensional data, then plots the three decision boundaries side by side.
compare_methods(x, y, kernel = "rbf", gamma = 0.5, c1 = 1, c2 = 1, cost = 1)compare_methods(x, y, kernel = "rbf", gamma = 0.5, c1 = 1, c2 = 1, cost = 1)
x |
Numeric two-column matrix or data frame. |
y |
Two-class response. |
kernel |
Kernel name: |
gamma |
Kernel scale. |
c1, c2
|
Positive twin-SVM regularization parameters. |
cost |
Positive C-SVC cost parameter. |
A faceted ggplot object.
Other visualization:
kernel_lift(),
lift_plot(),
lift_plotly()
set.seed(30) dat <- gen_moons(50, noise = 0.1) compare_methods(dat$x, dat$y, gamma = 1, c1 = 0.2, c2 = 0.2, cost = 1)set.seed(30) dat <- gen_moons(50, noise = 0.1) compare_methods(dat$x, dat$y, gamma = 1, c1 = 0.2, c2 = 0.2, cost = 1)
Predicts on x and returns a confusion matrix plus overall accuracy. This
works for both binary and one-vs-one multiclass tsvm and svms fits.
confusion(object, x, y)confusion(object, x, y)
object |
A fitted |
x |
Numeric matrix or data frame of predictors. |
y |
True class labels. |
A list with table, a table(truth, predicted) confusion matrix,
and accuracy, the overall classification accuracy.
Other multiclass:
predict.svms_multiclass(),
predict.tsvm_multiclass(),
print.svms_multiclass(),
print.tsvm_multiclass()
set.seed(50) x <- rbind( matrix(rnorm(20, -2, 0.2), ncol = 2), matrix(rnorm(20, 0, 0.2), ncol = 2), matrix(rnorm(20, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 10)) fit <- tsvm(x, y, kernel = "linear") confusion(fit, x, y)set.seed(50) x <- rbind( matrix(rnorm(20, -2, 0.2), ncol = 2), matrix(rnorm(20, 0, 0.2), ncol = 2), matrix(rnorm(20, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 10)) fit <- tsvm(x, y, kernel = "linear") confusion(fit, x, y)
Runs k-fold cross-validation over c1, c2, and optionally gamma.
cv_tsvm(x, y, c1_grid, c2_grid, gamma_grid = NULL, k = 5, ...)cv_tsvm(x, y, c1_grid, c2_grid, gamma_grid = NULL, k = 5, ...)
x |
Numeric matrix or data frame of predictors. |
y |
Two-class response. |
c1_grid, c2_grid
|
Positive numeric vectors. |
gamma_grid |
Optional positive numeric vector. If |
k |
Number of folds. |
... |
Additional arguments passed to |
A cv_tsvm object with best_params and results.
set.seed(10) dat <- gen_moons(40, noise = 0.1) cv <- cv_tsvm(dat$x, dat$y, c1_grid = c(0.1, 1), c2_grid = c(0.1, 1), k = 3) cv$best_paramsset.seed(10) dat <- gen_moons(40, noise = 0.1) cv <- cv_tsvm(dat$x, dat$y, c1_grid = c(0.1, 1), c2_grid = c(0.1, 1), k = 3) cv$best_params
Generate Concentric Circles Data
gen_circles(n, noise = 0.05)gen_circles(n, noise = 0.05)
n |
Number of observations. |
noise |
Standard deviation of Gaussian noise. |
A list with numeric matrix x and factor y.
dat <- gen_circles(20, noise = 0.05) str(dat)dat <- gen_circles(20, noise = 0.05) str(dat)
Generate Two-Moons Data
gen_moons(n, noise = 0.2)gen_moons(n, noise = 0.2)
n |
Number of observations. |
noise |
Standard deviation of Gaussian noise. |
A list with numeric matrix x and factor y.
dat <- gen_moons(20, noise = 0.1) str(dat)dat <- gen_moons(20, noise = 0.1) str(dat)
kernel_lift() is kept for existing code. New code should call
lift_plot().
kernel_lift(x, y, gamma = 1, center = NULL)kernel_lift(x, y, gamma = 1, center = NULL)
x |
Numeric two-column matrix or data frame. |
y |
Two-class response. |
gamma |
Positive RBF scale. |
center |
Optional numeric length-two center for the RBF bump. If |
A ggplot object.
Other visualization:
compare_methods(),
lift_plot(),
lift_plotly()
set.seed(22) dat <- gen_circles(60, noise = 0.04) kernel_lift(dat$x, dat$y, gamma = 1)set.seed(22) dat <- gen_circles(60, noise = 0.04) kernel_lift(dat$x, dat$y, gamma = 1)
Maps two-dimensional data to a third RBF height and draws a static oblique
projection. The height is exp(-gamma * ||x - center||^2), so points near
the center rise while points farther away stay low. A translucent plane is
drawn halfway between the two class mean heights.
lift_plot(x, y, gamma = 1, center = NULL)lift_plot(x, y, gamma = 1, center = NULL)
x |
Numeric two-column matrix or data frame. |
y |
Two-class response. |
gamma |
Positive RBF scale. |
center |
Optional numeric length-two center for the RBF bump. If |
A ggplot object.
Other visualization:
compare_methods(),
kernel_lift(),
lift_plotly()
set.seed(20) dat <- gen_circles(80, noise = 0.04) lift_plot(dat$x, dat$y, gamma = 1)set.seed(20) dat <- gen_circles(80, noise = 0.04) lift_plot(dat$x, dat$y, gamma = 1)
Draws the same RBF lift as lift_plot() as a rotatable plotly 3D chart.
This function is optional; the package does not require plotly to build or
check.
lift_plotly(x, y, gamma = 1, center = NULL)lift_plotly(x, y, gamma = 1, center = NULL)
x |
Numeric two-column matrix or data frame. |
y |
Two-class response. |
gamma |
Positive RBF scale. |
center |
Optional numeric length-two center for the RBF bump. If |
A plotly object.
Other visualization:
compare_methods(),
kernel_lift(),
lift_plot()
set.seed(21) dat <- gen_circles(80, noise = 0.04) if (requireNamespace("plotly", quietly = TRUE)) { lift_plotly(dat$x, dat$y, gamma = 1) }set.seed(21) dat <- gen_circles(80, noise = 0.04) if (requireNamespace("plotly", quietly = TRUE)) { lift_plotly(dat$x, dat$y, gamma = 1) }
Refits a model over a sequence of hyperparameter values and returns a
gganimate object showing the boundary change.
morph_boundary( x, y, param = c("gamma", "cost", "c1"), range, model = c("tsvm", "svms"), n = 30, ... )morph_boundary( x, y, param = c("gamma", "cost", "c1"), range, model = c("tsvm", "svms"), n = 30, ... )
x |
Numeric two-column matrix or data frame. |
y |
Two-class response. |
param |
Hyperparameter to vary. |
range |
Numeric length-two range for the hyperparameter. |
model |
Model family: |
n |
Number of frames. |
... |
Additional arguments passed to the model fit function. |
A gganim object.
if (interactive()) { dat <- gen_moons(40) morph_boundary(dat$x, dat$y, param = "gamma", range = c(0.5, 2), n = 4) }if (interactive()) { dat <- gen_moons(40) morph_boundary(dat$x, dat$y, param = "gamma", range = c(0.5, 2), n = 4) }
Plot Twin-SVM Cross-Validation Results
## S3 method for class 'cv_tsvm' plot(x, ...)## S3 method for class 'cv_tsvm' plot(x, ...)
x |
A |
... |
Unused. |
A ggplot object.
set.seed(11) dat <- gen_moons(40, noise = 0.1) cv <- cv_tsvm(dat$x, dat$y, c1_grid = c(0.1, 1), c2_grid = c(0.1, 1), k = 3) plot(cv)set.seed(11) dat <- gen_moons(40, noise = 0.1) cv <- cv_tsvm(dat$x, dat$y, c1_grid = c(0.1, 1), c2_grid = c(0.1, 1), k = 3) plot(cv)
Plot a Standard SVM Decision Boundary
## S3 method for class 'svms' plot(x, ...)## S3 method for class 'svms' plot(x, ...)
x |
A fitted |
... |
Unused. |
A ggplot object.
dat <- gen_moons(40) fit <- svms(dat$x, dat$y, kernel = "linear") plot(fit)dat <- gen_moons(40) fit <- svms(dat$x, dat$y, kernel = "linear") plot(fit)
Plot a Twin-SVM Decision Boundary
## S3 method for class 'tsvm' plot(x, ...)## S3 method for class 'tsvm' plot(x, ...)
x |
A fitted |
... |
Unused. |
A ggplot object.
dat <- gen_moons(40) fit <- tsvm(dat$x, dat$y) plot(fit)dat <- gen_moons(40) fit <- tsvm(dat$x, dat$y) plot(fit)
Predict from a Standard SVM
## S3 method for class 'svms' predict(object, newdata, decision.values = FALSE, ...)## S3 method for class 'svms' predict(object, newdata, decision.values = FALSE, ...)
object |
A fitted |
newdata |
Numeric matrix or data frame. |
decision.values |
If |
... |
Unused. |
A factor of predicted classes, or a numeric vector when
decision.values = TRUE.
set.seed(2) dat <- gen_moons(30) fit <- svms(dat$x, dat$y) predict(fit, dat$x, decision.values = TRUE)set.seed(2) dat <- gen_moons(30) fit <- svms(dat$x, dat$y) predict(fit, dat$x, decision.values = TRUE)
Predicts from a one-vs-one multiclass standard SVM. Each binary model votes for one class. Ties are resolved deterministically by choosing the class that appears first in the training factor level order.
## S3 method for class 'svms_multiclass' predict(object, newdata, type = c("class", "votes"), ...)## S3 method for class 'svms_multiclass' predict(object, newdata, type = c("class", "votes"), ...)
object |
A fitted |
newdata |
Numeric matrix or data frame. |
type |
Output type. |
... |
Unused. |
A factor of predicted classes, or an integer vote matrix when
type = "votes".
Other multiclass:
confusion(),
predict.tsvm_multiclass(),
print.svms_multiclass(),
print.tsvm_multiclass()
set.seed(45) x <- rbind( matrix(rnorm(8, -2, 0.2), ncol = 2), matrix(rnorm(8, 0, 0.2), ncol = 2), matrix(rnorm(8, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 4)) fit <- svms(x, y, kernel = "linear", max_passes = 2, max_iter = 100) predict(fit, x[1:3, , drop = FALSE])set.seed(45) x <- rbind( matrix(rnorm(8, -2, 0.2), ncol = 2), matrix(rnorm(8, 0, 0.2), ncol = 2), matrix(rnorm(8, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 4)) fit <- svms(x, y, kernel = "linear", max_passes = 2, max_iter = 100) predict(fit, x[1:3, , drop = FALSE])
Predicts from a one-vs-one multiclass twin SVM. Each binary model votes for one class. Ties are resolved deterministically by choosing the class that appears first in the training factor level order.
## S3 method for class 'tsvm_multiclass' predict(object, newdata, type = c("class", "votes"), ...)## S3 method for class 'tsvm_multiclass' predict(object, newdata, type = c("class", "votes"), ...)
object |
A fitted |
newdata |
Numeric matrix or data frame. |
type |
Output type. |
... |
Unused. |
A factor of predicted classes, or an integer vote matrix when
type = "votes".
Other multiclass:
confusion(),
predict.svms_multiclass(),
print.svms_multiclass(),
print.tsvm_multiclass()
set.seed(40) x <- rbind( matrix(rnorm(20, -2, 0.2), ncol = 2), matrix(rnorm(20, 0, 0.2), ncol = 2), matrix(rnorm(20, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 10)) fit <- tsvm(x, y, kernel = "linear") predict(fit, x[1:3, , drop = FALSE])set.seed(40) x <- rbind( matrix(rnorm(20, -2, 0.2), ncol = 2), matrix(rnorm(20, 0, 0.2), ncol = 2), matrix(rnorm(20, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 10)) fit <- tsvm(x, y, kernel = "linear") predict(fit, x[1:3, , drop = FALSE])
Print a Multiclass Standard SVM
## S3 method for class 'svms_multiclass' print(x, ...)## S3 method for class 'svms_multiclass' print(x, ...)
x |
A fitted |
... |
Unused. |
The input object, invisibly.
Other multiclass:
confusion(),
predict.svms_multiclass(),
predict.tsvm_multiclass(),
print.tsvm_multiclass()
set.seed(46) x <- rbind( matrix(rnorm(8, -2, 0.2), ncol = 2), matrix(rnorm(8, 0, 0.2), ncol = 2), matrix(rnorm(8, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 4)) print(svms(x, y, kernel = "linear", max_passes = 2, max_iter = 100))set.seed(46) x <- rbind( matrix(rnorm(8, -2, 0.2), ncol = 2), matrix(rnorm(8, 0, 0.2), ncol = 2), matrix(rnorm(8, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 4)) print(svms(x, y, kernel = "linear", max_passes = 2, max_iter = 100))
Print a Multiclass Twin SVM
## S3 method for class 'tsvm_multiclass' print(x, ...)## S3 method for class 'tsvm_multiclass' print(x, ...)
x |
A fitted |
... |
Unused. |
The input object, invisibly.
Other multiclass:
confusion(),
predict.svms_multiclass(),
predict.tsvm_multiclass(),
print.svms_multiclass()
set.seed(41) x <- rbind( matrix(rnorm(20, -2, 0.2), ncol = 2), matrix(rnorm(20, 0, 0.2), ncol = 2), matrix(rnorm(20, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 10)) print(tsvm(x, y, kernel = "linear"))set.seed(41) x <- rbind( matrix(rnorm(20, -2, 0.2), ncol = 2), matrix(rnorm(20, 0, 0.2), ncol = 2), matrix(rnorm(20, 2, 0.2), ncol = 2) ) y <- factor(rep(c("a", "b", "c"), each = 10)) print(tsvm(x, y, kernel = "linear"))
Fits a C-SVC support vector machine with a Platt SMO solver. With two classes this uses the validated binary path. With three or more classes, the function fits one binary SVM for each class pair and predicts by majority vote. Multiclass ties are resolved by choosing the class that appears first in the factor level order.
svms( x, y, kernel = c("linear", "rbf", "poly"), cost = 1, gamma = NULL, degree = 3, coef0 = 1, tol = 0.001, max_passes = 10L, max_iter = 10000L )svms( x, y, kernel = c("linear", "rbf", "poly"), cost = 1, gamma = NULL, degree = 3, coef0 = 1, tol = 0.001, max_passes = 10L, max_iter = 10000L )
x |
Numeric matrix or data frame of predictors. |
y |
Response with at least two classes. In the binary path, level 1 is the negative class and level 2 is the positive class. |
kernel |
Kernel name: |
cost |
Positive C-SVC cost parameter. |
gamma |
Kernel scale. Defaults to |
degree |
Polynomial degree. |
coef0 |
Polynomial offset. |
tol |
SMO tolerance. |
max_passes |
Maximum consecutive passes without alpha changes. |
max_iter |
Maximum SMO iterations. |
A fitted svms object for two classes, or a svms_multiclass object
for three or more classes.
set.seed(1) dat <- gen_moons(40, noise = 0.1) fit <- svms(dat$x, dat$y, kernel = "linear", cost = 1) predict(fit, dat$x[1:3, ])set.seed(1) dat <- gen_moons(40, noise = 0.1) fit <- svms(dat$x, dat$y, kernel = "linear", cost = 1) predict(fit, dat$x[1:3, ])
Fits a twin support vector machine. With two classes this uses the validated
binary twin-SVM path: level 1 of y is class B, level 2 is class A, plane 1
is close to class A, and plane 2 is close to class B. With three or more
classes, the function fits one binary twin SVM for each class pair and
predicts by majority vote. Multiclass ties are resolved by choosing the class
that appears first in the factor level order.
tsvm( x, y, method = c("ls", "twin"), kernel = c("linear", "rbf", "poly"), c1 = 1, c2 = 1, gamma = NULL, degree = 3, coef0 = 1, eps = 1e-06 )tsvm( x, y, method = c("ls", "twin"), kernel = c("linear", "rbf", "poly"), c1 = 1, c2 = 1, gamma = NULL, degree = 3, coef0 = 1, eps = 1e-06 )
x |
Numeric matrix or data frame of predictors. |
y |
Response with at least two classes. |
method |
Twin-SVM method. |
kernel |
Kernel name. |
c1, c2
|
Positive regularization parameters. |
gamma |
Kernel scale. Defaults to |
degree |
Polynomial degree. |
coef0 |
Polynomial offset. |
eps |
Ridge term added to every linear solve. |
A fitted tsvm object for two classes, or a tsvm_multiclass object
for three or more classes.
Jayadeva, Khemchandani, R., and Chandra, S. (2007). Twin support vector machines for pattern classification. IEEE Transactions on Pattern Analysis and Machine Intelligence, 29(5), 905-910.
Kumar, M. A. and Gopal, M. (2009). Least squares twin support vector machines for pattern classification. Expert Systems with Applications, 36(4), 7535-7543.
set.seed(3) dat <- gen_moons(50, noise = 0.05) fit <- tsvm(dat$x, dat$y) predict(fit, dat$x[1:4, ])set.seed(3) dat <- gen_moons(50, noise = 0.05) fit <- tsvm(dat$x, dat$y) predict(fit, dat$x[1:4, ])
A small two-class two-moons data set for examples and tests.
data(twomoons)data(twomoons)
A data frame with 120 rows and 3 variables:
First coordinate.
Second coordinate.
Two-class factor with levels B and A.
data(twomoons) str(twomoons)data(twomoons) str(twomoons)