Qu

svm_model

Builtin index · Statistics & ML

result = svm_model(X, y, [kernel=], [C=1.0], [gamma=], [degree=3], [coef0=1])

Input: X, y, kernel, C, gamma, degree, coef0 · Output: result

What it is

Binary-classification support-vector machine by simplified SMO on an N-row, D-column X and a length-N target y that must take exactly two distinct values (no one-vs-rest wrapper). kernel is a string, one of "linear", "rbf", "poly"; C (number, default 1.0) is the regularization strength; gamma (number) scales the "rbf"/"poly" kernels; degree (integer, default 3) and coef0 (number, default 1) apply to "poly".

Overloads

3 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
result = svm_model(X, y, [kernel=], [C=1.0], [gamma=], [degree=3], [coef0=1])
X vectorBinary-classification support-vector machine by simplified SMO on an N-row, D-column X and a length-N target y that must take exactly two distinct values (no one-vs-rest wrapper).
y vectorBinary-classification support-vector machine by simplified SMO on an N-row, D-column X and a length-N target y that must take exactly two distinct values (no one-vs-rest wrapper).
kernel string, optionalkernel = "linear" fits a straight decision boundary -- fast, but blind to a relationship that isn't linearly separable.
C number, optional (default 1.0)C (number, default 1.0) is the regularization strength.
gamma number, optionalgamma (number) scales the "rbf"/"poly" kernels.
degree number, optional (default 3)degree (integer, default 3) and coef0 (number, default 1) apply to "poly".
coef0 number, optional (default 1)degree (integer, default 3) and coef0 (number, default 1) apply to "poly".
result Record
—

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

Xx = [0.0, 0.0; 0.0, 1.0; 1.0, 0.0; 1.0, 1.0; 0.1, 0.1; 0.1, 0.9; 0.9, 0.1; 0.9, 0.9]
Yx = [0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0]
svl = svm_model(Xx, Yx, kernel = "linear", C = 1.0)
print("linear kernel accuracy on XOR: {round(svl.score(Xx, Yx), 3)}")
output
linear kernel accuracy on XOR: 0.5
CodeInputOutputDescription
result = svm_model(X, y, [kernel=], [C=1.0], [gamma=], [degree=3], [coef0=1])
X vectorBinary-classification support-vector machine by simplified SMO on an N-row, D-column X and a length-N target y that must take exactly two distinct values (no one-vs-rest wrapper).
y vectorBinary-classification support-vector machine by simplified SMO on an N-row, D-column X and a length-N target y that must take exactly two distinct values (no one-vs-rest wrapper).
kernel string, optionalkernel = "rbf" maps into an implicit, much higher-dimensional space through a Gaussian kernel scaled by gamma, so it can separate a boundary no straight line can.
C number, optional (default 1.0)C (number, default 1.0) is the regularization strength.
gamma number, optionalkernel = "rbf" maps into an implicit, much higher-dimensional space through a Gaussian kernel scaled by gamma, so it can separate a boundary no straight line can.
degree number, optional (default 3)degree (integer, default 3) and coef0 (number, default 1) apply to "poly".
coef0 number, optional (default 1)degree (integer, default 3) and coef0 (number, default 1) apply to "poly".
result Record
—

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

Xx = [0.0, 0.0; 0.0, 1.0; 1.0, 0.0; 1.0, 1.0; 0.1, 0.1; 0.1, 0.9; 0.9, 0.1; 0.9, 0.9]
Yx = [0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0]
svr = svm_model(Xx, Yx, kernel = "rbf", gamma = 2.0, C = 1.0)
print("rbf kernel accuracy on XOR:    {round(svr.score(Xx, Yx), 3)}")
output
rbf kernel accuracy on XOR:    1
CodeInputOutputDescription
result = svm_model(X, y, [kernel=], [C=1.0], [gamma=], [degree=3], [coef0=1])
X vectorBinary-classification support-vector machine by simplified SMO on an N-row, D-column X and a length-N target y that must take exactly two distinct values (no one-vs-rest wrapper).
y vectorBinary-classification support-vector machine by simplified SMO on an N-row, D-column X and a length-N target y that must take exactly two distinct values (no one-vs-rest wrapper).
kernel string, optionalkernel = "poly" raises the dot product to degree (shifted by coef0) -- a middle ground between "linear" and "rbf".
C number, optional (default 1.0)C (number, default 1.0) is the regularization strength.
gamma number, optionalgamma (number) scales the "rbf"/"poly" kernels.
degree number, optional (default 3)kernel = "poly" raises the dot product to degree (shifted by coef0) -- a middle ground between "linear" and "rbf".
coef0 number, optional (default 1)kernel = "poly" raises the dot product to degree (shifted by coef0) -- a middle ground between "linear" and "rbf".
result Record
—

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

Xx = [0.0, 0.0; 0.0, 1.0; 1.0, 0.0; 1.0, 1.0; 0.1, 0.1; 0.1, 0.9; 0.9, 0.1; 0.9, 0.9]
Yx = [0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0]
svp = svm_model(Xx, Yx, kernel = "poly", degree = 3, coef0 = 1.0, C = 1.0)
print("poly kernel accuracy on XOR:   {round(svp.score(Xx, Yx), 3)}")
output
poly kernel accuracy on XOR:   0.75

How it works

svm_model(X, y, kernel=, C=, gamma=) — binary-classification support vector machine, fit via svm_smo_fit's simplified SMO (Andrew Ng's CS229 "Simplified SMO" notes — a textbook dual-coordinate-ascent solver, chosen over full Platt SMO's heuristic working-set selection because it's straightforward to verify against a written reference and needs no general QP dependency). kernel="linear" (default) or "rbf" (gamma= controls width, default 1/n_features — the standard heuristic scikit-learn's own default used before "scale"); "poly" (degree=/coef0=, defaults 3/1) is a stretch-goal third kernel, exercised far less than the required linear/rbf pair. y must have exactly two distinct values (mapped to -1/+1 internally, smaller value -> -1); multi-class (one-vs-rest) wasn't built this pass — see svm_fit_model's own doc comment. The training kernel (Gram) matrix's X @ X^T cross term is computed via svm_cross_term, GPU-accelerated through the same gpu_matmul/qu_gpu::GpuContext path when this crate is built with --features gpu (plain CPU matmul otherwise) — see that function's own doc comment.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.