RIDGE_CLASSIFY
Ridge classification converts the target labels to \{-1, 1\} (for binary) and treats the task as a regression problem with L2 regularization. The weights w are found by minimizing the penalized squared error:
\min_{w} \|Xw - y\|^2 + \alpha \|w\|^2
The predicted class is the one with the largest linear decision score. It is a fast baseline for dense tabular classification tasks when calibrated probabilities are not required.
This wrapper accepts rows as samples and a target supplied as a single row or single column. It returns training accuracy together with predicted labels, class counts, decision scores, and fitted coefficient arrays.
Excel Usage
=RIDGE_CLASSIFY(data, target, alpha, ridge_solver, fit_intercept, tol, random_state)
data(list[list], required): 2D array of numeric feature data with rows as samples and columns as features.target(list[list], required): Target labels as a single row, single column, or scalar when only one sample is present.alpha(float, optional, default: 1): L2 regularization strength applied to the classifier.ridge_solver(str, optional, default: “auto”): Linear algebra solver used to fit the classifier.fit_intercept(bool, optional, default: true): Whether to include an intercept term in the linear decision function.tol(float, optional, default: 0.0001): Convergence tolerance for iterative solvers.random_state(int, optional, default: null): Integer seed for stochastic solvers. Leave blank for the estimator default.
Returns (dict): Excel data type containing training accuracy, predictions, decision scores, and fitted coefficient arrays.
Example 1: Fit ridge classification for two string-labeled classes
Inputs:
| data | target | alpha | ridge_solver | fit_intercept | tol | random_state | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | cold | 1 | auto | true | 0.0001 | 0 |
| 0 | 1 | cold | |||||
| 1 | 0 | cold | |||||
| 2 | 2 | hot | |||||
| 2 | 3 | hot | |||||
| 3 | 2 | hot |
Excel formula:
=RIDGE_CLASSIFY({0,0;0,1;1,0;2,2;2,3;3,2}, {"cold";"cold";"cold";"hot";"hot";"hot"}, 1, "auto", TRUE, 0.0001, 0)
Expected output:
{"type":"Double","basicValue":1,"properties":{"accuracy":{"type":"Double","basicValue":1},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":2},"class_count":{"type":"Double","basicValue":2},"classes":{"type":"Array","elements":[[{"type":"String","basicValue":"cold"}],[{"type":"String","basicValue":"hot"}]]},"predictions":{"type":"Array","elements":[[{"type":"String","basicValue":"cold"}],[{"type":"String","basicValue":"cold"}],[{"type":"String","basicValue":"cold"}],[{"type":"String","basicValue":"hot"}],[{"type":"String","basicValue":"hot"}],[{"type":"String","basicValue":"hot"}]]},"prediction_counts":{"type":"Array","elements":[[{"type":"String","basicValue":"class"},{"type":"String","basicValue":"count"}],[{"type":"String","basicValue":"cold"},{"type":"Double","basicValue":3}],[{"type":"String","basicValue":"hot"},{"type":"Double","basicValue":3}]]},"decision_scores":{"type":"Array","elements":[[{"type":"Double","basicValue":-1.17073}],[{"type":"Double","basicValue":-0.731707}],[{"type":"Double","basicValue":-0.731707}],[{"type":"Double","basicValue":0.585366}],[{"type":"Double","basicValue":1.02439}],[{"type":"Double","basicValue":1.02439}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":0.439024},{"type":"Double","basicValue":0.439024}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":-1.17073}]]}}}
Example 2: Flatten a single-row numeric target for ridge classification
Inputs:
| data | target | alpha | ridge_solver | fit_intercept | tol | random_state | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | auto | true | 0.0001 | 0 |
| 0.2 | |||||||||||
| 0.4 | |||||||||||
| 1.2 | |||||||||||
| 1.4 | |||||||||||
| 1.6 |
Excel formula:
=RIDGE_CLASSIFY({0;0.2;0.4;1.2;1.4;1.6}, {0,0,0,1,1,1}, 1, "auto", TRUE, 0.0001, 0)
Expected output:
{"type":"Double","basicValue":1,"properties":{"accuracy":{"type":"Double","basicValue":1},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":1},"class_count":{"type":"Double","basicValue":2},"classes":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":1}]]},"predictions":{"type":"Array","elements":[[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":0}],[{"type":"Double","basicValue":1}],[{"type":"Double","basicValue":1}],[{"type":"Double","basicValue":1}]]},"prediction_counts":{"type":"Array","elements":[[{"type":"String","basicValue":"class"},{"type":"String","basicValue":"count"}],[{"type":"Double","basicValue":0},{"type":"Double","basicValue":3}],[{"type":"Double","basicValue":1},{"type":"Double","basicValue":3}]]},"decision_scores":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.86747}],[{"type":"Double","basicValue":-0.650602}],[{"type":"Double","basicValue":-0.433735}],[{"type":"Double","basicValue":0.433735}],[{"type":"Double","basicValue":0.650602}],[{"type":"Double","basicValue":0.86747}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":1.08434}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.86747}]]}}}
Example 3: Fit ridge classification for three separated groups
Inputs:
| data | target | alpha | ridge_solver | fit_intercept | tol | random_state | |
|---|---|---|---|---|---|---|---|
| 0 | 0 | left | 1 | auto | true | 0.0001 | 0 |
| 0.2 | 0.1 | left | |||||
| 4 | 4 | center | |||||
| 4.2 | 3.9 | center | |||||
| 8 | 0 | right | |||||
| 8.2 | 0.1 | right |
Excel formula:
=RIDGE_CLASSIFY({0,0;0.2,0.1;4,4;4.2,3.9;8,0;8.2,0.1}, {"left";"left";"center";"center";"right";"right"}, 1, "auto", TRUE, 0.0001, 0)
Expected output:
{"type":"Double","basicValue":1,"properties":{"accuracy":{"type":"Double","basicValue":1},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":2},"class_count":{"type":"Double","basicValue":3},"classes":{"type":"Array","elements":[[{"type":"String","basicValue":"center"}],[{"type":"String","basicValue":"left"}],[{"type":"String","basicValue":"right"}]]},"predictions":{"type":"Array","elements":[[{"type":"String","basicValue":"left"}],[{"type":"String","basicValue":"left"}],[{"type":"String","basicValue":"center"}],[{"type":"String","basicValue":"center"}],[{"type":"String","basicValue":"right"}],[{"type":"String","basicValue":"right"}]]},"prediction_counts":{"type":"Array","elements":[[{"type":"String","basicValue":"class"},{"type":"String","basicValue":"count"}],[{"type":"String","basicValue":"center"},{"type":"Double","basicValue":2}],[{"type":"String","basicValue":"left"},{"type":"Double","basicValue":2}],[{"type":"String","basicValue":"right"},{"type":"Double","basicValue":2}]]},"decision_scores":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.992335},{"type":"Double","basicValue":1.00431},{"type":"Double","basicValue":-1.01198}],[{"type":"Double","basicValue":-0.943513},{"type":"Double","basicValue":0.930727},{"type":"Double","basicValue":-0.987214}],[{"type":"Double","basicValue":0.960875},{"type":"Double","basicValue":-0.955539},{"type":"Double","basicValue":-1.00534}],[{"type":"Double","basicValue":0.912022},{"type":"Double","basicValue":-0.980309},{"type":"Double","basicValue":-0.931713}],[{"type":"Double","basicValue":-0.992936},{"type":"Double","basicValue":-0.962803},{"type":"Double","basicValue":0.955739}],[{"type":"Double","basicValue":-0.944113},{"type":"Double","basicValue":-1.03639},{"type":"Double","basicValue":0.980501}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.0000750657},{"type":"Double","basicValue":0.488378}],[{"type":"Double","basicValue":-0.245889},{"type":"Double","basicValue":-0.244073}],[{"type":"Double","basicValue":0.245964},{"type":"Double","basicValue":-0.244304}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.992335}],[{"type":"Double","basicValue":1.00431}],[{"type":"Double","basicValue":-1.01198}]]}}}
Example 4: Fit ridge classification with boolean target labels
Inputs:
| data | target | alpha | ridge_solver | fit_intercept | tol | random_state |
|---|---|---|---|---|---|---|
| 0 | false | 1 | auto | true | 0.0001 | 0 |
| 0.3 | false | |||||
| 0.6 | false | |||||
| 1.4 | true | |||||
| 1.7 | true | |||||
| 2 | true |
Excel formula:
=RIDGE_CLASSIFY({0;0.3;0.6;1.4;1.7;2}, {FALSE;FALSE;FALSE;TRUE;TRUE;TRUE}, 1, "auto", TRUE, 0.0001, 0)
Expected output:
{"type":"Double","basicValue":1,"properties":{"accuracy":{"type":"Double","basicValue":1},"sample_count":{"type":"Double","basicValue":6},"feature_count":{"type":"Double","basicValue":1},"class_count":{"type":"Double","basicValue":2},"classes":{"type":"Array","elements":[[{"type":"Boolean","basicValue":false}],[{"type":"Boolean","basicValue":true}]]},"predictions":{"type":"Array","elements":[[{"type":"Boolean","basicValue":false}],[{"type":"Boolean","basicValue":false}],[{"type":"Boolean","basicValue":false}],[{"type":"Boolean","basicValue":true}],[{"type":"Boolean","basicValue":true}],[{"type":"Boolean","basicValue":true}]]},"prediction_counts":{"type":"Array","elements":[[{"type":"String","basicValue":"class"},{"type":"String","basicValue":"count"}],[{"type":"Boolean","basicValue":false},{"type":"Double","basicValue":3}],[{"type":"Boolean","basicValue":true},{"type":"Double","basicValue":3}]]},"decision_scores":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.976744}],[{"type":"Double","basicValue":-0.683721}],[{"type":"Double","basicValue":-0.390698}],[{"type":"Double","basicValue":0.390698}],[{"type":"Double","basicValue":0.683721}],[{"type":"Double","basicValue":0.976744}]]},"coefficients":{"type":"Array","elements":[[{"type":"Double","basicValue":0.976744}]]},"intercepts":{"type":"Array","elements":[[{"type":"Double","basicValue":-0.976744}]]}}}
Python Code
import numpy as np
from sklearn.linear_model import RidgeClassifier as SklearnRidgeClassifier
def ridge_classify(data, target, alpha=1, ridge_solver='auto', fit_intercept=True, tol=0.0001, random_state=None):
"""
Fit a ridge classifier and return training predictions with decision scores.
See: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.RidgeClassifier.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]): Target labels as a single row, single column, or scalar when only one sample is present.
alpha (float, optional): L2 regularization strength applied to the classifier. Default is 1.
ridge_solver (str, optional): Linear algebra solver used to fit the classifier. Valid options: Auto, SVD, LSQR, SAG, SAGA. Default is 'auto'.
fit_intercept (bool, optional): Whether to include an intercept term in the linear decision function. Default is True.
tol (float, optional): Convergence tolerance for iterative solvers. Default is 0.0001.
random_state (int, optional): Integer seed for stochastic solvers. Leave blank for the estimator default. Default is None.
Returns:
dict: Excel data type containing training accuracy, predictions, decision scores, and fitted coefficient arrays.
"""
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 = []
classes = []
for item in labels:
item = py(item)
if isinstance(item, str):
if not item.strip():
return None, "Error: target labels must not be blank"
elif isinstance(item, bool):
item = bool(item)
elif isinstance(item, (int, float)) and not isinstance(item, bool):
if not np.isfinite(float(item)):
return None, "Error: target labels must be finite"
item = float(item) if isinstance(item, float) else int(item)
else:
return None, "Error: target labels must be scalar string, boolean, or numeric values"
parsed.append(item)
if not any(type(existing) is type(item) and existing == item for existing in classes):
classes.append(item)
if len(classes) < 2:
return None, "Error: target must contain at least 2 classes"
return parsed, None
def count_table(predictions, classes):
rows = [[{"type": "String", "basicValue": "class"}, {"type": "String", "basicValue": "count"}]]
for class_label in classes:
count = sum(type(prediction) is type(class_label) and prediction == class_label for prediction in predictions)
rows.append([cell(class_label), {"type": "Double", "basicValue": float(count)}])
return rows
def score_rows(values):
values = np.asarray(values)
return [[float(value)] for value in values.tolist()] if values.ndim == 1 else values.tolist()
try:
data_np, error = parse_data(data)
if error:
return error
target_values, error = parse_target(target, data_np.shape[0])
if error:
return error
solver_value = str(ridge_solver).strip().lower()
if solver_value not in {"auto", "svd", "lsqr", "sag", "saga"}:
return "Error: solver must be 'auto', 'svd', 'lsqr', 'sag', or 'saga'"
if float(alpha) < 0:
return "Error: alpha must be non-negative"
if float(tol) <= 0:
return "Error: tol must be greater than 0"
fitted = SklearnRidgeClassifier(
alpha=float(alpha),
solver=solver_value,
fit_intercept=bool(fit_intercept),
tol=float(tol),
random_state=None if random_state in (None, "") else int(random_state)
).fit(data_np, target_values)
prediction_array = fitted.predict(data_np)
predictions = [py(item) for item in prediction_array.tolist()]
classes = [py(item) for item in fitted.classes_.tolist()]
accuracy = float(np.mean([
type(prediction) is type(actual) and prediction == actual
for prediction, actual in zip(predictions, target_values)
]))
return {
"type": "Double",
"basicValue": accuracy,
"properties": {
"accuracy": {"type": "Double", "basicValue": accuracy},
"sample_count": {"type": "Double", "basicValue": float(data_np.shape[0])},
"feature_count": {"type": "Double", "basicValue": float(data_np.shape[1])},
"class_count": {"type": "Double", "basicValue": float(len(classes))},
"classes": {"type": "Array", "elements": col(classes)},
"predictions": {"type": "Array", "elements": col(predictions)},
"prediction_counts": {"type": "Array", "elements": count_table(predictions, classes)},
"decision_scores": {"type": "Array", "elements": mat(score_rows(fitted.decision_function(data_np)))},
"coefficients": {"type": "Array", "elements": mat(np.atleast_2d(fitted.coef_).tolist())},
"intercepts": {"type": "Array", "elements": col(np.atleast_1d(fitted.intercept_).tolist())}
}
}
except Exception as e:
return f"Error: {str(e)}"