【Python载入数据】How to Load Data in Python with Scikit-Learn

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 usingscikit-learn.

Load CSV Data

Photo byJim Makos, some rights reserved

Packaged Datasets

The scikit-learn library ispackaged 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 famousIris flowers dataset.

# Load the packaged iris flowers dataset

# Iris flower dataset (4x150, reals, multi-label classification)

iris=load_iris()

print(iris)

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 thePima Indians diabetesclassification dataset from theUCI Machine Learning Repository.

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

# Load the Pima Indians diabetes dataset from CSV URL

importnumpyasnp

importurllib

# URL for the Pima Indians Diabetes dataset (UCI Machine Learning Repository)

url="http://goo.gl/j0Rvxq"

# download the file

raw_data=urllib.urlopen(url)

# load the CSV file as a numpy matrix

dataset=np.loadtxt(raw_data,delimiter=",")

print(dataset.shape)

# separate the data from the target attributes

X=dataset[:,0:7]

y=dataset[:,8]

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 theurllib libraryand how you can read that data as a NumPy matrix for use in scikit-learn.

Take The Next Step

Are you looking to get started or make the most of the scikit-learn library without getting bogged down with the mathematics and theory of the algorithms?

In this 35-page PDF guide you will discover 35 standalone scikit-learn recipes that you can copy-paste into your project.

Jump-Start Scikit-Learn

Recipes cover data handling, supervised learning algorithm, regularization, ensemble methods and advanced topics like feature selection, cross validation and parameter tuning.

If you want to get up and running with scikit-learn fast, this recipe book is for you!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容