Page
1
(This page has no text content)
Page
2
(This page has no text content)
Page
4
(This page has no text content)
Page
5
Python® & AI by John C. Shovic, PhD, and Mary “Marz” Everett, PhD
Page
6
Python® & AI For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2026 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies. Media and software compilation copyright © 2026 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies. No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. The manufacturer’s authorized representative according to the EU General Product Safety Regulation is Wiley-VCH GmbH, Boschstr. 12, 69469 Weinheim, Germany, e-mail: Product_Safety@wiley.com. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. Python is a trademark or registered trademark of the Python Software Foundation. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media that is not included in the version you purchased, you may download this material at http://booksupport. wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number is available from the publisher. ISBN 978-1-394-41346-1 (pbk); ISBN 978-1-394-41348-5 (ebk); ISBN 978-1-394-41347-8 (ebk)
Page
7
Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Part 1: Getting Started with Python and AI . . . . . . . . . . . . . . . . . . . 5 CHAPTER 1: Starting with Python Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 2: Defining Fundamental AI Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 CHAPTER 3: Working with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Part 2: Fundamental Machine Learning Models . . . . . . . . . . . . 59 CHAPTER 4: Working with the scikit-learn Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 CHAPTER 5: The Plot Thickens: Regression Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 CHAPTER 6: Making Decisions with Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 CHAPTER 7: Better Together: Boosting, Bagging, and Random Forests . . . . . . . . . . 103 Part 3: The Neural Network Family . . . . . . . . . . . . . . . . . . . . . . . . . . 119 CHAPTER 8: Meeting Your Brain: Neural Network Architectures . . . . . . . . . . . . . . . . 121 CHAPTER 9: The Not-So-Scary Math That Powers Neural Networks . . . . . . . . . . . . . 137 CHAPTER 10: Training a Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 CHAPTER 11: Identifying the Neural Network Family . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Part 4: Machine Vision with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . 197 CHAPTER 12: Introducing Machine Vision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 CHAPTER 13: Getting Started with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 CHAPTER 14: Classifying Images with Convolutional Neural Networks . . . . . . . . . . . 229 CHAPTER 15: Creating an Object Detector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Part 5: Advanced AI Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 CHAPTER 16: Large and in Charge: LLMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 CHAPTER 17: Generating Stuff with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 CHAPTER 18: AI Agents: The Ghosts in Our Machines . . . . . . . . . . . . . . . . . . . . . . . . . . 307 CHAPTER 19: Improving Your Computer with Reinforcement Learning . . . . . . . . . . . 323 CHAPTER 20: Nature Knows Best: Using Evolutionary Algorithms . . . . . . . . . . . . . . . . 339
Page
8
Part 6: The Part of Tens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 CHAPTER 21: Ten Steps for Developing an AI Project . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 CHAPTER 22: Ten Common AI Mistakes to Avoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Appendix: Setting Up Your Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Page
9
Table of Contents vii Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 PART 1: GETTING STARTED WITH PYTHON AND AI . . . . . . . . . 5 CHAPTER 1: Starting with Python Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Using Python to Work with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 What Is Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8 Why use Python with AI? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Setting Up a Python Programming Environment . . . . . . . . . . . . . . . . . .10 Text editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Python interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11 Integrated development environment . . . . . . . . . . . . . . . . . . . . . . . .12 First-Time Python Programming Gotchas . . . . . . . . . . . . . . . . . . . . . . . .13 Understanding the terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Using different virtual environments . . . . . . . . . . . . . . . . . . . . . . . . .15 Installing Python packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 Creating a Virtual Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17 Breaking Down the VS Code Interface . . . . . . . . . . . . . . . . . . . . . . . . . . .18 Creating a new file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 Selecting the Python interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Opening a terminal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 A more advanced program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 CHAPTER 2: Defining Fundamental AI Concepts . . . . . . . . . . . . . . . . . . 29 Defining AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 Expert systems or rule-based approaches . . . . . . . . . . . . . . . . . . . .31 Machine learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31 How Big Data Gave Rise to Machine Learning . . . . . . . . . . . . . . . . . . . .34 Breaking Down Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Input data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Internal parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
Page
10
viii Python & AI For Dummies Output data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Correct answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Evaluating and Improving a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Getting Started in the AI World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 CHAPTER 3: Working with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Preparing Data for a Machine Learning Model . . . . . . . . . . . . . . . . . . . .41 Explaining variable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 Cute and cuddly: Installing the pandas library . . . . . . . . . . . . . . . . .43 Describing the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Accessing and Filtering the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Handling Missing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50 Visualizing the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Trusting the Experts: The Importance of Domain Knowledge . . . . . . .56 PART 2: FUNDAMENTAL MACHINE LEARNING MODELS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 CHAPTER 4: Working with the scikit-learn Library . . . . . . . . . . . . . . . . 61 An Overview of scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62 Installing scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Preprocessing Data with scikit-learn . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Encoding outputs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Scaling data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Dividing Data into Training, Validation, and Test Sets . . . . . . . . . . . . . .68 Classification, Regression, and Clustering, Oh My! . . . . . . . . . . . . . . . . .71 Using the scikit-learn Model API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 CHAPTER 5: The Plot Thickens: Regression Models . . . . . . . . . . . . . . . 77 Taking a Look at a Classic: The Linear Regression Model . . . . . . . . . . .78 Reviewing the equation of a line . . . . . . . . . . . . . . . . . . . . . . . . . . . . .78 Modeling linear regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 Exploring ordinary least squares . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Calculating the coefficient of determination . . . . . . . . . . . . . . . . . . .81 Implementing Linear Regression Manually in Python . . . . . . . . . . . . . .82 Implementing Linear Regression in scikit-learn . . . . . . . . . . . . . . . . . . .85 Not Forgetting Other Regression Models . . . . . . . . . . . . . . . . . . . . . . . .87 Multiple regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Logistic regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88 Ridge and lasso regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88 Polynomial regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88
Page
11
Table of Contents ix CHAPTER 6: Making Decisions with Trees . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Exploring the Beautiful Flowchart of a Decision Tree . . . . . . . . . . . . . .90 Understanding Information Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Outlining entropy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Gaining more information through entropy . . . . . . . . . . . . . . . . . . .94 Gini in a bottle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Programming a Decision Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Explaining Important Decision-Tree Parameters . . . . . . . . . . . . . . . . . .98 CHAPTER 7: Better Together: Boosting, Bagging, and Random Forests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Two Heads Are Better than One: Ensemble Models . . . . . . . . . . . . . .104 Bragging about Your Bagging Model . . . . . . . . . . . . . . . . . . . . . . . . . . .105 Choosing data subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106 Bagging model parameters in scikit-learn . . . . . . . . . . . . . . . . . . . .107 Implementing a bagging model in Python . . . . . . . . . . . . . . . . . . . .108 Boosting Your Models for Liftoff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110 Types of boosting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110 Implementing a boosting model in Python . . . . . . . . . . . . . . . . . . .111 Random Forest: Your scikit-learn Powerhouse . . . . . . . . . . . . . . . . . . .113 Implementing a random forest model in Python . . . . . . . . . . . . . .114 Important random forest parameters . . . . . . . . . . . . . . . . . . . . . . .115 When 100 Percent Accuracy Is 100 Percent Wrong . . . . . . . . . . . . . . .116 It Works Well, But I Have No Idea Why . . . . . . . . . . . . . . . . . . . . . . . . . .117 PART 3: THE NEURAL NETWORK FAMILY . . . . . . . . . . . . . . . . . . . 119 CHAPTER 8: Meeting Your Brain: Neural Network Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Examining the Biological Concepts of Neural Networks . . . . . . . . . . .122 Neurons: The basic building blocks . . . . . . . . . . . . . . . . . . . . . . . . .122 Neural network: Sensory architecture . . . . . . . . . . . . . . . . . . . . . . .123 Neural connections: Training your brain . . . . . . . . . . . . . . . . . . . . .124 Exploring the Computer Science Model of Neural Networks . . . . . . .125 Grouping neurons together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Connecting your neural network . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Transforming data in the feed-forward phase . . . . . . . . . . . . . . . .131 CHAPTER 9: The Not-So-Scary Math That Powers Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Choosing the Right Activation and Error Functions . . . . . . . . . . . . . . .138 Activation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Error functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140
Page
12
x Python & AI For Dummies Creating Meaningful Change with Gradient Descent . . . . . . . . . . . . . .143 Error surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 Local and global minimums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Types of gradient descent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .146 Demystifying Backpropagation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 A weight’s magical journey to error . . . . . . . . . . . . . . . . . . . . . . . . .148 The Three Musketeers of backpropagation . . . . . . . . . . . . . . . . . . .148 A mathematical backpropagation example . . . . . . . . . . . . . . . . . . .149 CHAPTER 10: Training a Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Avoiding Data Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 Validating Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 Overfitting and Underfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162 Setting Neural Network Parameters and Hyperparameters . . . . . . .163 Architecting neural networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163 Hyperparameter tuning techniques . . . . . . . . . . . . . . . . . . . . . . . . .164 Creating and Training a Neural Network . . . . . . . . . . . . . . . . . . . . . . . .164 Preparing your dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165 Training a neural network with Keras . . . . . . . . . . . . . . . . . . . . . . . .167 Training a neural network with PyTorch . . . . . . . . . . . . . . . . . . . . .172 CHAPTER 11: Identifying the Neural Network Family . . . . . . . . . . . . 181 Exploring the Neural Network Family Tree . . . . . . . . . . . . . . . . . . . . . .182 Building Basic Feed-Forward Networks . . . . . . . . . . . . . . . . . . . . . . . . .182 Creating Convolutional Neural Networks . . . . . . . . . . . . . . . . . . . . . . .183 ImageNet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184 Yolo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 Fully convolutional neural networks . . . . . . . . . . . . . . . . . . . . . . . . .186 Running Recurrent Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . .186 Analyzing Autoencoders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 Attention, Please! Transformer Models . . . . . . . . . . . . . . . . . . . . . . . . .190 Using Multiple Architectures in One Network . . . . . . . . . . . . . . . . . . . .191 Convolutional layers in other networks . . . . . . . . . . . . . . . . . . . . . .191 Encoders and decoders in other networks . . . . . . . . . . . . . . . . . . .192 Hybrid architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192 PART 4: MACHINE VISION WITH OpenCV . . . . . . . . . . . . . . . . . . . 197 CHAPTER 12: Introducing Machine Vision . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Your Personal Vision System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Comparing Biological Vision with Camera Vision . . . . . . . . . . . . . . . . .201 Focus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201 Aperture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .201
Page
13
Table of Contents xi Shutter speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 Color interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 Depth perception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 Information processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202 Exploring Common Use Cases for Machine Vision . . . . . . . . . . . . . . . .203 Considering Machine Vision Data Needs . . . . . . . . . . . . . . . . . . . . . . . .204 Uncovering Potential Machine Vision Pitfalls . . . . . . . . . . . . . . . . . . . .205 Lighting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Coverage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Ethical concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 “Hello Visual World” with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206 What is OpenCV? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206 Writing your first OpenCV program . . . . . . . . . . . . . . . . . . . . . . . . .207 Writing your second OpenCV program . . . . . . . . . . . . . . . . . . . . . .209 Troubleshooting the OpenCV toolchain . . . . . . . . . . . . . . . . . . . . . .210 CHAPTER 13: Getting Started with OpenCV . . . . . . . . . . . . . . . . . . . . . . . . 213 Your Image Is in the Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 Manipulating Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215 Color Spaces and Blurry Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 Reviewing Kernel Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Finding the Image Gradient . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .223 Refining with Canny Edge Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 From Image Processing to AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 CHAPTER 14: Classifying Images with Convolutional Neural Networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Exploring the AI Machine Vision Task Hierarchy . . . . . . . . . . . . . . . . . .230 Architecting a Convolutional Neural Network . . . . . . . . . . . . . . . . . . . .231 Transfer learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 Common CNN models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235 Training a Vision Classification Model in Keras . . . . . . . . . . . . . . . . . . .235 CHAPTER 15: Creating an Object Detector . . . . . . . . . . . . . . . . . . . . . . . . . . 241 Collecting Your Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242 Annotating Data with Free Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244 Organizing your images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .244 Creating a free CVAT account . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .245 Labeling your images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .248 Downloading your labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250 Rinse and repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Adding the data.yaml file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252
Page
14
xii Python & AI For Dummies Coding with Google Colab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 Connecting to a more advanced runtime . . . . . . . . . . . . . . . . . . . .254 The model training program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259 Implementing Your Model in Real Time . . . . . . . . . . . . . . . . . . . . . . . . .260 PART 5: ADVANCED AI MODELS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 CHAPTER 16: Large and in Charge: LLMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Building the House: Architectures of LLMs . . . . . . . . . . . . . . . . . . . . . .268 Transforming the Attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271 How LLMs Rocked the World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272 Using an LLM via an API in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 Training Your Own LLM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .279 Being Mindful of the Limitations of LLMs . . . . . . . . . . . . . . . . . . . . . . .280 CHAPTER 17: Generating Stuff with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Identifying the Types of Generative AI . . . . . . . . . . . . . . . . . . . . . . . . . .283 Exploring Generative Adversarial Networks . . . . . . . . . . . . . . . . . . . . .284 Making Sense of Data with Variational Autoencoders . . . . . . . . . . . . .291 Imaging with Diffusion Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .298 Generating Code with Transformer Models . . . . . . . . . . . . . . . . . . . . .302 Last Words on Generative AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305 CHAPTER 18: AI Agents: The Ghosts in Our Machines . . . . . . . . . . . . 307 Separating Agentic AI from Traditional AI . . . . . . . . . . . . . . . . . . . . . . .307 Traditional AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308 Agentic AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .309 Agentic RAG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .311 Building Simple AI Agents in Python . . . . . . . . . . . . . . . . . . . . . . . . . . . .312 Building an AI web-scraping agent . . . . . . . . . . . . . . . . . . . . . . . . . .312 Building an AI RAG agent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .316 Considering the Limitations of AI Agents . . . . . . . . . . . . . . . . . . . . . . . .320 CHAPTER 19: Improving Your Computer with Reinforcement Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Learning about Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324 Getting Feedback: Reinforcement Learning . . . . . . . . . . . . . . . . . . . . .325 Defining Policy with a Markov Chain . . . . . . . . . . . . . . . . . . . . . . . . . . .327 Optimizing Policy with Gradients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .329 Exploring Reinforcement Learning in Python . . . . . . . . . . . . . . . . . . . .330
Page
15
Table of Contents xiii CHAPTER 20: Nature Knows Best: Using Evolutionary Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Evolving Your Idea of Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . .339 Optimizing Search with a Genetic Algorithm . . . . . . . . . . . . . . . . . . . . .342 Parameterizing a Genetic Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . .344 Population initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .344 Population scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .345 Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .346 Mutation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347 Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .347 Implementing a Genetic Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . .348 Wrapping Up Your Evolutionary Computation Journey . . . . . . . . . . . .356 PART 6: THE PART OF TENS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 CHAPTER 21: Ten Steps for Developing an AI Project . . . . . . . . . . . . . 359 Step 1: Learn from the Domain Experts . . . . . . . . . . . . . . . . . . . . . . . . .359 Step 2: Define the Problem Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .360 Step 3: Sort Problems by Feasibility and Impact . . . . . . . . . . . . . . . . . .360 Step 4: Scope Out the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361 Step 5: Collect the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361 Step 6: Explore the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .362 Step 7: Choose Appropriate Model Types . . . . . . . . . . . . . . . . . . . . . . .362 Step 8: Choose Your Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .362 Step 9: Iterate, Iterate, Iterate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363 Step 10: Make Your Outputs Count . . . . . . . . . . . . . . . . . . . . . . . . . . . .363 CHAPTER 22: Ten Common AI Mistakes to Avoid . . . . . . . . . . . . . . . . . . 365 Skipping Data Exploration and Quality Checks . . . . . . . . . . . . . . . . . . .366 Using the Wrong Evaluation Metrics . . . . . . . . . . . . . . . . . . . . . . . . . . .366 Overfitting Your Model to Training Data . . . . . . . . . . . . . . . . . . . . . . . .367 Ignoring Time-Series Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . .367 Choosing Complex Models When Simple Models Work . . . . . . . . . . .368 Trying Only One Set of Model Hyperparameters . . . . . . . . . . . . . . . . .368 Forgetting to Consult with Domain Experts . . . . . . . . . . . . . . . . . . . . .369 Not Quantifying a Model’s Impact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369 Deploying Models without Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . .370 Ignoring Ethical and Bias Considerations . . . . . . . . . . . . . . . . . . . . . . .370 APPENDIX: SETTING UP YOUR DEVELOPMENT ENVIRONMENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Page
16
(This page has no text content)
Page
17
Introduction 1 Introduction Welcome, AI aficionados! If you picked up this book, you’re probably aware that the field of artificial intelligence (AI) is enormous and omni- present. Its abilities are sometimes magical, sometimes bewildering, and sometimes completely incomprehensible. Many AI tools are concentrated in one particular snake-named programming lan- guage: Python. Whether you are interested in Python and want to level up your AI skills, or are interested in AI but have no clue how to start programming for it, this book gives you the tools you need. We cover many different aspects of AI (data, models, and training) and provide hands-on Python programs for you to test them out. We draw from our experience in teaching and research at a university robotics center, where we use Python and AI every day. We’ve been spending the last several years in our careers applying AI solutions to problems primarily in the manufacturing and agriculture industries, and we’ve learned a lot along the way. AI isn’t just a large language model app on your phone that can helpfully assure you no hospital is needed when you accidentally brush your teeth with anti-itch cream (long story). AI encompasses a wide range of models, techniques, processes, and search methods that suit an array of problems. Not every technique is a solution for every scenario, though. We put our experi- ence to work when explaining how certain models fit certain situations better. About This Book This book is intended to be a reference for implementing the full AI toolchain in the Python programming language. We devote chapters to every part of the pro- cess, including Python fundamentals, manipulating and formatting data, install- ing and using AI libraries, and evaluating the models and algorithms. John says that while he did his doctoral dissertation on the AI technique of expert systems, he is thrilled to be obsoleted by these new techniques in this book. Really. This book isn’t intended to be comprehensive — there are many more AI tech- niques than are dreamt of in our philosophy! — but it is intended to give you the essential skills and point you to places where you can learn more. This book pro- vides an overview of many different kinds of AI algorithms, but users often
Page
18
2 Python & AI For Dummies gravitate toward one type of model or another. Your new favorite technique may only be a few chapters away. (We’ll give you a hint: How about those genetic algorithms?) If you’re new to programming, it’s okay to read the book in order, as we start with programming and AI basics and gradually implement more complex models. But you can just as easily get comfortable in one section and skip over to another. Absolutely zero judgment if no universe exists where you care about linear regres- sion. Seriously. Getting “dangerous” in programming AI techniques with Python can happen surprisingly fast — no prior experience required. That being said, as with any new skills, mastery takes persistence and a willingness to try and try again. This book gives you a solid starting point for beginning an AI journey, but don’t be discour- aged if you break a few things on the way to success. Marz spent her first day of graduate school blowing up a robot, which was perhaps not the best start to her career. Running into problems is part of the process — but we’ve included plenty of troubleshooting tips throughout the book to help you if you get stuck. One of the best features of this book (other than presenting information in the easy-to-read Dummies format) is the sheer number of projects you get to try. We recommend implementing all the projects on your computer — there’s nothing that quite stirs the emotions like seeing your very own AI model solve a problem. We have projects that analyze steel plates, projects that classify handwritten numbers, and projects that magically draw a box around a rubber duck you hold up to your webcam. It’s spectacular to learn about AI, but way more fun to create it. We stuffed the book full of programs for you to write, play with, break, improve, and eventually use to take over the world (okay, maybe not that last one). Curiosity is the most important skill you bring to developing AI with Python. As you’ll learn, models range from simple to complex, and techniques won’t always behave like you want them to. We cover plenty of tricks to help you analyze prob- lems that can be solved with AI, but we also try to get you thinking about how our modern-day solutions can be improved. Foolish Assumptions We assume that, while you’re curious about AI, you haven’t actually implemented any AI models yet — we’ll get to those! We also assume that you don’t have prior Python language experience (or any programming experience, for that matter). However, we do assume:
Page
19
Introduction 3 » You are familiar with the basic workings of a computer, including how to install software and manage your file system. » You have a computer accessible to you for projects in this book. » You have an Internet connection that will help you access project materials. Icons Used in This Book Throughout this book, icons in the margins highlight certain types of valuable information that call out for your attention. Here are the icons you’ll encounter and a brief description of each. The Tip icon marks tips and shortcuts you can use on a variety of topics. They are usually informed by personal experience we have with the subject or tool. Remember icons mark the information that’s especially important to know, but easy to forget! The Technical Stuff icon marks information of a highly technical nature that you can normally skip over. It will often give more detail about a technical subject or point you to resources that will let you dive deeper into a topic. The Warning icon tells you to watch out! It marks important information that may save you headaches or avoid particularly catastrophic situations. We don’t use this icon very often, so make sure to take note when we do! Beyond the Book Besides the contents you will find in the book, you can also access other cool materials: » Cheat Sheet: A cheat sheet provides you with even more information and guidance related to AI and Python. You can find the Cheat Sheet for this book at www.dummies.com. Type Python & AI For Dummies in the Search box and click the Cheat Sheet option that appears.
Page
20
4 Python & AI For Dummies » Companion files: The source code referenced in this book is available for download. You can find these files on this book’s page at www.dummies.com/ go/python&aifd. Just enter the book title in the Search box, click Books on the page that appears, click the book’s title, and scroll down the page to Downloads. Where to Go from Here You can start anywhere in this book — but if you’ve never programmed in Python before, Chapter 1 is an excellent way to begin. If you have done some programming and have a little more familiarity with AI (but not implementing it), you can skip to Chapter 4, where we cover machine learning libraries. If your true love lies with neural network architectures, Chapter 8 is the place for you. If you want to learn about the latest and greatest advanced AI models, head on over to Chapter 16. If you picked up this book for the sole express purpose of fulfilling your lifelong dream to build a rubber duck detector, you’re among friends! Oh, and that’s found in Chapter 15.