sklearn_wrapper

Convert a trained scikit-learn model into a callable compatible with URANIE Relauncher.

uranie_evaluator(model_sklearn)

Parameters:

Parameter

Type

Description

model_sklearn

sklearn.base.BaseEstimator

A trained scikit-learn estimator (e.g., RandomForestRegressor, LogisticRegression, SVC). Must have been fitted using .fit() method.

Returns:

Type

Description

callable or None

A Python callable that accepts *args (floats) and returns a 1-D list of floats. Returns None if the model is not trained or arguments are invalid.

Exceptions/Errors:

  • Returns None and prints error message if model_sklearn is not a valid scikit-learn estimator

  • Returns None and prints error message if model has not been fitted (checked via check_is_fitted)

Examples

Regression example:

from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split

from uratools.sklearn_wrapper import uranie_evaluator

# Load data
X, y = load_diabetes(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train model
model = RandomForestRegressor(n_estimators=100, random_state=42)
model.fit(X_train, y_train)

# Wrap for URANIE
wrapped = uranie_evaluator(model)

if wrapped:
    # Test with first sample
    result = wrapped(*X_test[0])
    print(result)  # [predicted_value]

Classification example:

from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

from uratools.sklearn_wrapper import uranie_evaluator

# Load data
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(
    iris.data, iris.target, test_size=0.2, random_state=42
)

# Train model
model = LogisticRegression(max_iter=200, random_state=42)
model.fit(X_train, y_train)

# Wrap for URANIE
wrapped = uranie_evaluator(model)

if wrapped:
    result = wrapped(*X_test[0])
    print(f"Predicted class: {result[0]}")

Implementation Details

  • Input conversion: Arguments are passed to .predict() as a single-row NumPy array.

  • Output: For regression models, returns the predicted value. For classifiers with predict_proba, returns the class prediction (not probabilities).

  • Fitting check: Model is considered fitted if check_is_fitted() returns True (does not raise NotFittedError).

Supported Model Types

Works with all scikit-learn estimators that implement the .predict() method and pass check_is_fitted():

  • LinearRegression, Ridge, Lasso, ElasticNet

  • SVR, KNeighborsRegressor, RandomForestRegressor, GradientBoostingRegressor

  • LogisticRegression, SVC, RandomForestClassifier, GradientBoostingClassifier

  • KNeighborsClassifier, DecisionTreeClassifier, GaussianNB, MLPClassifier

  • KMeans, DBSCAN, AgglomerativeClustering, SpectralClustering

Supported Output Types

  • Scalar outputs (int, float)

  • 1-D arrays, tuples, and dictionaries

  • Multi-dimensional tensors

  • Using outputkey parameter for multi-output models