O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Python Machine Learning: Perform Python Machine Learning and Deep Learning with Python, scikit-learn, and TensorFlow

Book Description

Unlock modern machine learning and deep learning techniques with Python by using the latest cutting-edge open source Python libraries.

About This Book

  • Second edition of the bestselling book on Machine Learning
  • A practical approach to key frameworks in data science, machine learning, and deep learning
  • Use the most powerful Python libraries to implement machine learning and deep learning
  • Get to know the best practices to improve and optimize your machine learning systems and algorithms

Who This Book Is For

If you know some Python and you want to use machine learning and deep learning, pick up this book. Whether you want to start from scratch or extend your machine learning knowledge, this is an essential and unmissable resource. Written for developers and data scientists who want to create practical machine learning and deep learning code, this book is ideal for developers and data scientists who want to teach computers how to learn from data.

What You Will Learn

  • Understand the key frameworks in data science, machine learning, and deep learning
  • Harness the power of the latest Python open source libraries in machine learning
  • Explore machine learning techniques using challenging real-world data
  • Master deep neural network implementation using the TensorFlow library
  • Learn the mechanics of classification algorithms to implement the best tool for the job
  • Predict continuous target outcomes using regression analysis
  • Uncover hidden patterns and structures in data with clustering
  • Delve deeper into textual and social media data using sentiment analysis

In Detail

Machine learning is eating the software world, and now deep learning is extending machine learning. Understand and work at the cutting edge of machine learning, neural networks, and deep learning with this second edition of Sebastian Raschka’s bestselling book, Python Machine Learning. Thoroughly updated using the latest Python open source libraries, this book offers the practical knowledge and techniques you need to create and contribute to machine learning, deep learning, and modern data analysis.

Fully extended and modernized, Python Machine Learning Second Edition now includes the popular TensorFlow deep learning library. The scikit-learn code has also been fully updated to include recent improvements and additions to this versatile machine learning library.

Sebastian Raschka and Vahid Mirjalili’s unique insight and expertise introduce you to machine learning and deep learning algorithms from scratch, and show you how to apply them to practical industry challenges using realistic and interesting examples. By the end of the book, you’ll be ready to meet the new data analysis opportunities in today’s world.

If you’ve read the first edition of this book, you’ll be delighted to find a new balance of classical ideas and modern insights into machine learning. Every chapter has been critically updated, and there are new chapters on key technologies. You’ll be able to learn and work with TensorFlow more deeply than ever before, and get essential coverage of the Keras neural network library, along with the most recent updates to scikit-learn.

Style and Approach

Python Machine Learning Second Edition takes a practical, hands-on coding approach so you can learn about machine learning by coding with Python. This book moves fluently between the theoretical principles of machine learning and the practical details of implementation with Python.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Python Machine Learning Second Edition
    1. Table of Contents
    2. Python Machine Learning Second Edition
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    7. Packt is Searching for Authors Like You
    8. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    9. 1. Giving Computers the Ability to Learn from Data
      1. Building intelligent machines to transform data into knowledge
      2. The three different types of machine learning
        1. Making predictions about the future with supervised learning
          1. Classification for predicting class labels
          2. Regression for predicting continuous outcomes
        2. Solving interactive problems with reinforcement learning
        3. Discovering hidden structures with unsupervised learning
          1. Finding subgroups with clustering
          2. Dimensionality reduction for data compression
      3. Introduction to the basic terminology and notations
      4. A roadmap for building machine learning systems
        1. Preprocessing – getting data into shape
        2. Training and selecting a predictive model
        3. Evaluating models and predicting unseen data instances
      5. Using Python for machine learning
        1. Installing Python and packages from the Python Package Index
        2. Using the Anaconda Python distribution and package manager
        3. Packages for scientific computing, data science, and machine learning
      6. Summary
    10. 2. Training Simple Machine Learning Algorithms for Classification
      1. Artificial neurons – a brief glimpse into the early history of machine learning
        1. The formal definition of an artificial neuron
        2. The perceptron learning rule
      2. Implementing a perceptron learning algorithm in Python
        1. An object-oriented perceptron API
        2. Training a perceptron model on the Iris dataset
      3. Adaptive linear neurons and the convergence of learning
        1. Minimizing cost functions with gradient descent
        2. Implementing Adaline in Python
        3. Improving gradient descent through feature scaling
        4. Large-scale machine learning and stochastic gradient descent
      4. Summary
    11. 3. A Tour of Machine Learning Classifiers Using scikit-learn
      1. Choosing a classification algorithm
      2. First steps with scikit-learn – training a perceptron
      3. Modeling class probabilities via logistic regression
        1. Logistic regression intuition and conditional probabilities
        2. Learning the weights of the logistic cost function
        3. Converting an Adaline implementation into an algorithm for logistic regression
        4. Training a logistic regression model with scikit-learn
        5. Tackling overfitting via regularization
      4. Maximum margin classification with support vector machines
        1. Maximum margin intuition
        2. Dealing with a nonlinearly separable case using slack variables
        3. Alternative implementations in scikit-learn
      5. Solving nonlinear problems using a kernel SVM
        1. Kernel methods for linearly inseparable data
        2. Using the kernel trick to find separating hyperplanes in high-dimensional space
      6. Decision tree learning
        1. Maximizing information gain – getting the most bang for your buck
        2. Building a decision tree
        3. Combining multiple decision trees via random forests
      7. K-nearest neighbors – a lazy learning algorithm
      8. Summary
    12. 4. Building Good Training Sets – Data Preprocessing
      1. Dealing with missing data
        1. Identifying missing values in tabular data
        2. Eliminating samples or features with missing values
        3. Imputing missing values
        4. Understanding the scikit-learn estimator API
      2. Handling categorical data
        1. Nominal and ordinal features
          1. Creating an example dataset
        2. Mapping ordinal features
        3. Encoding class labels
        4. Performing one-hot encoding on nominal features
      3. Partitioning a dataset into separate training and test sets
      4. Bringing features onto the same scale
      5. Selecting meaningful features
        1. L1 and L2 regularization as penalties against model complexity
        2. A geometric interpretation of L2 regularization
        3. Sparse solutions with L1 regularization
        4. Sequential feature selection algorithms
      6. Assessing feature importance with random forests
      7. Summary
    13. 5. Compressing Data via Dimensionality Reduction
      1. Unsupervised dimensionality reduction via principal component analysis
        1. The main steps behind principal component analysis
        2. Extracting the principal components step by step
        3. Total and explained variance
        4. Feature transformation
        5. Principal component analysis in scikit-learn
      2. Supervised data compression via linear discriminant analysis
        1. Principal component analysis versus linear discriminant analysis
        2. The inner workings of linear discriminant analysis
        3. Computing the scatter matrices
        4. Selecting linear discriminants for the new feature subspace
        5. Projecting samples onto the new feature space
        6. LDA via scikit-learn
      3. Using kernel principal component analysis for nonlinear mappings
        1. Kernel functions and the kernel trick
        2. Implementing a kernel principal component analysis in Python
          1. Example 1 – separating half-moon shapes
          2. Example 2 – separating concentric circles
        3. Projecting new data points
        4. Kernel principal component analysis in scikit-learn
      4. Summary
    14. 6. Learning Best Practices for Model Evaluation and Hyperparameter Tuning
      1. Streamlining workflows with pipelines
        1. Loading the Breast Cancer Wisconsin dataset
        2. Combining transformers and estimators in a pipeline
      2. Using k-fold cross-validation to assess model performance
        1. The holdout method
        2. K-fold cross-validation
      3. Debugging algorithms with learning and validation curves
        1. Diagnosing bias and variance problems with learning curves
        2. Addressing over- and underfitting with validation curves
      4. Fine-tuning machine learning models via grid search
        1. Tuning hyperparameters via grid search
        2. Algorithm selection with nested cross-validation
      5. Looking at different performance evaluation metrics
        1. Reading a confusion matrix
        2. Optimizing the precision and recall of a classification model
        3. Plotting a receiver operating characteristic
        4. Scoring metrics for multiclass classification
      6. Dealing with class imbalance
      7. Summary
    15. 7. Combining Different Models for Ensemble Learning
      1. Learning with ensembles
      2. Combining classifiers via majority vote
        1. Implementing a simple majority vote classifier
        2. Using the majority voting principle to make predictions
        3. Evaluating and tuning the ensemble classifier
      3. Bagging – building an ensemble of classifiers from bootstrap samples
        1. Bagging in a nutshell
        2. Applying bagging to classify samples in the Wine dataset
      4. Leveraging weak learners via adaptive boosting
        1. How boosting works
        2. Applying AdaBoost using scikit-learn
      5. Summary
    16. 8. Applying Machine Learning to Sentiment Analysis
      1. Preparing the IMDb movie review data for text processing
        1. Obtaining the movie review dataset
        2. Preprocessing the movie dataset into more convenient format
      2. Introducing the bag-of-words model
        1. Transforming words into feature vectors
        2. Assessing word relevancy via term frequency-inverse document frequency
        3. Cleaning text data
        4. Processing documents into tokens
      3. Training a logistic regression model for document classification
      4. Working with bigger data – online algorithms and out-of-core learning
      5. Topic modeling with Latent Dirichlet Allocation
        1. Decomposing text documents with LDA
        2. LDA with scikit-learn
      6. Summary
    17. 9. Embedding a Machine Learning Model into a Web Application
      1. Serializing fitted scikit-learn estimators
      2. Setting up an SQLite database for data storage
      3. Developing a web application with Flask
        1. Our first Flask web application
        2. Form validation and rendering
          1. Setting up the directory structure
          2. Implementing a macro using the Jinja2 templating engine
          3. Adding style via CSS
          4. Creating the result page
      4. Turning the movie review classifier into a web application
        1. Files and folders – looking at the directory tree
        2. Implementing the main application as app.py
        3. Setting up the review form
        4. Creating a results page template
      5. Deploying the web application to a public server
        1. Creating a PythonAnywhere account
        2. Uploading the movie classifier application
        3. Updating the movie classifier
      6. Summary
    18. 10. Predicting Continuous Target Variables with Regression Analysis
      1. Introducing linear regression
        1. Simple linear regression
        2. Multiple linear regression
      2. Exploring the Housing dataset
        1. Loading the Housing dataset into a data frame
        2. Visualizing the important characteristics of a dataset
        3. Looking at relationships using a correlation matrix
      3. Implementing an ordinary least squares linear regression model
        1. Solving regression for regression parameters with gradient descent
        2. Estimating coefficient of a regression model via scikit-learn
      4. Fitting a robust regression model using RANSAC
      5. Evaluating the performance of linear regression models
      6. Using regularized methods for regression
      7. Turning a linear regression model into a curve – polynomial regression
        1. Adding polynomial terms using scikit-learn
        2. Modeling nonlinear relationships in the Housing dataset
      8. Dealing with nonlinear relationships using random forests
        1. Decision tree regression
        2. Random forest regression
      9. Summary
    19. 11. Working with Unlabeled Data – Clustering Analysis
      1. Grouping objects by similarity using k-means
        1. K-means clustering using scikit-learn
        2. A smarter way of placing the initial cluster centroids using k-means++
        3. Hard versus soft clustering
        4. Using the elbow method to find the optimal number of clusters
        5. Quantifying the quality of clustering via silhouette plots
      2. Organizing clusters as a hierarchical tree
        1. Grouping clusters in bottom-up fashion
        2. Performing hierarchical clustering on a distance matrix
        3. Attaching dendrograms to a heat map
        4. Applying agglomerative clustering via scikit-learn
      3. Locating regions of high density via DBSCAN
      4. Summary
    20. 12. Implementing a Multilayer Artificial Neural Network from Scratch
      1. Modeling complex functions with artificial neural networks
        1. Single-layer neural network recap
        2. Introducing the multilayer neural network architecture
        3. Activating a neural network via forward propagation
      2. Classifying handwritten digits
        1. Obtaining the MNIST dataset
        2. Implementing a multilayer perceptron
      3. Training an artificial neural network
        1. Computing the logistic cost function
        2. Developing your intuition for backpropagation
        3. Training neural networks via backpropagation
      4. About the convergence in neural networks
      5. A few last words about the neural network implementation
      6. Summary
    21. 13. Parallelizing Neural Network Training with TensorFlow
      1. TensorFlow and training performance
        1. What is TensorFlow?
        2. How we will learn TensorFlow
        3. First steps with TensorFlow
        4. Working with array structures
        5. Developing a simple model with the low-level TensorFlow API
      2. Training neural networks efficiently with high-level TensorFlow APIs
        1. Building multilayer neural networks using TensorFlow's Layers API
        2. Developing a multilayer neural network with Keras
      3. Choosing activation functions for multilayer networks
        1. Logistic function recap
        2. Estimating class probabilities in multiclass classification via the softmax function
        3. Broadening the output spectrum using a hyperbolic tangent
        4. Rectified linear unit activation
      4. Summary
    22. 14. Going Deeper – The Mechanics of TensorFlow
      1. Key features of TensorFlow
      2. TensorFlow ranks and tensors
        1. How to get the rank and shape of a tensor
      3. Understanding TensorFlow's computation graphs
      4. Placeholders in TensorFlow
        1. Defining placeholders
        2. Feeding placeholders with data
        3. Defining placeholders for data arrays with varying batchsizes
      5. Variables in TensorFlow
        1. Defining variables
        2. Initializing variables
        3. Variable scope
        4. Reusing variables
      6. Building a regression model
      7. Executing objects in a TensorFlow graph using their names
      8. Saving and restoring a model in TensorFlow
      9. Transforming Tensors as multidimensional data arrays
      10. Utilizing control flow mechanics in building graphs
      11. Visualizing the graph with TensorBoard
        1. Extending your TensorBoard experience
      12. Summary
    23. 15. Classifying Images with Deep Convolutional Neural Networks
      1. Building blocks of convolutional neural networks
        1. Understanding CNNs and learning feature hierarchies
        2. Performing discrete convolutions
          1. Performing a discrete convolution in one dimension
          2. The effect of zero-padding in a convolution
          3. Determining the size of the convolution output
          4. Performing a discrete convolution in 2D
        3. Subsampling
      2. Putting everything together to build a CNN
        1. Working with multiple input or color channels
        2. Regularizing a neural network with dropout
      3. Implementing a deep convolutional neural network using TensorFlow
        1. The multilayer CNN architecture
        2. Loading and preprocessing the data
        3. Implementing a CNN in the TensorFlow low-level API
        4. Implementing a CNN in the TensorFlow Layers API
      4. Summary
    24. 16. Modeling Sequential Data Using Recurrent Neural Networks
      1. Introducing sequential data
        1. Modeling sequential data – order matters
        2. Representing sequences
        3. The different categories of sequence modeling
      2. RNNs for modeling sequences
        1. Understanding the structure and flow of an RNN
        2. Computing activations in an RNN
        3. The challenges of learning long-range interactions
        4. LSTM units
      3. Implementing a multilayer RNN for sequence modeling in TensorFlow
      4. Project one – performing sentiment analysis of IMDb movie reviews using multilayer RNNs
        1. Preparing the data
        2. Embedding
        3. Building an RNN model
        4. The SentimentRNN class constructor
        5. The build method
          1. Step 1 – defining multilayer RNN cells
          2. Step 2 – defining the initial states for the RNN cells
          3. Step 3 – creating the RNN using the RNN cells and their states
        6. The train method
        7. The predict method
        8. Instantiating the SentimentRNN class
        9. Training and optimizing the sentiment analysis RNN model
      5. Project two – implementing an RNN for character-level language modeling in TensorFlow
        1. Preparing the data
        2. Building a character-level RNN model
        3. The constructor
        4. The build method
        5. The train method
        6. The sample method
        7. Creating and training the CharRNN Model
        8. The CharRNN model in the sampling mode
      6. Chapter and book summary
    25. Index