Exercises
Saving and Using a Model
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Save the trained pipeline, load it back, and predict with the loaded one.
Python needs scikit-learn, downloaded on first run
import joblib
from sklearn.datasets import load_iris
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).fit(X, y)
# save it, load it, and print a prediction
joblib.dump(model, path) then joblib.load(path).
import joblib
from sklearn.datasets import load_iris
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).fit(X, y)
joblib.dump(model, "iris.joblib")
loaded = joblib.load("iris.joblib")
print(loaded.predict([[5.1, 3.5, 1.4, 0.2]])[0])Exercise 2Passed
Only the estimator was saved, so the scaler is lost. Save the whole pipeline instead.
Python needs scikit-learn, downloaded on first run
import joblib
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X, y = load_iris(return_X_y=True)
scaler = StandardScaler().fit(X)
bare = LogisticRegression(max_iter=1000).fit(scaler.transform(X), y)
joblib.dump(bare, "model.joblib")
print(joblib.load("model.joblib").predict([[5.1, 3.5, 1.4, 0.2]])[0])make_pipeline keeps the scaler with the model.
import joblib
from sklearn.datasets import load_iris
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
X, y = load_iris(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).fit(X, y)
joblib.dump(model, "model.joblib")
print(joblib.load("model.joblib").predict([[5.1, 3.5, 1.4, 0.2]])[0])Exercise 3Passed
Save the model together with its feature names and class names, then print the classes from the loaded file.
Python needs scikit-learn, downloaded on first run
import joblib
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
data = load_iris()
model = LogisticRegression(max_iter=1000).fit(data.data, data.target)
# dump a dict holding the model, features and classes, then load and print the classes
joblib can save any Python object, including a dict.
import joblib
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
data = load_iris()
model = LogisticRegression(max_iter=1000).fit(data.data, data.target)
bundle = {
"model": model,
"features": list(data.feature_names),
"classes": [str(name) for name in data.target_names],
}
joblib.dump(bundle, "bundle.joblib")
print(joblib.load("bundle.joblib")["classes"])