sklearn_wrapper¶
Convert a trained scikit-learn model into a callable compatible with URANIE Relauncher.
uranie_evaluator(model_sklearn)
Parameters:
Parameter |
Type |
Description |
|---|---|---|
|
|
A trained scikit-learn estimator (e.g., |
Returns:
Type |
Description |
|---|---|
|
A Python callable that accepts |
Exceptions/Errors:
Returns
Noneand prints error message ifmodel_sklearnis not a valid scikit-learn estimatorReturns
Noneand prints error message if model has not been fitted (checked viacheck_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 raiseNotFittedError).
Supported Model Types¶
Works with all scikit-learn estimators that implement the .predict() method and pass check_is_fitted():
LinearRegression,Ridge,Lasso,ElasticNetSVR,KNeighborsRegressor,RandomForestRegressor,GradientBoostingRegressorLogisticRegression,SVC,RandomForestClassifier,GradientBoostingClassifierKNeighborsClassifier,DecisionTreeClassifier,GaussianNB,MLPClassifierKMeans,DBSCAN,AgglomerativeClustering,SpectralClustering
Supported Output Types¶
Scalar outputs (int, float)
1-D arrays, tuples, and dictionaries
Multi-dimensional tensors
Using
outputkeyparameter for multi-output models