Concepts 🚀 Project 🧮 Labs
🧮 Labs / Benchmark Engine / Load a .pkl model file (joblib + pickle)

Load a .pkl model file (joblib + pickle)

The first step of any benchmarking engine: load the model the user uploaded. joblib.load() and pickle.loads() both work.

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

Two ways to load a .pkl model: joblib.load('model.pkl') — loads from a file path. Faster for numpy arrays. pickle.loads(bytes) — loads from a bytes object. This is what you use when the model comes from an HTTP upload: `file_bytes = await upload_file.read()` then `model = pickle.loads(file_bytes)`. Both produce the same object — the trained model with all its coefficients. WARNING: pickle is unsafe for untrusted files — it can execute arbitrary code. In production, run pickle.loads in a sandbox.

✏️ Try these changes
  • Change make_regression n_features from 3 to 5
    → model trains with 5 features instead of 3
  • Change the save path from 'model.pkl' to 'my_model.pkl'
    → saves to a different file
  • Add a print(model.coef_) after loading
    → shows the learned coefficients
  • Replace LinearRegression with from sklearn.ensemble import RandomForestRegressor
    → different model type — same load code works