GBSVR implements a Support Vector Regression algorithm accelerated and robustified by data reduction using granular balls.
pip install git+https://github.com/ankushbisht01/gbsvr.git
import numpy as np
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import r2_score
import time
from gbsvr import GBSVR
from gbsvr.config import DEFAULT_CONFIG as default_params # optional if you define defaults here
try:
from uci_datasets import Dataset
except ImportError:
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/treforevans/uci_datasets.git"])
from uci_datasets import Dataset
def set_seed(seed=0):
np.random.seed(seed)
set_seed(0)
def evaluate(y_true, y_pred):
y_true = y_true.flatten()
y_pred = y_pred.flatten()
mse = np.mean((y_true - y_pred) ** 2)
rmse = np.sqrt(mse)
r2 = r2_score(y_true, y_pred)
return r2, mse, rmse
def run_gbsvr_cv(dataset_name="autompg", fold=5):
# Load dataset
data = Dataset(dataset_name)
X = data.x
Y = data.y
print(f"Dataset: {dataset_name}, X shape: {X.shape}, Y shape: {Y.shape}")
# Parameters
C = 10
epsilon = 1e-5
pur = 0.997
num = 2
kernelparam = 0.003
n_bins = 6
# # Normalize
# X = StandardScaler().fit_transform(X)
# Y = StandardScaler().fit_transform(Y.reshape(-1, 1))
# Cross-validation
kf = KFold(n_splits=fold, shuffle=True, random_state=0)
all_scores = []
for fold_idx, (train_idx, test_idx) in enumerate(kf.split(X)):
X_train, X_test = X[train_idx], X[test_idx]
Y_train, Y_test = Y[train_idx], Y[test_idx]
#scale the data with training on X_train and Y_train only
X_scaler = StandardScaler().fit(X_train)
Y_scaler = StandardScaler().fit(Y_train.reshape(-1, 1))
X_train = X_scaler.transform(X_train)
X_test = X_scaler.transform(X_test)
Y_train = Y_scaler.transform(Y_train.reshape(-1, 1)).flatten()
Y_test = Y_scaler.transform(Y_test.reshape(-1, 1)).flatten()
# Initialize GBSVR
gbsvr = GBSVR(C=C, epsilon=epsilon, pur=pur, num=num, kernelparam=kernelparam, n_bins=n_bins)
# Fit
start_time = time.time()
gbsvr.fit(X_train, Y_train)
duration = time.time() - start_time
# Predict
y_pred = gbsvr.predict(X_test)
r2, mse, rmse = evaluate(Y_test, y_pred)
print(f"Fold {fold_idx + 1}: R2 = {r2:.4f}, MSE = {mse:.4f}, RMSE = {rmse:.4f}, Time = {duration:.2f}s")
all_scores.append((r2, mse, rmse, duration))
# Mean Results
all_scores = np.array(all_scores)
mean_scores = np.mean(all_scores, axis=0)
std_scores = np.std(all_scores, axis=0)
print("\n===== Average Results =====")
print(f"R2: {mean_scores[0]:.4f} ± {std_scores[0]:.4f}")
print(f"MSE: {mean_scores[1]:.4f} ± {std_scores[1]:.4f}")
print(f"RMSE: {mean_scores[2]:.4f} ± {std_scores[2]:.4f}")
print(f"Time: {mean_scores[3]:.2f}s ± {std_scores[3]:.2f}s")
# Save results to JSON file
scores = {"GBSVR": all_scores.tolist()}
return scores
if __name__ == "__main__":
run_gbsvr_cv("machine", fold=5)| Name | Type | Default | Description |
|---|---|---|---|
| C | float | 10 | Penalty parameter of the error term. |
| epsilon | float | 1e-3 | Epsilon-tube width in ε-SVR. |
| pur | float | 0.997 | Purity threshold for granular ball splitting. |
| num | int | 2 | Minimum points to form a granular ball. |
| kernel | str | "rbf" | Kernel type (“rbf”, “linear”, etc.). |
| kernelparam | float | 0.003 | Kernel hyperparameter (e.g., gamma for RBF). |
| n_bins | int | 6 | Number of bins for target discretization. |
| tol | float | 1e-3 | Tolerance for the optimization solver. |
X_train_fit_,Y_train_fit_: Stored training dataCenters_,Radii_: Granular ball geometrytarget_balls_: Target values per ballalpha_,alpha_star_: Lagrange multipliersW_,bias_: Model coefficientsobjective_value_: Final objective valuefit_time_: Training durationn_balls_: Number of granular ballsoptim_success_,optim_message_: Solver status
.fit(X, Y).predict(X_test).fit_balls(Centers, Radii, target_balls).get_params(),.set_params()— sklearn compatibility
- GBSVR: Granular Ball Support Vector Regression