Understand Your Machine Learning Data With Descriptive Statistics in Python

You must understand your data in order to get the best results.

In this post you will discover 7 recipes that you can use in Python to learn more about your machine learning data.

Let’s get started.

Understand Your Machine Learning Data With Descriptive Statistics in Python

Understand Your Machine Learning Data With Descriptive Statistics in Python
Photo by passer-by, some rights reserved.

Python Recipes To Understand Your Machine Learning Data

This section lists 7 recipes that you can use to better understand your machine learning data.

Each recipe is demonstrated by loading the Pima Indians Diabetes classification dataset from the UCI Machine Learning repository.

Open your python interactive environment and try each recipe out in turn.

Need help with Machine Learning in Python?

Take my free 2-week email course and discover data prep, algorithms and more (with sample code).

Click to sign-up now and also get a free PDF Ebook version of the course.

Start Your FREE Mini-Course Now!

1. Peek at Your Data

There is no substitute for looking at the raw data.

Looking at the raw data can reveal insights that you cannot get any other way. It can also plant seeds that may later grow into ideas on how to better preprocess and handle the data for machine learning tasks.

You can review the first 20 rows of your data using the head() function on the Pandas DataFrame.

You can see that the first column lists the row number, which is handy for referencing a specific observation.

2. Dimensions of Your Data

You must have a very good handle on how much data you have, both in terms of rows and columns.

  • Too many rows and algorithms may take too long to train. Too few and perhaps you do not have enough data to train the algorithms.
  • Too many features and some algorithms can be distracted or suffer poor performance due to the curse of dimensionality.

You can review the shape and size of your dataset by printing the shape property on the Pandas DataFrame.

The results are listed in rows then columns. You can see that the dataset has 768 rows and 9 columns.

3. Data Type For Each Attribute

The type of each attribute is important.

Strings may need to be converted to floating point values or integers to represent categorical or ordinal values.

You can get an idea of the types of attributes by peeking at the raw data, as above. You can also list the data types used by the DataFrame to characterize each attribute using the dtypes property.

You can see that most of the attributes are integers and that mass and pedi are floating point values.

4. Descriptive Statistics

Descriptive statistics can give you great insight into the shape of each attribute.

Often you can create more summaries than you have time to review. The describe() function on the Pandas DataFrame lists 8 statistical properties of each attribute:

  • Count
  • Mean
  • Standard Devaition
  • Minimum Value
  • 25th Percentile
  • 50th Percentile (Median)
  • 75th Percentile
  • Maximum Value

You can see that you do get a lot of data. You will note some calls to pandas.set_option() in the recipe to change the precision of the numbers and the preferred width of the output. This is to make it more readable for this example.

When describing your data this way, it is worth taking some time and reviewing observations from the results. This might include the presence of “NA” values for missing data or surprising distributions for attributes.

5. Class Distribution (Classification Only)

On classification problems you need to know how balanced the class values are.

Highly imbalanced problems (a lot more observations for one class than another) are common and may need special handling in the data preparation stage of your project.

You can quickly get an idea of the distribution of the class attribute in Pandas.

You can see that there are nearly double the number of observations with class 0 (no onset of diabetes) than there are with class 1 (onset of diabetes).

6. Correlation Between Attributes

Correlation refers to the relationship between two variables and how they may or may not change together.

The most common method for calculating correlation is Pearson’s Correlation Coefficient, that assumes a normal distribution of the attributes involved. A correlation of -1 or 1 shows a full negative or positive correlation respectively. Whereas a value of 0 shows no correlation at all.

Some machine learning algorithms like linear and logistic regression can suffer poor performance if there are highly correlated attributes in your dataset. As such, it is a good idea to review all of the pair-wise correlations of the attributes in your dataset. You can use the corr() function on the Pandas DataFrame to calculate a correlation matrix.

The matrix lists all attributes across the top and down the side, to give correlation between all pairs of attributes (twice, because the matrix is symmetrical). You can see the diagonal line through the matrix from the top left to bottom right corners of the matrix shows perfect correlation of each attribute with itself.

7. Skew of Univariate Distributions

Skew refers to a distribution that is assumed Gaussian (normal or bell curve) that is shifted or squashed in one direction or another.

Many machine learning algorithms assume a Gaussian distribution. Knowing that an attribute has a skew may allow you to perform data preparation to correct the skew and later improve the accuracy of your models.

You can calculate the skew of each attribute using the skew() function on the Pandas DataFrame.

The skew result show a positive (right) or negative (left) skew. Values closer to zero show less skew.

More Recipes

This was just a selection of the most useful summaries and descriptive statistics that you can use on your machine learning data for classification and regression.

There are many other statistics that you could calculate.

Is there a specific statistic that you like to calculate and review when you start working on a new data set? Leave a comment and let me know.

Tips To Remember

This section gives you some tips to remember when reviewing your data using summary statistics.

  • Review the numbers. Generating the summary statistics is not enough. Take a moment to pause, read and really think about the numbers you are seeing.
  • Ask why. Review your numbers and ask a lot of questions. How and why are you seeing specific numbers. Think about how the numbers relate to the problem domain in general and specific entities that observations relate to.
  • Write down ideas. Write down your observations and ideas. Keep a small text file or note pad and jot down all of the ideas for how variables may relate, for what numbers mean, and ideas for techniques to try later. The things you write down now while the data is fresh will be very valuable later when you are trying to think up new things to try.

Summary

In this post you discovered the importance of describing your dataset before you start work on your machine learning project.

You discovered 7 different ways to summarize your dataset using Python and Pandas:

  1. Peek At Your Data
  2. Dimensions of Your Data
  3. Data Types
  4. Class Distribution
  5. Data Summary
  6. Correlations
  7. Skewness

Action Step

  1. Open your Python interactive environment.
  2. Type or copy-and-paste each recipe and see how it works.
  3. Let me know how you go in the comments.

Do you have any questions about Python, Pandas or the recipes in this post? Leave a comment and ask your question, I will do my best to answer it.


Frustrated With Python Machine Learning?

Master Machine Learning With Python

Develop Your Own Models in Minutes

…with just a few lines of scikit-learn code

Discover how in my new Ebook:
Machine Learning Mastery With Python

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, modeling, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

Click to learn more.


11 Responses to Understand Your Machine Learning Data With Descriptive Statistics in Python

  1. M. Wilson August 28, 2016 at 8:23 pm #

    Excellent write-up. I definitely appreciate this site. Continue the good
    work!

    • Jason Brownlee August 29, 2016 at 8:07 am #

      Thanks M. Willson, I’m glad you found it useful.

  2. Mayur Agarwal April 19, 2017 at 10:11 pm #

    Hi Jason .

    Thankyou for the explanation .

    it is very clear and well understood many things from the article .

    Perhaps i have a doubt in understand the purpose of each step as like what message it is conveying with respect to the data. for example ” Descriptive Statistics” give some many output with respect to the data but what do i understand from that.

    Seeking for some clarification on the same.

    • Jason Brownlee April 20, 2017 at 9:26 am #

      We are understanding the univariate distribution of each feature.

      This can help in data preparation and algorithm selection.

  3. Neeraj Wagh May 2, 2017 at 4:48 pm #

    Hey Jason, amazingly concise and effective post, as always.

    Any suggestions on how to do exploratory analysis with binary features?

    Please don’t stop your work, it’s immensely helpful. 🙂

    • Jason Brownlee May 3, 2017 at 7:32 am #

      Thanks, glad to hear it Neeraj.

      A good start with binary and categorical variables is to look at proportions by level.

  4. Rizwan Mian, PhD August 1, 2017 at 11:55 am #

    “Is there a specific statistic that you like to calculate and review when you start working on a new data set?”

    > in addition to the descriptive stats given by .describe(), I like to calculate:
    – median
    – 95%-ile

  5. Sarra,Data Scientist October 2, 2017 at 10:38 pm #

    Hi, I want to thank you for your useful and well explained articles, specially this one.. But I would like to ask you about imbalanced data and resampling effectiveness if you don’t mind.

    Well, as you have stated in your other article, in some cases the imbalance is so natural because like in my case where I have mainly 2 classes with this data distribution (A: 6418, B: 81) the class B is a rare phenomena that we are looking to understand its reasons.

    I’m more interested in class B, but I’m afraid that resampling changed a lot in my dada since I noticed a change in correlation after applying an undersampling for class A and oversampling for B to get 1000:1000 samples in final dataset.

    total correlation of my 6 features with the target feature passed from 0.14 to 0.67 which I find somehow artificial and not realistic.

    If you can help me understand this, I will be so grateful. And thanks for the wonderful website 🙂

Leave a Reply