SVR_REGRESS
Support vector regression fits a margin-based model for continuous targets and can use linear or nonlinear kernels to capture curved relationships. It is a practical choice when a flexible predictor is needed on modestly sized tabular datasets.
The regressor constructs a tube of radius \epsilon and minimizes the dual objective:
\min_{w} \frac{1}{2} ||w||^2_2 + C \sum_{i=1}^n \max(0, |y_i - f(x_i)| - \epsilon)
where C is the regularization parameter penalizing predictions that fall outside the \epsilon-tube.
This wrapper accepts tabular feature data with rows as samples and columns as features, plus a numeric target supplied as a single row or single column. It returns the training R^2 together with fitted predictions, residuals, and support-vector summary properties.
Excel Usage
=SVR_REGRESS(data, target, C, svr_kernel, degree, svr_gamma, epsilon, tol)
data(list[list], required): 2D array of numeric feature data with rows as samples and columns as features.target(list[list], required): Numeric target values as a single row, single column, or scalar when only one sample is present.C(float, optional, default: 1): Regularization parameter controlling the penalty for margin violations.svr_kernel(str, optional, default: “rbf”): Kernel function used to build the regression model.degree(int, optional, default: 3): Polynomial degree when the polynomial kernel is used.svr_gamma(str, optional, default: “scale”): Gamma scaling mode for non-linear kernels.epsilon(float, optional, default: 0.1): Width of the epsilon-insensitive tube around the fitted prediction function.tol(float, optional, default: 0.001): Convergence tolerance for the optimizer.
Returns (dict): Excel data type containing training R^2, predictions, residuals, and support-vector summary properties.
Example 1: Fit linear support vector regression on a one-feature trend
Inputs:
| data | target | C | svr_kernel | degree | svr_gamma | epsilon | tol |
|---|---|---|---|---|---|---|---|
| 0 | 1 | 10 | linear | 3 | scale | 0.01 | 0.001 |
| 1 | 3 | ||||||
| 2 | 5 | ||||||
| 3 | 7 | ||||||
| 4 | 9 | ||||||
| 5 | 11 |
Excel formula:
=SVR_REGRESS({0;1;2;3;4;5}, {1;3;5;7;9;11}, 10, "linear", 3, "scale", 0.01, 0.001)
Expected output:
{"type":"Double","basicValue":0.999996,"properties":{"training_r2":{"type":"Double","basicValue":0.999996},"mean_squared_error":{"type":"Double","basicValue":0.0000466667},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":1},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":1.01}],[{"type":"Double","basicValue":3.006}],[{"type":"Double","basicValue":5.002}],[{"type":"Double","basicValue":6.998}],[{"type":"Double","basicValue":8.994}],[{"type":"Double","basicValue":10.99}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.01}],[{"type":"Double","basicValue":-0.006}],[{"type":"Double","basicValue":-0.002}],[{"type":"Double","basicValue":0.002}],[{"type":"Double","basicValue":0.006}],[{"type":"Double","basicValue":0.01}]]},"support_vector_count":{"type":"Double","basicValue":2},"support_counts":{"type":"Array","elements":[[{"type":"Double","basicValue":2}]]},"support_indices":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":5}]]},"support_vectors":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":5}]]},"dual_coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.3992},{"type":"Double","basicValue":0.3992}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":1.01}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":1.996}]]}}}
Example 2: Fit RBF support vector regression on a curved target pattern
Inputs:
| data | target | C | svr_kernel | degree | svr_gamma | epsilon | tol | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0.8 | 1.2 | 0.8 | 0.1 | -0.6 | 20 | rbf | 3 | scale | 0.01 | 0.001 |
| 0.5 | ||||||||||||
| 1 | ||||||||||||
| 1.5 | ||||||||||||
| 2 | ||||||||||||
| 2.5 |
Excel formula:
=SVR_REGRESS({0;0.5;1;1.5;2;2.5}, {0,0.8,1.2,0.8,0.1,-0.6}, 20, "rbf", 3, "scale", 0.01, 0.001)
Expected output:
{"type":"Double","basicValue":0.99972,"properties":{"training_r2":{"type":"Double","basicValue":0.99972},"mean_squared_error":{"type":"Double","basicValue":0.000103229},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":1},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":0.0101623}],[{"type":"Double","basicValue":0.790349}],[{"type":"Double","basicValue":1.18936}],[{"type":"Double","basicValue":0.810162}],[{"type":"Double","basicValue":0.0898215}],[{"type":"Double","basicValue":-0.589856}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.0101623}],[{"type":"Double","basicValue":0.00965111}],[{"type":"Double","basicValue":0.0106389}],[{"type":"Double","basicValue":-0.0101623}],[{"type":"Double","basicValue":0.0101785}],[{"type":"Double","basicValue":-0.0101439}]]},"support_vector_count":{"type":"Double","basicValue":6},"support_counts":{"type":"Array","elements":[[{"type":"Double","basicValue":6}]]},"support_indices":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":1}],[{"type":"Double","basicValue":2}],[{"type":"Double","basicValue":3}],[{"type":"Double","basicValue":4}],[{"type":"Double","basicValue":5}]]},"support_vectors":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":0.5}],[{"type":"Double","basicValue":1}],[{"type":"Double","basicValue":1.5}],[{"type":"Double","basicValue":2}],[{"type":"Double","basicValue":2.5}]]},"dual_coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.71684},{"type":"Double","basicValue":0.486863},{"type":"Double","basicValue":1.14565},{"type":"Double","basicValue":-0.630659},{"type":"Double","basicValue":1.08314},{"type":"Double","basicValue":-1.36815}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":0.115342}]]}}}
Example 3: Fit linear support vector regression on a two-feature plane
Inputs:
| data | target | C | svr_kernel | degree | svr_gamma | epsilon | tol | |
|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 10 | linear | 3 | scale | 0.01 | 0.001 |
| 1 | 0 | 3 | ||||||
| 0 | 1 | 4 | ||||||
| 1 | 1 | 6 | ||||||
| 2 | 1 | 8 | ||||||
| 2 | 2 | 11 |
Excel formula:
=SVR_REGRESS({0,0;1,0;0,1;1,1;2,1;2,2}, {1;3;4;6;8;11}, 10, "linear", 3, "scale", 0.01, 0.001)
Expected output:
{"type":"Double","basicValue":0.999995,"properties":{"training_r2":{"type":"Double","basicValue":0.999995},"mean_squared_error":{"type":"Double","basicValue":0.0000507931},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":2},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":1.01022}],[{"type":"Double","basicValue":3.00989}],[{"type":"Double","basicValue":4.00039}],[{"type":"Double","basicValue":6.00006}],[{"type":"Double","basicValue":7.99972}],[{"type":"Double","basicValue":10.9899}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.0102226}],[{"type":"Double","basicValue":-0.0098887}],[{"type":"Double","basicValue":-0.000389564}],[{"type":"Double","basicValue":-0.000055652}],[{"type":"Double","basicValue":0.00027826}],[{"type":"Double","basicValue":0.0101113}]]},"support_vector_count":{"type":"Double","basicValue":3},"support_counts":{"type":"Array","elements":[[{"type":"Double","basicValue":3}]]},"support_indices":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":1}],[{"type":"Double","basicValue":5}]]},"support_vectors":{"type":"Array","elements":[[{"type":"Double","basicValue":0},{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":1},{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":2},{"type":"Double","basicValue":2}]]},"dual_coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.504583},{"type":"Double","basicValue":-0.990501},{"type":"Double","basicValue":1.49508}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":1.01022}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":1.99967},{"type":"Double","basicValue":2.99017}]]}}}
Example 4: Fit polynomial support vector regression on a quadratic curve
Inputs:
| data | target | C | svr_kernel | degree | svr_gamma | epsilon | tol |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 50 | poly | 2 | auto | 0.01 | 0.001 |
| 1 | 1 | ||||||
| 2 | 4 | ||||||
| 3 | 9 | ||||||
| 4 | 16 | ||||||
| 5 | 25 |
Excel formula:
=SVR_REGRESS({0;1;2;3;4;5}, {0;1;4;9;16;25}, 50, "poly", 2, "auto", 0.01, 0.001)
Expected output:
{"type":"Double","basicValue":0.999999,"properties":{"training_r2":{"type":"Double","basicValue":0.999999},"mean_squared_error":{"type":"Double","basicValue":0.00005776},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":1},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":0.01}],[{"type":"Double","basicValue":1.0092}],[{"type":"Double","basicValue":4.0068}],[{"type":"Double","basicValue":9.0028}],[{"type":"Double","basicValue":15.9972}],[{"type":"Double","basicValue":24.99}]]},"residuals":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.01}],[{"type":"Double","basicValue":-0.0092}],[{"type":"Double","basicValue":-0.0068}],[{"type":"Double","basicValue":-0.0028}],[{"type":"Double","basicValue":0.0028}],[{"type":"Double","basicValue":0.01}]]},"support_vector_count":{"type":"Double","basicValue":2},"support_counts":{"type":"Array","elements":[[{"type":"Double","basicValue":2}]]},"support_indices":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":5}]]},"support_vectors":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":5}]]},"dual_coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.039968},{"type":"Double","basicValue":0.039968}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":0.01}]]}}}
Python Code
import numpy as np
from sklearn.svm import SVR as SklearnSVR
def svr_regress(data, target, C=1, svr_kernel='rbf', degree=3, svr_gamma='scale', epsilon=0.1, tol=0.001):
"""
Fit a support vector regression model and return training predictions.
See: https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVR.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): 2D array of numeric feature data with rows as samples and columns as features.
target (list[list]): Numeric target values as a single row, single column, or scalar when only one sample is present.
C (float, optional): Regularization parameter controlling the penalty for margin violations. Default is 1.
svr_kernel (str, optional): Kernel function used to build the regression model. Valid options: RBF, Linear, Polynomial, Sigmoid. Default is 'rbf'.
degree (int, optional): Polynomial degree when the polynomial kernel is used. Default is 3.
svr_gamma (str, optional): Gamma scaling mode for non-linear kernels. Valid options: Scale, Auto. Default is 'scale'.
epsilon (float, optional): Width of the epsilon-insensitive tube around the fitted prediction function. Default is 0.1.
tol (float, optional): Convergence tolerance for the optimizer. Default is 0.001.
Returns:
dict: Excel data type containing training $R^2$, predictions, residuals, and support-vector summary properties.
"""
def py(value):
return value.item() if isinstance(value, np.generic) else value
def cell(value):
value = py(value)
if isinstance(value, bool):
return {"type": "Boolean", "basicValue": bool(value)}
if isinstance(value, (int, float)) and not isinstance(value, bool):
return {"type": "Double", "basicValue": float(value)}
return {"type": "String", "basicValue": str(value)}
def col(values):
return [[cell(value)] for value in values]
def mat(values):
return [[cell(value) for value in row] for row in values]
def parse_data(value):
value = [[value]] if not isinstance(value, list) else value
if not isinstance(value, list) or not value or not all(isinstance(row, list) and row for row in value):
return None, "Error: data must be a non-empty 2D list"
if len({len(row) for row in value}) != 1:
return None, "Error: data must be a rectangular 2D list"
data_np = np.array(value, dtype=float)
if data_np.ndim != 2 or data_np.size == 0:
return None, "Error: data must be a non-empty 2D list"
if not np.isfinite(data_np).all():
return None, "Error: data must contain only finite numeric values"
return data_np, None
def parse_target(value, sample_count):
if not isinstance(value, list):
labels = [value]
elif not value:
return None, "Error: target must be non-empty"
elif all(not isinstance(item, list) for item in value):
labels = value
elif len(value) == 1:
labels = value[0]
elif all(isinstance(row, list) and len(row) == 1 for row in value):
labels = [row[0] for row in value]
else:
return None, "Error: target must be a single row or column"
if len(labels) != sample_count:
return None, "Error: target length must match sample count"
parsed = []
for item in labels:
item = py(item)
if isinstance(item, bool) or not isinstance(item, (int, float)):
return None, "Error: target values must be finite numeric scalars"
if not np.isfinite(float(item)):
return None, "Error: target values must be finite numeric scalars"
parsed.append(float(item))
return np.array(parsed, dtype=float), None
def flat_float_list(values):
return [float(py(item)) for item in np.asarray(values).reshape(-1).tolist()]
try:
data_np, error = parse_data(data)
if error:
return error
target_np, error = parse_target(target, data_np.shape[0])
if error:
return error
if float(C) <= 0:
return "Error: C must be greater than 0"
kernel_value = str(svr_kernel).strip().lower()
if kernel_value not in {"rbf", "linear", "poly", "sigmoid"}:
return "Error: svr_kernel must be 'rbf', 'linear', 'poly', or 'sigmoid'"
if int(degree) < 1:
return "Error: degree must be at least 1"
gamma_value = str(svr_gamma).strip().lower()
if gamma_value not in {"scale", "auto"}:
return "Error: svr_gamma must be 'scale' or 'auto'"
if float(epsilon) < 0:
return "Error: epsilon must be non-negative"
if float(tol) <= 0:
return "Error: tol must be greater than 0"
fitted = SklearnSVR(
C=float(C),
kernel=kernel_value,
degree=int(degree),
gamma=gamma_value,
epsilon=float(epsilon),
tol=float(tol)
).fit(data_np, target_np)
prediction_array = np.asarray(fitted.predict(data_np), dtype=float)
residual_array = target_np - prediction_array
predictions = flat_float_list(prediction_array)
residuals = flat_float_list(residual_array)
training_r2 = float(fitted.score(data_np, target_np))
mse = float(np.mean(np.square(residual_array)))
properties = {
"training_r2": {"type": "Double", "basicValue": training_r2},
"mean_squared_error": {"type": "Double", "basicValue": mse},
"sample_count": {"type": "Double", "basicValue": float(data_np.shape[0])},
"feature_count": {"type": "Double", "basicValue": float(data_np.shape[1])},
"predictions": {"type": "Array", "elements": col(predictions)},
"residuals": {"type": "Array", "elements": col(residuals)},
"support_vector_count": {"type": "Double", "basicValue": float(len(fitted.support_))},
"support_counts": {"type": "Array", "elements": col(np.atleast_1d(fitted.n_support_).tolist())},
"support_indices": {"type": "Array", "elements": col(fitted.support_.tolist())},
"support_vectors": {"type": "Array", "elements": mat(fitted.support_vectors_.tolist())},
"dual_coefficients": {"type": "Array", "elements": mat(np.atleast_2d(fitted.dual_coef_).tolist())},
"intercepts": {"type": "Array", "elements": col(np.atleast_1d(fitted.intercept_).tolist())}
}
if kernel_value == "linear":
properties["coefficients"] = {"type": "Array", "elements": mat(np.atleast_2d(fitted.coef_).tolist())}
return {
"type": "Double",
"basicValue": training_r2,
"properties": properties
}
except Exception as e:
return f"Error: {str(e)}"