You are previewing Learning Predictive Analytics with Python.
O'Reilly logo
Learning Predictive Analytics with Python

Book Description

Gain practical insights into predictive modelling by implementing Predictive Analytics algorithms on public datasets with Python

About This Book

  • A step-by-step guide to predictive modeling including lots of tips, tricks, and best practices

  • Get to grips with the basics of Predictive Analytics with Python

  • Learn how to use the popular predictive modeling algorithms such as Linear Regression, Decision Trees, Logistic Regression, and Clustering

  • Who This Book Is For

    If you wish to learn how to implement Predictive Analytics algorithms using Python libraries, then this is the book for you. If you are familiar with coding in Python (or some other programming/statistical/scripting language) but have never used or read about Predictive Analytics algorithms, this book will also help you. The book will be beneficial to and can be read by any Data Science enthusiasts. Some familiarity with Python will be useful to get the most out of this book, but it is certainly not a prerequisite.

    What You Will Learn

  • Understand the statistical and mathematical concepts behind Predictive Analytics algorithms and implement Predictive Analytics algorithms using Python libraries

  • Analyze the result parameters arising from the implementation of Predictive Analytics algorithms

  • Write Python modules/functions from scratch to execute segments or the whole of these algorithms

  • Recognize and mitigate various contingencies and issues related to the implementation of Predictive Analytics algorithms

  • Get to know various methods of importing, cleaning, sub-setting, merging, joining, concatenating, exploring, grouping, and plotting data with pandas and numpy

  • Create dummy datasets and simple mathematical simulations using the Python numpy and pandas libraries

  • Understand the best practices while handling datasets in Python and creating predictive models out of them

  • In Detail

    Social Media and the Internet of Things have resulted in an avalanche of data. Data is powerful but not in its raw form - It needs to be processed and modeled, and Python is one of the most robust tools out there to do so. It has an array of packages for predictive modeling and a suite of IDEs to choose from. Learning to predict who would win, lose, buy, lie, or die with Python is an indispensable skill set to have in this data age.

    This book is your guide to getting started with Predictive Analytics using Python. You will see how to process data and make predictive models from it. We balance both statistical and mathematical concepts, and implement them in Python using libraries such as pandas, scikit-learn, and numpy. You’ll start by getting an understanding of the basics of predictive modeling, then you will see how to cleanse your data of impurities and get it ready it for predictive modeling. You will also learn more about the best predictive modeling algorithms such as Linear Regression, Decision Trees, and Logistic Regression. Finally, you will see the best practices in predictive modeling, as well as the different applications of predictive modeling in the modern world.

    Style and approach

    All the concepts in this book been explained and illustrated using a dataset, and in a step-by-step manner. The Python code snippet to implement a method or concept is followed by the output, such as charts, dataset heads, pictures, and so on. The statistical concepts are explained in detail wherever required.

    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. Learning Predictive Analytics with Python
      1. Table of Contents
      2. Learning Predictive Analytics with Python
      3. Credits
      4. Foreword
      5. About the Author
      6. Acknowledgments
      7. About the Reviewer
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      9. 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
      10. 1. Getting Started with Predictive Modelling
        1. Introducing predictive modelling
          1. Scope of predictive modelling
            1. Ensemble of statistical algorithms
            2. Statistical tools
            3. Historical data
            4. Mathematical function
            5. Business context
          2. Knowledge matrix for predictive modelling
          3. Task matrix for predictive modelling
        2. Applications and examples of predictive modelling
          1. LinkedIn's "People also viewed" feature
            1. What it does?
            2. How is it done?
          2. Correct targeting of online ads
            1. How is it done?
          3. Santa Cruz predictive policing
            1. How is it done?
          4. Determining the activity of a smartphone user using accelerometer data
            1. How is it done?
          5. Sport and fantasy leagues
            1. How was it done?
        3. Python and its packages – download and installation
          1. Anaconda
          2. Standalone Python
          3. Installing a Python package
            1. Installing pip
            2. Installing Python packages with pip
        4. Python and its packages for predictive modelling
        5. IDEs for Python
        6. Summary
      11. 2. Data Cleaning
        1. Reading the data – variations and examples
          1. Data frames
          2. Delimiters
        2. Various methods of importing data in Python
          1. Case 1 – reading a dataset using the read_csv method
        3. The read_csv method
        4. Use cases of the read_csv method
          1. Passing the directory address and filename as variables
          2. Reading a .txt dataset with a comma delimiter
          3. Specifying the column names of a dataset from a list
        5. Case 2 – reading a dataset using the open method of Python
          1. Reading a dataset line by line
          2. Changing the delimiter of a dataset
        6. Case 3 – reading data from a URL
        7. Case 4 – miscellaneous cases
          1. Reading from an .xls or .xlsx file
          2. Writing to a CSV or Excel file
        8. Basics – summary, dimensions, and structure
        9. Handling missing values
          1. Checking for missing values
          2. What constitutes missing data?
            1. How missing values are generated and propagated
          3. Treating missing values
            1. Deletion
            2. Imputation
        10. Creating dummy variables
        11. Visualizing a dataset by basic plotting
          1. Scatter plots
          2. Histograms
          3. Boxplots
        12. Summary
      12. 3. Data Wrangling
        1. Subsetting a dataset
          1. Selecting columns
          2. Selecting rows
          3. Selecting a combination of rows and columns
          4. Creating new columns
        2. Generating random numbers and their usage
          1. Various methods for generating random numbers
          2. Seeding a random number
          3. Generating random numbers following probability distributions
            1. Probability density function
            2. Cumulative density function
            3. Uniform distribution
            4. Normal distribution
          4. Using the Monte-Carlo simulation to find the value of pi
            1. Geometry and mathematics behind the calculation of pi
          5. Generating a dummy data frame
        3. Grouping the data – aggregation, filtering, and transformation
          1. Aggregation
          2. Filtering
          3. Transformation
          4. Miscellaneous operations
        4. Random sampling – splitting a dataset in training and testing datasets
          1. Method 1 – using the Customer Churn Model
          2. Method 2 – using sklearn
          3. Method 3 – using the shuffle function
        5. Concatenating and appending data
        6. Merging/joining datasets
          1. Inner Join
          2. Left Join
          3. Right Join
          4. An example of the Inner Join
          5. An example of the Left Join
          6. An example of the Right Join
          7. Summary of Joins in terms of their length
        7. Summary
      13. 4. Statistical Concepts for Predictive Modelling
        1. Random sampling and the central limit theorem
        2. Hypothesis testing
          1. Null versus alternate hypothesis
          2. Z-statistic and t-statistic
          3. Confidence intervals, significance levels, and p-values
          4. Different kinds of hypothesis test
          5. A step-by-step guide to do a hypothesis test
          6. An example of a hypothesis test
        3. Chi-square tests
        4. Correlation
        5. Summary
      14. 5. Linear Regression with Python
        1. Understanding the maths behind linear regression
          1. Linear regression using simulated data
            1. Fitting a linear regression model and checking its efficacy
            2. Finding the optimum value of variable coefficients
        2. Making sense of result parameters
          1. p-values
          2. F-statistics
          3. Residual Standard Error
        3. Implementing linear regression with Python
          1. Linear regression using the statsmodel library
          2. Multiple linear regression
          3. Multi-collinearity
            1. Variance Inflation Factor
        4. Model validation
          1. Training and testing data split
          2. Summary of models
          3. Linear regression with scikit-learn
          4. Feature selection with scikit-learn
        5. Handling other issues in linear regression
          1. Handling categorical variables
          2. Transforming a variable to fit non-linear relations
          3. Handling outliers
          4. Other considerations and assumptions for linear regression
        6. Summary
      15. 6. Logistic Regression with Python
        1. Linear regression versus logistic regression
        2. Understanding the math behind logistic regression
          1. Contingency tables
          2. Conditional probability
          3. Odds ratio
          4. Moving on to logistic regression from linear regression
          5. Estimation using the Maximum Likelihood Method
            1. Likelihood function:
            2. Log likelihood function:
            3. Building the logistic regression model from scratch
          6. Making sense of logistic regression parameters
            1. Wald test
            2. Likelihood Ratio Test statistic
            3. Chi-square test
        3. Implementing logistic regression with Python
          1. Processing the data
          2. Data exploration
          3. Data visualization
          4. Creating dummy variables for categorical variables
          5. Feature selection
          6. Implementing the model
        4. Model validation and evaluation
          1. Cross validation
        5. Model validation
          1. The ROC curve
            1. Confusion matrix
        6. Summary
      16. 7. Clustering with Python
        1. Introduction to clustering – what, why, and how?
          1. What is clustering?
          2. How is clustering used?
          3. Why do we do clustering?
        2. Mathematics behind clustering
          1. Distances between two observations
            1. Euclidean distance
            2. Manhattan distance
            3. Minkowski distance
            4. The distance matrix
          2. Normalizing the distances
          3. Linkage methods
            1. Single linkage
            2. Compete linkage
            3. Average linkage
            4. Centroid linkage
            5. Ward's method
          4. Hierarchical clustering
          5. K-means clustering
        3. Implementing clustering using Python
          1. Importing and exploring the dataset
          2. Normalizing the values in the dataset
          3. Hierarchical clustering using scikit-learn
          4. K-Means clustering using scikit-learn
            1. Interpreting the cluster
        4. Fine-tuning the clustering
          1. The elbow method
          2. Silhouette Coefficient
        5. Summary
      17. 8. Trees and Random Forests with Python
        1. Introducing decision trees
          1. A decision tree
        2. Understanding the mathematics behind decision trees
          1. Homogeneity
          2. Entropy
          3. Information gain
          4. ID3 algorithm to create a decision tree
          5. Gini index
          6. Reduction in Variance
          7. Pruning a tree
          8. Handling a continuous numerical variable
          9. Handling a missing value of an attribute
        3. Implementing a decision tree with scikit-learn
          1. Visualizing the tree
          2. Cross-validating and pruning the decision tree
        4. Understanding and implementing regression trees
          1. Regression tree algorithm
          2. Implementing a regression tree using Python
        5. Understanding and implementing random forests
          1. The random forest algorithm
          2. Implementing a random forest using Python
          3. Why do random forests work?
          4. Important parameters for random forests
        6. Summary
      18. 9. Best Practices for Predictive Modelling
        1. Best practices for coding
          1. Commenting the codes
          2. Defining functions for substantial individual tasks
            1. Example 1
            2. Example 2
            3. Example 3
          3. Avoid hard-coding of variables as much as possible
          4. Version control
          5. Using standard libraries, methods, and formulas
        2. Best practices for data handling
        3. Best practices for algorithms
        4. Best practices for statistics
        5. Best practices for business contexts
        6. Summary
      19. A. A List of Links
      20. Index