How to Load Data in Python with Scikit-Learn

Last Updated on

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.

Discover how to prepare data with pandas, fit and evaluate models with scikit-learn, and more in my new book, with 16 step-by-step tutorials, 3 projects, and full python code.

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

Need help with Machine Learning in Python?

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

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

Start Your FREE Mini-Course Now!

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 from the UCI Machine Learning Repository (update: download from 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.

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.

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

  1. 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. 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. 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. 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?

    • 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. abhinav August 31, 2017 at 11:36 pm #

    great blog! thanks helped a lot

  6. 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. shivaprasad October 24, 2017 at 2:48 pm #

    thanks a lot sir

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

Leave a Reply