Concepts 🚀 Project 🧮 Labs
🧮 Labs / Benchmark Engine / Handle ANY model type (sklearn, xgboost, custom)

Handle ANY model type (sklearn, xgboost, custom)

A benchmarking engine must work with ANY .pkl — not just LinearRegression. Use duck typing: check for .predict().

script.py v3.1
Python output click Run to execute
Click "Run" to execute the code on the left.
The output will appear here.
🧠 How it works

A benchmarking engine MUST work with any model — not just the ones you trained. Use DUCK TYPING: check hasattr(model, 'predict') instead of isinstance(model, LinearRegression). Why? Because xgboost.XGBRegressor, lightgbm.LGBMRegressor, custom classes — all have .predict(). Don't care WHAT it is, care what it CAN DO. Detect regression vs classification by checking y_pred.dtype: float → regression (predict returns continuous values). int → classification (predict returns class labels). Also check for predict_proba — only classifiers have it. feature_importances_ → tree-based models (RF, XGBoost). coef_ → linear models (LinearRegression, LogisticRegression, SVM). This is how OpenBenchML handles ANY uploaded .pkl file.

✏️ Try these changes
  • Add a dummy class with no predict() method and try to benchmark it
    → shows the error handling
  • Change the regression detection to also check predict_proba
    → classifiers have predict_proba, regressors don't
  • Add from sklearn.cluster import KMeans; joblib.dump(KMeans(n_clusters=3).fit(Xr), 'kmeans.pkl'); benchmark_any_model('kmeans.pkl', Xr[:10])
    → KMeans has predict() but it's clustering, not classification
  • Print model.get_params() if the model has it
    → shows hyperparameters