www.it-ebooks.info Mastering Machine Learning with scikit-learn Apply effective learning algorithms to real-world problems using scikit-learn Gavin Hackeling BIRMINGHAM - MUMBAI www.it-ebooks.info Mastering Machine Learning with scikit-learn Copyright © 2014 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, and its dealers and distributors will be held liable for any damages caused or alleged to be 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 First published: October 2014 Production reference: 1221014 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78398-836-5 www.packtpub.com Cover image by Amy-Lee Winfield (abjure@outlook.com) www.it-ebooks.info Credits Author Project Coordinator Gavin Hackeling Danuta Jones Reviewers Proofreaders Fahad Arshad Simran Bhogal Sarah Guido Tarsonia Sanghera Mikhail Korobov Lindsey Thomas Aman Madaan Indexer Monica Ajmera Mehta Acquisition Editor Meeta Rajani Graphics Content Development Editor Neeshma Ramakrishnan Sheetal Aute Ronak Dhruv Disha Haria Technical Editor Faisal Siddiqui Production Coordinator Kyle Albuquerque Copy Editors Roshni Banerjee Adithi Shetty Cover Work Kyle Albuquerque www.it-ebooks.info About the Author Gavin Hackeling develops machine learning services for large-scale documents and image classification at an advertising network in New York He received his Master's degree from New York University's Interactive Telecommunications Program, and his Bachelor's degree from the University of North Carolina To Hallie, for her support, and Zipper, without whose contributions this book would have been completed in half the time www.it-ebooks.info About the Reviewers Fahad Arshad completed his PhD at Purdue University in the Department of Electrical and Computer Engineering His research interests focus on developing algorithms for software testing, error detection, and failure diagnosis in distributed systems He is particularly interested in data-driven analysis of computer systems His work has appeared at top dependability conferences—DSN, ISSRE, ICAC, Middleware, and SRDS—and he has been awarded grants to attend DSN, ICAC, and ICNP Fahad has also been an active contributor to security research while working as a cybersecurity engineer at NEEScomm IT He has recently taken on a position as a systems engineer in the industry Sarah Guido is a data scientist at Reonomy, where she's helping build disruptive technology in the commercial real estate industry She loves Python, machine learning, and the startup world She is an accomplished conference speaker and an O'Reilly Media author, and is very involved in the Python community Prior to joining Reonomy, Sarah earned a Master's degree from the University of Michigan School of Information www.it-ebooks.info Mikhail Korobov is a software developer at ScrapingHub Inc., where he works on web scraping, information extraction, natural language processing, machine learning, and web development tasks He is an NLTK team member, Scrapy team member, and an author or contributor to many other open source projects I'd like to thank my wife, Aleksandra, for her support and patience and for the cookies Aman Madaan is currently pursuing his Master's in Computer Science and Engineering His interests span across machine learning, information extraction, natural language processing, and distributed computing More details about his skills, interests, and experience can be found at http://www.amanmadaan.in www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book 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.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details At www.PacktPub.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 TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library Here, you can access, read, and search across Packt's entire library of books Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books Simply use your login credentials for immediate access www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: The Fundamentals of Machine Learning Learning from experience Machine learning tasks 10 Training data and test data 11 Performance measures, bias, and variance 13 An introduction to scikit-learn 16 Installing scikit-learn 16 Installing scikit-learn on Windows 17 Installing scikit-learn on Linux 17 Installing scikit-learn on OS X 18 Verifying the installation 18 Installing pandas and matplotlib 18 Summary 19 Chapter 2: Linear Regression 21 Simple linear regression 21 Evaluating the fitness of a model with a cost function 25 Solving ordinary least squares for simple linear regression 27 Evaluating the model 29 Multiple linear regression 31 Polynomial regression 35 Regularization 40 Applying linear regression 41 Exploring the data 41 Fitting and evaluating the model 44 Fitting models with gradient descent 46 Summary 50 www.it-ebooks.info www.it-ebooks.info Thank you for buying Mastering Machine Learning with scikit-learn About Packt Publishing Packt, pronounced 'packed', published its first book "Mastering phpMyAdmin for Effective MySQL Management" in April 2004 and subsequently continued to specialize in publishing highly focused books on specific technologies and solutions Our books and publications share the experiences of your fellow IT professionals in adapting and customizing today's systems, applications, and frameworks Our solution based books give you the knowledge and power to customize the software and technologies you're using to get the job done Packt books are more specific and less general than the IT books you have seen in the past Our unique business model allows us to bring you more focused information, giving you more of what you need to know, and less of what you don't Packt is a modern, yet unique publishing company, which focuses on producing quality, cutting-edge books for communities of developers, administrators, and newbies alike For more information, please visit our website: www.packtpub.com About Packt Open Source In 2010, Packt launched two new brands, Packt Open Source and Packt Enterprise, in order to continue its focus on specialization This book is part of the Packt Open Source brand, home to books published on software built around Open Source licenses, and offering information to anybody from advanced developers to budding web designers The Open Source brand also runs Packt's Open Source Royalty Scheme, by which Packt gives a royalty to each Open Source project about whose software a book is sold Writing for Packt We welcome all inquiries from people who are interested in authoring Book proposals should be sent to author@packtpub.com If your book idea is still at an early stage and you would like to discuss it first before writing a formal book proposal, contact us; one of our commissioning editors will get in touch with you We're not just looking for published authors; if you have strong technical skills but no writing experience, our experienced editors can help you develop a writing career, or simply get some additional reward for your expertise www.it-ebooks.info Learning scikit-learn: Machine Learning in Python ISBN: 978-1-78328-193-0 Paperback: 118 pages Experience the benefits of machine learning techniques by applying them to real-world problems using Python and the open source scikit-learn library Use Python and scikit-learn to create intelligent applications Apply regression techniques to predict future behavior and learn to cluster items in groups by their similarities Make use of classification techniques to perform image recognition and document classification Building Machine Learning Systems with Python ISBN: 978-1-78216-140-0 Paperback: 290 pages Master the art of machine learning with Python and build effective machine learning systems with this intensive hands-on guide Master Machine Learning using a broad set of Python libraries and start building your own Python-based ML systems Covers classification, regression, feature engineering, and much more guided by practical examples A scenario-based tutorial to get into the right mind-set of a machine learner (data exploration) and successfully implement this in your new or existing projects Please check www.PacktPub.com for information on our titles www.it-ebooks.info Clojure for Machine Learning ISBN: 978-1-78328-435-1 Paperback: 292 pages Successfully leverage advanced machine learning techniques using the Clojure ecosystem Covers a lot of machine learning techniques with Clojure programming Encompasses precise patterns in data to predict future outcomes using various machine learning techniques Packed with several machine learning libraries available in the Clojure ecosystem Machine Learning with R ISBN: 978-1-78216-214-8 Paperback: 396 pages Learn how to use R to apply powerful machine learning methods and gain an insight into real-world applications Harness the power of R for statistical computing and data science Use R to apply common machine learning algorithms with real-world applications Prepare, examine, and visualize data for analysis Understand how to choose between machine learning models Please check www.PacktPub.com for information on our titles www.it-ebooks.info .. .Mastering Machine Learning with scikit- learn Apply effective learning algorithms to real-world problems using scikit- learn Gavin Hackeling BIRMINGHAM - MUMBAI www.it-ebooks.info Mastering Machine. .. at machine learning' s potential In this book, we will examine several machine learning models and learning algorithms We will discuss tasks that machine learning is commonly applied to, and learn. .. Fundamentals of Machine Learning An introduction to scikit- learn Since its release in 2007, scikit- learn has become one of the most popular open source machine learning libraries for Python scikit- learn