How to Load Data in Python with Scikit-Learn

Before you can build machine learning models, you need to load your data into memory.

In this post you will discover how to load data for machine learning in Python using scikit-learn.

Kick-start your project with my new book Machine Learning Mastery With Python, including step-by-step tutorials and the Python source code files for all examples.

Let’s get started.

  • Update March/2018: Added alternate link to download the dataset as the original appears to have been taken down.
load csv data

Load CSV Data
Photo by Jim Makos, some rights reserved

Packaged Datasets

The scikit-learn library is packaged with datasets. These datasets are useful for getting a handle on a given machine learning algorithm or library feature before using it in your own work.

This recipe demonstrates how to load the famous Iris flowers dataset.

Load from CSV

It is very common for you to have a dataset as a CSV file on your local workstation or on a remote server.

This recipe show you how to load a CSV file from a URL, in this case the Pima Indians diabetes classification dataset.

You can learn more about the dataset here:

From the prepared X and y variables, you can train a machine learning model.

Summary

In this post you discovered that the scikit-learn method comes with packaged data sets including the iris flowers dataset. These datasets can be loaded easily and used for explore and experiment with different machine learning models.

You also saw how you can load CSV data with scikit-learn. You learned a way of opening CSV files from the web using the urllib library and how you can read that data as a NumPy matrix for use in scikit-learn.

Discover Fast Machine Learning in Python!

Master Machine Learning With Python

Develop Your Own Models in Minutes

...with just a few lines of scikit-learn code

Learn 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.

See What's Inside

19 Responses to How to Load Data in Python with Scikit-Learn

  1. Avatar
    retsreg January 12, 2015 at 7:10 am #

    It’s great to see how to load data from a URL. Do you have an example of how to actually load text from a file?

  2. Avatar
    Tarik February 24, 2015 at 8:21 am #

    I think there is a small bug.
    When code separating features to X array, it is missing the 8ths features.
    So the twelfth line should be : X = dataset[:,0:8]
    This way it will consist last features right before the targets given in the dataset.

  3. Avatar
    Robin April 5, 2015 at 6:32 am #

    Hi, thanks for this. Just a note, for Python 3*, it should be “import urllib.request” with “raw_data = urllib.request.urlopen(url)” and for Python 2* it should be “import urllib2” with “raw_data = urllib2.urlopen(url)”.

  4. Avatar
    emilia June 10, 2015 at 4:35 am #

    I am preparing a file for scikit learn and I would like to know how to build this file, I have my instances, features and classes. I am not sure how to make the file that is uploaded with scikit learn. Could you please clarify that?

    • Avatar
      mod233 March 22, 2018 at 11:46 am #

      neither do I. do you know how to build the file for scikit learn now? … i really need you help…

  5. Avatar
    abhinav August 31, 2017 at 11:36 pm #

    great blog! thanks helped a lot

  6. Avatar
    shivaprasad October 22, 2017 at 5:16 pm #

    hello sir i am a new bee to the data science i have gon through the books written by you,like machie learning mastery,machine learning algorithms from scratch and master machine learnong algorithms,i have gon through the books,next steps what i need to follow please guide me

  7. Avatar
    shivaprasad October 24, 2017 at 2:48 pm #

    thanks a lot sir

  8. Avatar
    Yuliyan December 1, 2017 at 1:53 am #

    Dear Jason,

    Great blog, thank you for that! But, it seems that Tarik is right (see his comment above). I would just have made a mistake because I applied your code. Good that I checked what the output of this operation really is. It somehow seems that when you specify the array like
    X = dataset[:,0:8] the last column is actually not included in the resulting array! So it acutally goes from 0-7 (this is what you want!). If you write X = dataset[:,0:7] then you are missing the 8-th column! The next line is correct y = dataset[:,8] this is the 9th column!

    Please, consider editing the code.

    Best
    Yuliyan

  9. Avatar
    Yohan October 11, 2020 at 12:01 pm #

    any ideas on how to learn these tutorials of iris dataset without using url , ideally i would like to exchange to a custom dataset locally to understand the code better , any help is appreciated.

Leave a Reply