Python 3 for Machine Learning
Total Page:16
File Type:pdf, Size:1020Kb
PYTHON 3 FOR MACHINE LEARNING LICENSE, DISCLAIMER OF LIABILITY, AND LIMITED WARRANTY By purchasing or using this book and its companion files (the “Work”), you agree that this license grants permission to use the contents contained herein, but does not give you the right of ownership to any of the textual content in the book or ownership to any of the information, files, or products contained in it. This license does not permit uploading of the Work onto the Internet or on a network (of any kind) without the written consent of the Publisher. Duplication or dissemination of any text, code, simulations, images, etc. contained herein is limited to and subject to licensing terms for the respective products, and permission must be obtained from the Publisher or the owner of the content, etc., in order to reproduce or network any portion of the textual material (in any media) that is contained in the Work. MERCURY LEARNING AND INFORMATION (“MLI” or “the Publisher”) and anyone involved in the creation, writing, production, accompanying algorithms, code, or computer programs (“the software”), and any accompanying Web site or software of the Work, cannot and do not warrant the performance or results that might be obtained by using the contents of the Work. The author, developers, and the Publisher have used their best efforts to insure the accuracy and functionality of the textual material and/or programs contained in this package; we, however, make no warranty of any kind, express or implied, regarding the performance of these contents or programs. The Work is sold “as is” without warranty (except for defective materials used in manufacturing the book or due to faulty workmanship). The author, developers, and the publisher of any accompanying content, and anyone involved in the composition, production, and manufacturing of this work will not be liable for damages of any kind arising out of the use of (or the inability to use) the algorithms, source code, computer programs, or textual material contained in this publication. This includes, but is not limited to, loss of revenue or profit, or other incidental, physical, or consequential damages arising out of the use of this Work. The sole remedy in the event of a claim of any kind is expressly limited to replacement of the book and only at the discretion of the Publisher. The use of “implied warranty” and certain “exclusions” vary from state to state, and might not apply to the purchaser of this product. Companion files also available for downloading from the publisher by writing to [email protected]. PYTHON 3 FOR MACHINE LEARNING OSWALD CAMPESATO MERCURY LEARNING AND INFORMATION Dulles, Virginia Boston, Massachusetts New Delhi Copyright ©2020 by Mercury Learning and Information LLC. All rights reserved. This publication, portions of it, or any accompanying software may not be reproduced in any way, stored in a retrieval system of any type, or transmitted by any means, media, electronic display or mechanical display, including, but not limited to, photocopy, recording, Internet postings, or scanning, without prior permission in writing from the publisher. Publisher: David Pallai Mercury Learning and Information 22841 Quicksilver Drive Dulles, VA 20166 [email protected] www.merclearning.com 1-800-232-0223 O. Campesato. Python 3 for Machine Learning. ISBN: 978-1-68392-495-1 The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products. All brand names and product names mentioned in this book are trademarks or service marks of their respective companies. Any omission or misuse (of any kind) of service marks or trademarks, etc. is not an attempt to infringe on the property of others. Library of Congress Control Number: 2020930258 202122321 Printed on acid-free paper in the United States of America. Our titles are available for adoption, license, or bulk purchase by institutions, corporations, etc. For additional information, please contact the Customer Service Dept. at 800-232-0223(toll free). Companion files are available for download by writing to the publisher at [email protected]. All of our titles are available in digital format at Academiccourseware.com and other digital vendors. The sole obligation of Mercury Learning and Information to the purchaser is to replace the book, based on defective materials or faulty workmanship, but not based on the operation or functionality of the product. I’d like to dedicate this book to my parents – may this bring joy and happiness into their lives. CONTENTS Preface xvii Chapter 1 Introduction to Python 3 1 1.1 Tools for Python 2 1.1.1 easy_install and pip 3 1.1.2 virtualenv 3 1.1.3 IPython 3 1.2 Python Installation 4 1.3 Setting the PATH Environment Variable (Windows Only) 5 1.4 Launching Python on Your Machine 5 1.4.1 The Python Interactive Interpreter 5 1.5 Python Identifiers 6 1.6 Lines, Indentation, and Multilines 7 1.7 Quotation and Comments in Python 8 1.8 Saving Your Code in a Module 9 1.9 Some Standard Modules in Python 10 1.10 The help() and dir() Functions 11 1.11 Compile Time and Runtime Code Checking 12 1.12 Simple Data Types in Python 13 1.13 Working with Numbers 13 1.13.1 Working with Other Bases 15 1.13.2 The chr() Function 15 1.13.3 The round() Function in Python 16 1.13.4 Formatting Numbers in Python 16 1.14 Working with Fractions 17 1.15 Unicode and UTF-8 18 1.16 Working with Unicode 18 1.17 Working with Strings 19 1.17.1 Comparing Strings 21 1.17.2 Formatting Strings in Python 21 1.18 Uninitialized Variables and the Value None in Python 22 1.19 Slicing Strings 22 1.19.1 Testing for Digits and Alphabetic Characters 23 1.20 Search and Replace a String in Other Strings 24 1.21 Remove Leading and Trailing Characters 25 1.22 Printing Text without NewLine Characters 26 1.23 Text Alignment 27 1.24 Working with Dates 27 1.24.1 Converting Strings to Dates 29 viii • CONTENTS 1.25 Exception Handling in Python 29 1.26 Handling User Input 31 1.27 Command-Line Arguments 33 1.28 Summary 35 Chapter 2 Conditional Logic, Loops, and Functions 37 2.1 Precedence of Operators in Python 38 2.2 Python Reserved Words 39 2.3 Working with Loops in Python 39 2.3.1 Python for Loops 39 2.3.2 A for Loop with try/except in Python 40 2.3.3 Numeric Exponents in Python 41 2.4 Nested Loops 42 2.5 The split() Function with for Loops 43 2.6 Using the split() Function to Compare Words 43 2.7 Using the split() Function to Print Justified Text 44 2.8 Using the split() Function to Print Fixed Width Text 45 2.9 Using the split() Function to Compare Text Strings 47 2.10 Using a Basic for Loop to Display Characters in a String 48 2.11 The join() Function 48 2.12 Python while Loops 49 2.13 Conditional Logic in Python 50 2.14 The break/continue/pass Statements 50 2.15 Comparison and Boolean Operators 51 2.15.1 The in/not in/is/is not Comparison Operators 51 2.15.2 The and, or, and not Boolean Operators 52 2.16 Local and Global Variables 52 2.17 Scope of Variables 53 2.18 Pass by Reference versus Value 55 2.19 Arguments and Parameters 56 2.20 Using a while loop to Find the Divisors of a Number 56 2.20.1 Using a while loop to Find Prime Numbers 57 2.21 User-Defined Functions in Python 58 2.22 Specifying Default Values in a Function 59 2.22.1 Returning Multiple Values from a Function 60 2.23 Functions with a Variable Number of Arguments 60 2.24 Lambda Expressions 61 2.25 Recursion 62 2.25.1 Calculating Factorial Values 62 2.25.2 Calculating Fibonacci Numbers 63 2.25.3 Calculating the GCD of Two Numbers 64 2.25.4 Calculating the LCM of Two Numbers 65 2.26 Summary 66 CONTENTS • ix Chapter 3 Python Collections 67 3.1 Working with Lists 68 3.1.1 Lists and Basic Operations 68 3.1.2 Reversing and Sorting a List 70 3.1.3 Lists and Arithmetic Operations 71 3.1.4 Lists and Filter-Related Operations 72 3.2 Sorting Lists of Numbers and Strings 73 3.3 Expressions in Lists 74 3.4 Concatenating a List of Words 74 3.5 The BubbleSort in Python 75 3.6 The Python range() Function 76 3.6.1 Counting Digits, Uppercase, and Lowercase Letters 76 3.7 Arrays and the append() Function 77 3.8 Working with Lists and the split()Function 78 3.9 Counting Words in a List 79 3.10 Iterating through Pairs of Lists 79 3.11 Other List-Related Functions 80 3.12 Using a List as a Stack and a Queue 82 3.13 Working with Vectors 83 3.14 Working with Matrices 84 3.15 The NumPy Library for Matrices 85 3.16 Queues 86 3.17 Tuples (Immutable Lists) 87 3.18 Sets 88 3.19 Dictionaries 89 3.19.1 Creating a Dictionary 89 3.19.2 Displaying the Contents of a Dictionary 90 3.19.3 Checking for Keys in a Dictionary 90 3.19.4 Deleting Keys from a Dictionary 91 3.19.5 Iterating through a Dictionary 91 3.19.6 Interpolating Data from a Dictionary 92 3.20 Dictionary Functions and Methods 92 3.21 Dictionary Formatting 92 3.22 Ordered Dictionaries 93 3.22.1 Sorting Dictionaries 93 3.22.2 Python Multidictionaries 94 3.23 Other Sequence Types in Python 94 3.24 Mutable and Immutable Types in Python 95 3.25 The type() Function 96 3.26 Summary 97 Chapter 4 Introduction to NumPy and Pandas 99 4.1 What is NumPy? 101 4.1.1 Useful NumPy Features 101 x • CONTENTS 4.2 What are NumPy Arrays? 102 4.3 Working with Loops 103 4.4 Appending Elements to Arrays (1) 104 4.5 Appending Elements to Arrays (2) 105 4.6 Multiply Lists and Arrays 106 4.7 Doubling the Elements in a List 106 4.8 Lists and Exponents 107 4.9 Arrays and Exponents 107 4.10 Math Operations and Arrays 108 4.11 Working with “-1” Subranges with Vectors 109 4.12 Working with “-1” Subranges with Arrays