You are previewing Python Machine Learning.
O'Reilly logo
Python Machine Learning

Book Description

Unlock deeper insights into Machine Leaning with this vital guide to cutting-edge predictive analytics

About This Book

  • Leverage Python’s most powerful open-source libraries for deep learning, data wrangling, and data visualization

  • Learn effective strategies and best practices to improve and optimize machine learning systems and algorithms

  • Ask – and answer – tough questions of your data with robust statistical models, built for a range of datasets

  • Who This Book Is For

    If you want to find out how to use Python to start answering critical questions of your data, pick up Python Machine Learning – whether you want to get started from scratch or want to extend your data science knowledge, this is an essential and unmissable resource.

    What You Will Learn

  • Explore how to use different machine learning models to ask different questions of your data

  • Learn how to build neural networks using Pylearn 2 and Theano

  • Find out how to write clean and elegant Python code that will optimize the strength of your algorithms

  • Discover how to embed your machine learning model in a web application for increased accessibility

  • Predict continuous target outcomes using regression analysis

  • Uncover hidden patterns and structures in data with clustering

  • Organize data using effective pre-processing techniques

  • Get to grips with sentiment analysis to delve deeper into textual and social media data

  • In Detail

    Machine learning and predictive analytics are transforming the way businesses and other organizations operate. Being able to understand trends and patterns in complex data is critical to success, becoming one of the key strategies for unlocking growth in a challenging contemporary marketplace. Python can help you deliver key insights into your data – its unique capabilities as a language let you build sophisticated algorithms and statistical models that can reveal new perspectives and answer key questions that are vital for success.

    Python Machine Learning gives you access to the world of predictive analytics and demonstrates why Python is one of the world’s leading data science languages. If you want to ask better questions of data, or need to improve and extend the capabilities of your machine learning systems, this practical data science book is invaluable. Covering a wide range of powerful Python libraries, including scikit-learn, Theano, and Pylearn2, and featuring guidance and tips on everything from sentiment analysis to neural networks, you’ll soon be able to answer some of the most important questions facing you and your organization.

    Style and approach

    Python Machine Learning connects the fundamental theoretical principles behind machine learning to their practical application in a way that focuses you on asking and answering the right questions. It walks you through the key elements of Python and its powerful machine learning libraries, while demonstrating how to get to grips with a range of statistical models.

    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 If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Python Machine Learning
      1. Table of Contents
      2. Python Machine Learning
      3. Credits
      4. Foreword
      5. About the Author
      6. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      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. Errata
          3. Piracy
          4. 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. An 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 packages
        6. Summary
      10. 2. Training Machine Learning Algorithms for Classification
        1. Artificial neurons – a brief glimpse into the early history of machine learning
        2. Implementing a perceptron learning algorithm in Python
          1. 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 an Adaptive Linear Neuron in Python
          3. 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
          1. Training a perceptron via scikit-learn
        3. Modeling class probabilities via logistic regression
          1. Logistic regression intuition and conditional probabilities
          2. Learning the weights of the logistic cost function
          3. Training a logistic regression model with scikit-learn
          4. Tackling overfitting via regularization
        4. Maximum margin classification with support vector machines
          1. Maximum margin intuition
          2. Dealing with the nonlinearly separable case using slack variables
          3. Alternative implementations in scikit-learn
        5. Solving nonlinear problems using a kernel SVM
          1. Using the kernel trick to find separating hyperplanes in higher dimensional space
        6. Decision tree learning
          1. Maximizing information gain – getting the most bang for the buck
          2. Building a decision tree
          3. Combining weak to strong learners 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. Eliminating samples or features with missing values
          2. Imputing missing values
          3. Understanding the scikit-learn estimator API
        2. Handling categorical data
          1. Mapping ordinal features
          2. Encoding class labels
          3. Performing one-hot encoding on nominal features
        3. Partitioning a dataset in training and test sets
        4. Bringing features onto the same scale
        5. Selecting meaningful features
          1. Sparse solutions with L1 regularization
          2. 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. Total and explained variance
          2. Feature transformation
          3. Principal component analysis in scikit-learn
        2. Supervised data compression via linear discriminant analysis
          1. Computing the scatter matrices
          2. Selecting linear discriminants for the new feature subspace
          3. Projecting samples onto the new feature space
          4. 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 overfitting 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. The scoring metrics for multiclass classification
        6. Summary
      15. 7. Combining Different Models for Ensemble Learning
        1. Learning with ensembles
        2. Implementing a simple majority vote classifier
          1. Combining different algorithms for classification with majority vote
        3. Evaluating and tuning the ensemble classifier
        4. Bagging – building an ensemble of classifiers from bootstrap samples
        5. Leveraging weak learners via adaptive boosting
        6. Summary
      16. 8. Applying Machine Learning to Sentiment Analysis
        1. Obtaining the IMDb movie review dataset
        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. Summary
      17. 9. Embedding a Machine Learning Model into a Web Application
        1. Serializing fitted scikit-learn estimators
        2. Setting up a SQLite database for data storage
        3. Developing a web application with Flask
          1. Our first Flask web application
          2. Form validation and rendering
        4. Turning the movie classifier into a web application
        5. Deploying the web application to a public server
          1. Updating the movie review classifier
        6. Summary
      18. 10. Predicting Continuous Target Variables with Regression Analysis
        1. Introducing a simple linear regression model
        2. Exploring the Housing Dataset
          1. Visualizing the important characteristics of a dataset
        3. Implementing an ordinary least squares linear regression model
          1. Solving regression for regression parameters with gradient descent
          2. Estimating the 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. Modeling nonlinear relationships in the Housing Dataset
          2. Dealing with nonlinear relationships using random forests
            1. Decision tree regression
            2. Random forest regression
        8. Summary
      19. 11. Working with Unlabeled Data – Clustering Analysis
        1. Grouping objects by similarity using k-means
          1. K-means++
          2. Hard versus soft clustering
          3. Using the elbow method to find the optimal number of clusters
          4. Quantifying the quality of clustering via silhouette plots
        2. Organizing clusters as a hierarchical tree
          1. Performing hierarchical clustering on a distance matrix
          2. Attaching dendrograms to a heat map
          3. Applying agglomerative clustering via scikit-learn
        3. Locating regions of high density via DBSCAN
        4. Summary
      20. 12. Training Artificial Neural Networks for Image Recognition
        1. Modeling complex functions with artificial neural networks
          1. Single-layer neural network recap
          2. Introducing the multi-layer neural network architecture
          3. Activating a neural network via forward propagation
        2. Classifying handwritten digits
          1. Obtaining the MNIST dataset
          2. Implementing a multi-layer perceptron
        3. Training an artificial neural network
          1. Computing the logistic cost function
          2. Training neural networks via backpropagation
        4. Developing your intuition for backpropagation
        5. Debugging neural networks with gradient checking
        6. Convergence in neural networks
        7. Other neural network architectures
          1. Convolutional Neural Networks
          2. Recurrent Neural Networks
        8. A few last words about neural network implementation
        9. Summary
      21. 13. Parallelizing Neural Network Training with Theano
        1. Building, compiling, and running expressions with Theano
          1. What is Theano?
          2. First steps with Theano
          3. Configuring Theano
          4. Working with array structures
          5. Wrapping things up – a linear regression example
        2. Choosing activation functions for feedforward neural networks
          1. Logistic function recap
          2. Estimating probabilities in multi-class classification via the softmax function
          3. Broadening the output spectrum by using a hyperbolic tangent
        3. Training neural networks efficiently using Keras
        4. Summary
      22. Index