Chapter 11. Model Evaluation
11.0 Introduction
In this chapter we will examine strategies for evaluating the quality of models created through our learning algorithms. It might appear strange to discuss model evaluation before discussing how to create them, but there is a method to our madness. Models are only as useful as the quality of their predictions, and thus, fundamentally, our goal is not to create models (which is easy) but to create high-quality models (which is hard). Therefore, before we explore the myriad learning algorithms, let’s first learn how we can evaluate the models they produce.
11.1 Cross-Validating Models
Problem
You want to evaluate how well your classification model generalizes to unforeseen data.
Solution
Create a pipeline that preprocesses the data, trains the model, and then evaluates it using cross-validation:
# Load librariesfromsklearnimportdatasetsfromsklearnimportmetricsfromsklearn.model_selectionimportKFold,cross_val_scorefromsklearn.pipelineimportmake_pipelinefromsklearn.linear_modelimportLogisticRegressionfromsklearn.preprocessingimportStandardScaler# Load digits datasetdigits=datasets.load_digits()# Create features matrixfeatures=digits.data# Create target vectortarget=digits.target# Create standardizerstandardizer=StandardScaler()# Create logistic regression objectlogit=LogisticRegression()# Create a pipeline that standardizes, then runs logistic regressionpipeline=make_pipeline(standardizer ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access