Python® Machine Learning Python® Machine Learning Wei-Meng Lee Python® Machine Learning Published by John Wiley & Sons, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2019 by John Wiley & Sons, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-1-119-54563-7 ISBN: 978-1-119-54569-9 (ebk) ISBN: 978-1-119-54567-5 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 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 either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Per- missions 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. 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. Nei- ther the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site 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 United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. 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 such as a CD or DVD 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: 2019931301 Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the United States and other countries, and may not be used without written permission. Python is a regis- tered trademark of 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. I dedicate this book with love to my dearest wife (Sze Wa) and girl (Chloe), who have to endure my irregular work schedule and for their companionship when I am trying to meet writing deadlines! About the Author Wei-Meng Lee is a technologist and founder of Developer Learning Solutions (http://www.learn2develop.net), a company specializing in hands-on training on the latest technologies. Wei-Meng has many years of training experience, and his training courses place special emphasis on the learning-by-doing approach. His hands-on approach to learning programming makes understanding the subject much easier than just reading books, tutorials, and documentations. Wei-Meng’s name regularly appears in online and print publications such as DevX.com, MobiForge.com, and CoDe Magazine. You can contact Wei-Meng at: [email protected]. vii About the Technical Editor Doug Mahugh is a software developer who began his career in 1978 as a For- tran programmer for Boeing. Doug has worked for Microsoft since 2005 in a variety of roles including developer advocacy, standards engagement, and content development. Since learning Python in 2008, Doug has written samples and tutorials on topics ranging from caching and continuous integration to Azure Active Directory authentication and Microsoft Graph. Doug has spoken at industry events in over 20 countries, and he has been Microsoft’s technical representative to standards bodies including ISO/IEC, Ecma International, OASIS, CalConnect, and others. Doug currently lives in Seattle with his wife Megan and two Samoyeds named Jamie and Alice. ix Credits Acquisitions Editor Copy Editor Devon Lewis Kim Cofer Associate Publisher Proofreader Jim Minatel Nancy Bell Editorial Manager Indexer Pete Gaughan Potomac Indexing, LLC Production Manager Cover Designer Katie Wisor Wiley Project Editor Cover Image Gary Schwartz ©Lidiia Moor/iStockphoto- background texture Production Editor © Rick_Jo/iStockphoto-digital Barath Kumar Rajasekaran robotic brain Technical Editor Doug Mahugh xi Acknowledgments Writing a book is always exciting, but along with it come long hours of hard work, straining to get things done accurately and correctly. To make a book possible, a lot of unsung heroes work tirelessly behind the scenes. For this, I would like to take this opportunity to thank a number of special people who made this book possible. First, I want to thank my acquisitions editor Devon Lewis, who was my first point of contact for this book. Thank you, Devon, for giving me this opportu- nity and for your trust in me! Next, a huge thanks to Gary Schwartz, my project editor, who was always a pleasure to work with. Gary is always contactable, even when he is at the air- port! Gary has been very patient with me, even though I have missed several of my deadlines for the book. I know it threw a spanner into his plan, but he is always accommodating. Working with him, I know my book is in good hands. Thank you very much, Gary! Equally important is my technical editor—Doug Mahugh. Doug has been very eager-eyed editing and testing my code, and never fails to let me know if things do not work the way I intended. Thanks for catching my errors and making the book a better read, Doug! I would also like to take this opportunity to thank my production editor—Barath Kumar Rajasekaran. Without his hard work, this book would not be even possible. Thanks, Barath! Last, but not least, I want to thank my parents and my wife, Sze Wa, for all the support they have given me. They have selflessly adjusted their schedules to accommodate my busy schedule when I was working on this book. I love you all! xiii Contents at a glance Introduction xxiii Chapter 1 Introduction to Machine Learning 1 Chapter 2 Extending Python Using NumPy 19 Chapter 3 Manipulating Tabular Data Using Pandas 39 Chapter 4 Data Visualization Using matplotlib 67 Chapter 5 Getting Started with Scikit-learn for Machine Learning 93 Chapter 6 Supervised Learning—Linear Regression 119 Chapter 7 S upervised Learning—Classification Using Logistic Regression 151 Chapter 8 Supervised Learning—Classification Using Support Vector Machines 177 Chapter 9 Supervised Learning—Classification Using K-Nearest Neighbors (KNN) 205 Chapter 10 Unsupervised Learning—Clustering Using K-Means 221 Chapter 11 Using Azure Machine Learning Studio 243 Chapter 12 Deploying Machine Learning Models 269 Index 285 xv
Description: