數據是機器學習模型的原材料,當下機器學習的熱潮離不開大數據的支撐。在機器學習領域,有大量的公開數據集可以使用,從幾百個樣本到幾十萬個樣本的數據集都有。有些數據集被用來教學,有些被當做機器學習模型性能測試的標准(例如ImageNet圖片數據集以及相關的圖像分類比賽)。這些高質量的公開數據集為我們學習和研究機器學習算法提供了極大的便利,類似於模式生物對於生物學實驗的價值。
Iris數據集概況
Iris Data Set(鳶尾屬植物數據集)是我現在接觸到的歷史最悠久的數據集,它首次出現在著名的英國統計學家和生物學家Ronald Fisher 1936年的論文《The use of multiple measurements in taxonomic problems》中,被用來介紹線性判別式分析。在這個數據集中,包括了三類不同的鳶尾屬植物:Iris Setosa,Iris Versicolour,Iris Virginica。每類收集了50個樣本,因此這個數據集一共包含了150個樣本。
特征
該數據集測量了所有150個樣本的4個特征,分別是:
- sepal length(花萼長度)
- sepal width(花萼寬度)
- petal length(花瓣長度)
- petal width(花瓣寬度)
以上四個特征的單位都是厘米(cm)。
通常使用mm表示樣本量的大小,nn表示每個樣本所具有的特征數。因此在該數據集中,m=150,n=4m=150,n=4
數據集的獲取
該數據集被廣泛用於分類算法的示例中,很多機器學習相關的數據都對這個數據集進行了介紹,因此可以獲得的途徑應該也會很多。
下面是該數據集存放的原始位置,該位置好像已經無法下載了,但是收集了使用該數據集的論文列表可供參考:
https://archive.ics.uci.edu/ml/datasets/Iris/
另一個比較方便的獲取方式是,直接利用Python中的機器學習包scikit-learn直接導入該數據集,可參考Iris Plants Database,下面是具體的操作:
1 from sklearn.datasets import load_iris 2 data = load_iris() 3 print(dir(data)) # 查看data所具有的屬性或方法 4 print(data.DESCR) # 查看數據集的簡介 5 6 7 import pandas as pd 8 #直接讀到pandas的數據框中 9 pd.DataFrame(data=data.data, columns=data.feature_names)
下面是第3行和第4行的輸出:
['DESCR', 'data', 'feature_names', 'target', 'target_names']
Iris Plants Database
====================
Notes
-----
Data Set Characteristics:
:Number of Instances: 150 (50 in each of three classes)
:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class:
- Iris-Setosa
- Iris-Versicolour
- Iris-Virginica
:Summary Statistics:
============== ==== ==== ======= ===== ====================
Min Max Mean SD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.3 7.9 5.84 0.83 0.7826
sepal width: 2.0 4.4 3.05 0.43 -0.4194
petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
============== ==== ==== ======= ===== ====================
:Missing Attribute Values: None
:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
:Date: July, 1988
This is a copy of UCI ML iris datasets.
http://archive.ics.uci.edu/ml/datasets/Iris
The famous Iris database, first used by Sir R.A Fisher
This is perhaps the best known database to be found in the
pattern recognition literature. Fisher's paper is a classic in the field and
is referenced frequently to this day. (See Duda & Hart, for example.) The
data set contains 3 classes of 50 instances each, where each class refers to a
type of iris plant. One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.
References
----------
...
數據的可視化展示
將數據用圖像的形式展示出來,可以對該數據集有一個直觀的整體印象。下面利用該數據集4個特征中的后兩個,即花瓣的長度和寬度,來展示所有的樣本點。
1 import matplotlib.pyplot as plt
2 plt.style.use('ggplot')
3
4
5 X = data.data # 只包括樣本的特征,150x4
6 y = data.target # 樣本的類型,[0, 1, 2]
7 features = data.feature_names # 4個特征的名稱
8 targets = data.target_names # 3類鳶尾花的名稱,跟y中的3個數字對應
9
10 plt.figure(figsize=(10, 4))
11 plt.plot(X[:, 2][y==0], X[:, 3][y==0], 'bs', label=targets[0])
12 plt.plot(X[:, 2][y==1], X[:, 3][y==1], 'kx', label=targets[1])
13 plt.plot(X[:, 2][y==2], X[:, 3][y==2], 'ro', label=targets[2])
14 plt.xlabel(features[2])
15 plt.ylabel(features[3])
16 plt.title('Iris Data Set')
17 plt.legend()
18 plt.savefig('Iris Data Set.png', dpi=200)
19 plt.show()
利用上面的代碼畫出來的圖如下:

Reference
https://en.wikipedia.org/wiki/Iris_flower_data_set
https://archive.ics.uci.edu/ml/datasets/Iris/
https://matplotlib.org/users/style_sheets.html
http://scikit-learn.org/stable/datasets/index.html#iris-plants-database

