Artificial Intelligence with Python Cookbook Proven recipes for applying AI algorithms and deep learning techniques using TensorFlow 2.x and PyTorch 1.6 Ben Auffarth BIRMINGHAM - MUMBAI Artificial Intelligence with Python Cookbook Copyright © 2020 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Sunith Shetty Acquisition Editor: Siddharth Mandal Content Development Editor: Nathanya Dias Senior Editor: David Sugarman Technical Editor: Sonam Pandey Copy Editor: Safis Editing Project Coordinator: Aishwarya Mohan Proofreader: Safis Editing Indexer: Priyanka Dhadke Production Designer: Jyoti Chauhan First published: October 2020 Production reference: 1291020 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78913-396-7 www.packt.com Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and videos from over 4,000 industry professionals Improve your learning with skill plans tailored especially for you Get a free eBook or video every month Fully searchable for easy access to vital information Copy and paste, print, and bookmark content Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and, as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.packt.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Ben Auffarth is a full-stack data scientist with more than 15 years of work experience. With a background and Ph.D. in computational and cognitive neuroscience, he has designed and conducted wet lab experiments on cell cultures, analyzed experiments with terabytes of data, run brain models on IBM supercomputers with up to 64k cores, built production systems processing hundreds of thousands of transactions per day, and trained neural networks on millions of text documents. He resides in West London with his family, where you might find him in a playground with his young son. He co-founded and is the former president of Data Science Speakers, London. I am deeply grateful to the editors at Packt, who provided practical help and competent advice, and to everyone who has been close to me and supported me, especially my partner Diane. This book is dedicated to Diane and my son, Nicholas. About the reviewers va barbosa is a software engineer in the Qiskit Community at IBM, focused on building open source tools and creating educational content for developers, researchers, students, and educators in the field of quantum computing. Previously, va was a developer advocate at the Center for Open-Source Data and AI Technologies where he assisted developers discover and make use of data science and machine learning technologies. He is fueled by his passion to help others and guided by his enthusiasm for open source technology. Eyal Wirsansky is an Artificial Intelligence consultant and mentor, a senior software engineer, and a technology community leader. Eyal leads the Jacksonville (FL) Java User Group, hosts the Artificial Intelligence for Enterprise virtual user group, writes a developer-oriented Artificial Intelligence blog, and often speaks about topics related to Artificial Intelligence, Machine Learning and Genetic Algorithms. Eyal is the author of the book Hands-On Genetic Algorithms with Python. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Getting Started with Artificial Intelligence in Python 8 Technical requirements 8 Setting up a Jupyter environment 9 Getting ready 9 How to do it... 10 Installing libraries with Google Colab 10 Self-hosting a Jupyter Notebook environment 11 How it works... 14 There's more... 14 See also 15 Getting proficient in Python for AI 16 Getting ready 17 How to do it... 18 Obtaining the history of Jupyter commands and outputs 18 Execution history 19 Outputs 19 Auto-reloading packages 19 Debugging 20 Timing code execution 21 Displaying progress bars 22 Compiling your code 23 Speeding up pandas DataFrames 25 Parallelizing your code 26 See also 28 Classifying in scikit-learn, Keras, and PyTorch 29 Getting ready 29 How to do it... 30 Visualizing data in seaborn 31 Modeling in scikit-learn 33 Modeling in Keras 35 Modeling in PyTorch 42 How it works... 45 Neural network training 45 The SELU activation function 49 Softmax activation 49 Cross-entropy 49 See also 50 Modeling with Keras 51 Getting ready 52 How to do it... 53 Table of Contents Data loading and preprocessing 54 Model training 62 How it works... 66 Maximal information coefficient 66 Data generators 67 Permutation importance 68 See also 68 Chapter 2: Advanced Topics in Supervised Machine Learning 70 Technical requirements 71 Transforming data in scikit-learn 71 Getting ready 72 How to do it... 73 Encoding ranges numerically 73 Deriving higher-order features 76 Combining transformations 78 How it works... 80 There's more... 81 See also 82 Predicting house prices in PyTorch 82 Getting ready 82 How to do it... 85 How it works... 92 There's more... 93 See also 95 Live decisioning customer values 96 Getting ready 96 How to do it... 97 How it works... 100 Active learning 101 Hoeffding Tree 101 Class weighting 101 See also 102 Battling algorithmic bias 102 Getting ready 103 How to do it... 105 How it works... 113 There's more... 114 See also 117 Forecasting CO2 time series 118 Getting ready 118 How to do it... 119 Analyzing time series using ARIMA and SARIMA 121 How it works... 123 There's more... 126 See also 127 [ ii ] Table of Contents Chapter 3: Patterns, Outliers, and Recommendations 129 Clustering market segments 130 Getting ready 130 How to do it... 131 How it works... 136 There's more... 138 See also 140 Discovering anomalies 141 Getting ready 141 How to do it... 142 How it works... 149 k-nearest neighbors 149 Isolation forest 149 Autoencoder 149 See also 150 Representing for similarity search 150 Getting ready 151 How to do it... 152 Baseline – string comparison functions 153 Bag-of-characters approach 154 Siamese neural network approach 155 How it works... 159 Recommending products 160 Getting ready 160 How to do it... 162 How it works... 164 Precision at k 165 Matrix factorization 165 The lightfm model 166 See also 166 Spotting fraudster communities 167 Getting ready 167 How to do it... 168 Creating an adjacency matrix 168 Community detection algorithms 169 Evaluating the communities 170 How it works... 172 Graph community algorithms 172 Louvain algorithm 172 Girvan–Newman algorithm 173 Information entropy 173 There's more... 173 See also 175 Chapter 4: Probabilistic Modeling 176 Technical requirements 177 Predicting stock prices with confidence 177 [ iii ] Table of Contents Getting ready 177 How to do it... 178 How it works... 182 Featurization 182 Platt scaling 183 Isotonic regression 184 Naive Bayes 184 See also 185 Estimating customer lifetime value 186 Getting ready 186 How to do it... 186 How it works... 188 The BG/NBD model 189 The Gamma-Gamma model 189 See also 189 Diagnosing a disease 190 Getting ready 190 How to do it... 191 How it works... 196 Aleatoric uncertainty 196 Negative log-likelihood 197 Bernoulli distribution 197 Metrics 197 See also 198 Stopping credit defaults 198 Getting ready 198 How to do it... 199 How it works... 203 Epistemic uncertainty 203 See also 204 Chapter 5: Heuristic Search Techniques and Logical Inference 205 Making decisions based on knowledge 205 Getting ready 206 How to do it... 206 Logical reasoning 207 Knowledge embedding 207 How it works... 211 Logical reasoning 212 Logic provers 213 Knowledge embedding 213 Graph embedding with Walklets 213 See also 214 Solving the n-queens problem 216 Getting ready 216 How to do it... 217 Genetic algorithm 217 [ iv ]