2017年4月14日 星期五

[ Python 常見問題 ] How to Load Data in Python with Scikit-Learn

Source From Here 
Question 
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


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. Below recipe demonstrates how to load the famous Iris flowers dataset
>>> from sklearn.datasets import load_iris
>>> iris = load_iris()
>>> type(iris)

>>> X = iris.data # Store feature matrix in 'X' 
>>> y = iris.target # Store response vector in 'y'
>>> print X.shape
(150, 4) # There are 150 records with 4 features
>>> type(X)

>>> print y.shape
(150,)

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. 

From the prepared X and y variables, you can train a machine learning model: 
>>> import numpy as np
>>> import urllib
>>> url = "http://goo.gl/j0Rvxq" # URL for the Pima Indians Diabetes dataset (UCI Machine Learning Repository)
>>> raw_data = urllib.urlopen(url) # download the file
>>> dataset = np.loadtxt(raw_data, delimiter=',') # load the CSV file as a numpy matrix
>>> print dataset.shape
(768, 9)

# Separate the data from the target attributes
>>> X = dataset[:,0:7] # The front 7 columns are features
>>> y = dataset[:,8] # The last column is label/class
>>> X.shape # Now we have 768 records with 7 features as data set
(768, 7)
>>> y.shape
(768,)

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

Supplement 
Machine Learning: Python 機器學習:使­用Pytho­n 
[ Scikit- learn ] Training a machine learning model with scikit-learn

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...