Docsity

Impact of Climate Change on Agriculture: Adaptation Strategies and Technologies, Monografías, Ensayos de Métodos de Investigación para Administradores

Vista previa parcial del texto

¡Descarga Impact of Climate Change on Agriculture: Adaptation Strategies and Technologies y más Monografías, Ensayos en PDF de Métodos de Investigación para Administradores solo en Docsity!

Machine Learning

with Python

for Everyone

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.

The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.

For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at [email protected] or (800) 382-3419.

For government sales inquiries, please contact [email protected].

For questions about sales outside the U.S., please contact [email protected].

Visit us on the Web: informit.com/aw

Library of Congress Control Number: 2019938761

Copyright © 2020 Pearson Education, Inc.

Cover image: cono0430/Shutterstock

Pages 58, 87: Screenshot of seaborn © 2012–2018 Michael Waskom. Pages 167, 177, 192, 201, 278, 284, 479, 493: Screenshot of seaborn heatmap © 2012–2018 Michael Waskom. Pages 178, 185, 196, 197, 327, 328: Screenshot of seaborn swarmplot © 2012–2018 Michael Waskom. Page 222: Screenshot of seaborn stripplot © 2012–2018 Michael Waskom. Pages 351, 354: Screenshot of seaborn implot © 2012–2018 Michael Waskom. Pages 352, 353, 355: Screenshot of seaborn distplot © 2012–2018 Michael Waskom. Pages 460, 461: Screenshot of Manifold © 2007–2018, scikit-learn developers. Page 480: Screenshot of cluster © 2007–2018, scikit-learn developers. Pages 483, 484, 485: Image of accordion, Vereshchagin Dmitry/Shutterstock. Page 485: Image of fighter jet, 3dgenerator/123RF. Page 525: Screenshot of seaborn jointplot © 2012–2018 Michael Waskom.

All rights reserved. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/.

ISBN-13: 978-0-13-484562- ISBN-10: 0-13-484562-

ScoutAutomatedPrintCode

To my son, Ethan—

with the eternal hope of a better tomorrow

Contents

Foreword xxi

Preface xxiii

About the Author xxvii

I First Steps 1

1 Let’s Discuss Learning 3 1.1 Welcome 3 1.2 Scope, Terminology, Prediction, and Data 4 1.2.1 Features 5 1.2.2 Target Values and Predictions 6 1.3 Putting the Machine in Machine Learning 7 1.4 Examples of Learning Systems 9 1.4.1 Predicting Categories: Examples of Classifiers 9 1.4.2 Predicting Values: Examples of Regressors 10 1.5 Evaluating Learning Systems 11 1.5.1 Correctness 11 1.5.2 Resource Consumption 12 1.6 A Process for Building Learning Systems 13 1.7 Assumptions and Reality of Learning 15 1.8 End-of-Chapter Material 17 1.8.1 The Road Ahead 17 1.8.2 Notes 17

2 Some Technical Background 19 2.1 About Our Setup 19 2.2 The Need for Mathematical Language 19

viii Contents

3.5 Simple Classifier #1:

x Contents

4.4.5 Application to Linear Regression 101 4.5 Simple Evaluation and Comparison of Regressors 101 4.5.1 Root Mean Squared Error 101 4.5.2 Learning Performance 102 4.5.3 Resource Utilization in Regression 102 4.6 EOC 104 4.6.1 Limitations and Open Issues 104 4.6.2 Summary 105 4.6.3 Notes 105 4.6.4 Exercises 105

II Evaluation 107

5 Evaluating and Comparing Learners 109 5.1 Evaluation and Why Less Is More 109 5.2 Terminology for Learning Phases 110 5.2.1 Back to the Machines 110 5.2.2 More Technically Speaking... 113 5.3 Major Tom, There’s Something Wrong: Overfitting and Underfitting 116 5.3.1 Synthetic Data and Linear Regression 117 5.3.2 Manually Manipulating Model Complexity 118 5.3.3 Goldilocks: Visualizing Overfitting, Underfitting, and “Just Right” 120 5.3.4 Simplicity 124 5.3.5 Take-Home Notes on Overfitting 124 5.4 From Errors to Costs 125 5.4.1 Loss 125 5.4.2 Cost 126

Contents xi

5.4.3 Score 127 5.5 (Re)Sampling: Making More from Less 128 5.5.1 Cross-Validation 128 5.5.2 Stratification 132 5.5.3 Repeated Train-Test Splits 133 5.5.4 A Better Way and Shuffling 137 5.5.5 Leave-One-Out Cross-Validation 140 5.6 Break-It-Down: Deconstructing Error into Bias and Variance 142 5.6.1 Variance of the Data 143 5.6.2 Variance of the Model 144 5.6.3 Bias of the Model 144 5.6.4 All Together Now 145 5.6.5 Examples of Bias-Variance Tradeoffs 145 5.7 Graphical Evaluation and Comparison 149 5.7.1 Learning Curves: How Much Data Do We Need? 150 5.7.2 Complexity Curves 152 5.8 Comparing Learners with Cross-Validation 154 5.9 EOC 155 5.9.1 Summary 155 5.9.2 Notes 155 5.9.3 Exercises 157

6 Evaluating Classifiers 159 6.1 Baseline Classifiers 159 6.2 Beyond Accuracy: Metrics for Classification 161 6.2.1 Eliminating Confusion from the Confusion Matrix 163 6.2.2 Ways of Being Wrong 164 6.2.3 Metrics from the Confusion Matrix 165 6.2.4 Coding the Confusion Matrix 166 6.2.5 Dealing with Multiple Classes: Multiclass Averaging 168

Contents xiii

xiv Contents

8.5 Discriminant Analysis 269 8.5.1 Covariance 270 8.5.2 The Methods 282 8.5.3 Performing DA 283 8.6 Assumptions, Biases, and Classifiers 285 8.7 Comparison of Classifiers: Take Three 287 8.7.1 Digits 287 8.8 EOC 290 8.8.1 Summary 290 8.8.2 Notes 290 8.8.3 Exercises 293

9 More Regression Methods 295 9.1 Linear Regression in the Penalty Box: Regularization 295 9.1.1 Performing Regularized Regression 300 9.2 Support Vector Regression 301 9.2.1 Hinge Loss 301 9.2.2 From Linear Regression to Regularized Regression to Support Vector Regression 305 9.2.3 Just Do It—SVR Style 307 9.3 Piecewise Constant Regression 308 9.3.1 Implementing a Piecewise Constant Regressor 310 9.3.2 General Notes on Implementing Models 311 9.4 Regression Trees 313 9.4.1 Performing Regression with Trees 313 9.5 Comparison of Regressors: Take Three 314 9.6 EOC 318 9.6.1 Summary 318 9.6.2 Notes 318 9.6.3 Exercises 319

xvi Contents

Contents xvii

13 Models That Engineer Features for Us 409 13.1 Feature Selection 411 13.1.1 Single-Step Filtering with Metric-Based Feature Selection 412 13.1.2 Model-Based Feature Selection 423 13.1.3 Integrating Feature Selection with a Learning Pipeline 426 13.2 Feature Construction with Kernels 428 13.2.1 A Kernel Motivator 428 13.2.2 Manual Kernel Methods 433 13.2.3 Kernel Methods and Kernel Options 438 13.2.4 Kernelized SVCs: SVMs 442 13.2.5 Take-Home Notes on SVM and an Example 443 13.3 Principal Components Analysis: An Unsupervised Technique 445 13.3.1 A Warm Up: Centering 445 13.3.2 Finding a Different Best Line 448 13.3.3 A First PCA 449 13.3.4 Under the Hood of PCA 452 13.3.5 A Finale: Comments on General PCA 457 13.3.6 Kernel PCA and Manifold Methods 458 13.4 EOC 462 13.4.1 Summary 462 13.4.2 Notes 462 13.4.3 Exercises 467

14 Feature Engineering for Domains: Domain-Specific Learning 469 14.1 Working with Text 470 14.1.1 Encoding Text 471 14.1.2 Example of Text Learning 476 14.2 Clustering 479 14.2.1 k -Means Clustering 479

Contents xix

  • Learning 2.3 Our Software for Tackling Machine
  • 2.4 Probability
    • 2.4.1 Primitive Events
    • 2.4.2 Independence
    • 2.4.3 Conditional Probability
    • 2.4.4 Distributions
    • and Dot Products 2.5 Linear Combinations, Weighted Sums,
    • 2.5.1 Weighted Average
    • 2.5.2 Sums of Squares
    • 2.5.3 Sum of Squared Errors
    • Space 2.6 A Geometric View: Points in
    • 2.6.1 Lines
    • 2.6.2 Beyond Lines
  • 2.7 Notation and the Plus-One Trick
    • Straight-Jacket, and Nonlinearity 2.8 Getting Groovy, Breaking the
  • 2.9 NumPy versus “All the Maths”
    • 2.9.1 Back to 1D versus 2D
  • 2.10 Floating-Point Issues
  • 2.11 EOC
    • 2.11.1 Summary
    • 2.11.2 Notes
  • with Classification 3 Predicting Categories: Getting Started
  • 3.1 Classification Tasks
  • 3.2 A Simple Classification Dataset
    • to the Test 3.3 Training and Testing: Don’t Teach
  • 3.4 Evaluation: Grading the Exam
    • Relationships, and Assumptions Nearest Neighbors, Long Distance
    • 3.5.1 Defining Similarity
    • 3.5.2 The k in k -NN
    • 3.5.3 Answer Combination
      • 7.3 Residual Plots - 7.3.1 Error Plots - 7.3.2 Residual Plots
      • 7.4 A First Look at Standardization
        • Sophisticated Way: Take Two 7.5 Evaluating Regressors in a More - Multiple Metrics 7.5.1 Cross-Validated Results on - Results 7.5.2 Summarizing Cross-Validated
          • 7.5.3 Residuals
      • 7.6 EOC - 7.6.1 Summary - 7.6.2 Notes - 7.6.3 Exercises
  • III More Methods and Fundamentals
    • 8 More Classification Methods
      • 8.1 Revisiting Classification
      • 8.2 Decision Trees - 8.2.1 Tree-Building Algorithms - 8.2.2 Let’s Go: Decision Tree Time - Trees 8.2.3 Bias and Variance in Decision
      • 8.3 Support Vector Classifiers - 8.3.1 Performing SVC - 8.3.2 Bias and Variance in SVCs
      • 8.4 Logistic Regression - 8.4.1 Betting Odds - Log-Odds 8.4.2 Probabilities, Odds, and - Edition 8.4.3 Just Do It: Logistic Regression - Oddity 8.4.4 A Logistic Regression: A Space
        • Nested Cross-Validation 11.3 Down the Recursive Rabbit Hole:
        • 11.3.1 Cross-Validation, Redux
        • 11.3.2 GridSearch as a Model
          • within Cross-Validation 11.3.3 Cross-Validation Nested
          • CV 11.3.4 Comments on Nested
      • 11.4 Pipelines
        • 11.4.1 A Simple Pipeline
          • Pipeline 11.4.2 A More Complex
      • 11.5 Pipelines and Tuning Together
      • 11.6 EOC
        • 11.6.1 Summary
        • 11.6.2 Notes
        • 11.6.3 Exercises
  • IV Adding Complexity
    • 12 Combining Learners
      • 12.1 Ensembles
      • 12.2 Voting Ensembles
      • 12.3 Bagging and Random Forests
        • 12.3.1 Bootstrapping
          • Bagging 12.3.2 From Bootstrapping to
          • Forest 12.3.3 Through the Random
      • 12.4 Boosting
        • 12.4.1 Boosting Details
        • Methods 12.5 Comparing the Tree-Ensemble
      • 12.6 EOC
        • 12.6.1 Summary
        • 12.6.2 Notes
        • 12.6.3 Exercises
        • Regression 15.6.3 A PGM View of Logistic
    • 15.7 EOC
      • 15.7.1 Summary
      • 15.7.2 Notes
      • 15.7.3 Exercises
  • A mlwpy.py Listing
  • Index

This page intentionally left blank