鳶尾花(iris)數(shù)據(jù)集分析

??Iris 鳶尾花數(shù)據(jù)集是一個(gè)經(jīng)典數(shù)據(jù)集溉潭,在統(tǒng)計(jì)學(xué)習(xí)和機(jī)器學(xué)習(xí)領(lǐng)域都經(jīng)常被用作示例倦微。數(shù)據(jù)集內(nèi)包含 3 類共 150 條記錄弄兜,每類各 50 個(gè)數(shù)據(jù)药蜻,每條記錄都有 4 項(xiàng)特征:花萼長(zhǎng)度、花萼寬度挨队、花瓣長(zhǎng)度谷暮、花瓣寬度蒿往,可以通過這4個(gè)特征預(yù)測(cè)鳶尾花卉屬于(iris-setosa, iris-versicolour, iris-virginica)中的哪一品種盛垦。

據(jù)說在現(xiàn)實(shí)中,這三種花的基本判別依據(jù)其實(shí)是種子(因?yàn)榛ò攴浅H菀卓菸?/p>

0 準(zhǔn)備數(shù)據(jù)

??下面對(duì) iris 進(jìn)行探索性分析瓤漏,首先導(dǎo)入相關(guān)包和數(shù)據(jù)集:

# 導(dǎo)入相關(guān)包
import numpy as np
import pandas as pd
from pandas import plotting

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn')

import seaborn as sns
sns.set_style("whitegrid")

from sklearn.linear_model import LogisticRegression 
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.neighbors import KNeighborsClassifier
from sklearn import svm
from sklearn import metrics 
from sklearn.tree import DecisionTreeClassifier
# 導(dǎo)入數(shù)據(jù)集
iris = pd.read_csv('F:\pydata\dataset\kaggle\iris.csv', usecols=[1, 2, 3, 4, 5])

??查看數(shù)據(jù)集信息:

iris.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 5 columns):
SepalLengthCm    150 non-null float64
SepalWidthCm     150 non-null float64
PetalLengthCm    150 non-null float64
PetalWidthCm     150 non-null float64
Species          150 non-null object
dtypes: float64(4), object(1)
memory usage: 5.9+ KB

??查看數(shù)據(jù)集的頭 5 條記錄:

iris.head()

1 探索性分析

??先查看數(shù)據(jù)集各特征列的摘要統(tǒng)計(jì)信息:

iris.describe()

??通過Violinplot 和 Pointplot腾夯,分別從數(shù)據(jù)分布和斜率,觀察各特征與品種之間的關(guān)系:

# 設(shè)置顏色主題
antV = ['#1890FF', '#2FC25B', '#FACC14', '#223273', '#8543E0', '#13C2C2', '#3436c7', '#F04864'] 
# 繪制  Violinplot
f, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True)
sns.despine(left=True)

sns.violinplot(x='Species', y='SepalLengthCm', data=iris, palette=antV, ax=axes[0, 0])
sns.violinplot(x='Species', y='SepalWidthCm', data=iris, palette=antV, ax=axes[0, 1])
sns.violinplot(x='Species', y='PetalLengthCm', data=iris, palette=antV, ax=axes[1, 0])
sns.violinplot(x='Species', y='PetalWidthCm', data=iris, palette=antV, ax=axes[1, 1])

plt.show()
# 繪制  pointplot
f, axes = plt.subplots(2, 2, figsize=(8, 8), sharex=True)
sns.despine(left=True)

sns.pointplot(x='Species', y='SepalLengthCm', data=iris, color=antV[0], ax=axes[0, 0])
sns.pointplot(x='Species', y='SepalWidthCm', data=iris, color=antV[0], ax=axes[0, 1])
sns.pointplot(x='Species', y='PetalLengthCm', data=iris, color=antV[0], ax=axes[1, 0])
sns.pointplot(x='Species', y='PetalWidthCm', data=iris, color=antV[0], ax=axes[1, 1])

plt.show()

??生成各特征之間關(guān)系的矩陣圖:

g = sns.pairplot(data=iris, palette=antV, hue= 'Species')

??使用 Andrews Curves 將每個(gè)多變量觀測(cè)值轉(zhuǎn)換為曲線并表示傅立葉級(jí)數(shù)的系數(shù)蔬充,這對(duì)于檢測(cè)時(shí)間序列數(shù)據(jù)中的異常值很有用蝶俱。

Andrews Curves 是一種通過將每個(gè)觀察映射到函數(shù)來可視化多維數(shù)據(jù)的方法。

plt.subplots(figsize = (10,8))
plotting.andrews_curves(iris, 'Species', colormap='cool')

plt.show()

??下面分別基于花萼和花瓣做線性回歸的可視化:

g = sns.lmplot(data=iris, x='SepalWidthCm', y='SepalLengthCm', palette=antV, hue='Species')
g = sns.lmplot(data=iris, x='PetalWidthCm', y='PetalLengthCm', palette=antV, hue='Species')

??最后饥漫,通過熱圖找出數(shù)據(jù)集中不同特征之間的相關(guān)性榨呆,高正值或負(fù)值表明特征具有高度相關(guān)性:

fig=plt.gcf()
fig.set_size_inches(12, 8)
fig=sns.heatmap(iris.corr(), annot=True, cmap='GnBu', linewidths=1, linecolor='k', square=True, mask=False, vmin=-1, vmax=1, cbar_kws={"orientation": "vertical"}, cbar=True)

??從熱圖可看出,花萼的寬度和長(zhǎng)度不相關(guān)庸队,而花瓣的寬度和長(zhǎng)度則高度相關(guān)积蜻。

2 機(jī)器學(xué)習(xí)

??接下來,通過機(jī)器學(xué)習(xí)彻消,以花萼和花瓣的尺寸為根據(jù)竿拆,預(yù)測(cè)其品種。

??在進(jìn)行機(jī)器學(xué)習(xí)之前宾尚,將數(shù)據(jù)集拆分為訓(xùn)練和測(cè)試數(shù)據(jù)集丙笋。首先,使用標(biāo)簽編碼將 3 種鳶尾花的品種名稱轉(zhuǎn)換為分類值(0, 1, 2)煌贴。

# 載入特征和標(biāo)簽集
X = iris[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
y = iris['Species']
# 對(duì)標(biāo)簽集進(jìn)行編碼
encoder = LabelEncoder()
y = encoder.fit_transform(y)
print(y)
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 2 2]

??接著御板,將數(shù)據(jù)集以 7: 3 的比例,拆分為訓(xùn)練數(shù)據(jù)和測(cè)試數(shù)據(jù):

train_X, test_X, train_y, test_y = train_test_split(X, y, test_size = 0.3, random_state = 101)
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
(105, 4) (105,) (45, 4) (45,)

??檢查不同模型的準(zhǔn)確性:

# Support Vector Machine
model = svm.SVC()
model.fit(train_X, train_y)
prediction = model.predict(test_X)
print('The accuracy of the SVM is: {0}'.format(metrics.accuracy_score(prediction,test_y)))
The accuracy of the SVM is: 1.0
# Logistic Regression
model = LogisticRegression()
model.fit(train_X, train_y)
prediction = model.predict(test_X)
print('The accuracy of the Logistic Regression is: {0}'.format(metrics.accuracy_score(prediction,test_y)))
The accuracy of the Logistic Regression is: 0.9555555555555556
# Decision Tree
model=DecisionTreeClassifier()
model.fit(train_X, train_y)
prediction = model.predict(test_X)
print('The accuracy of the Decision Tree is: {0}'.format(metrics.accuracy_score(prediction,test_y)))
The accuracy of the Decision Tree is: 0.9555555555555556
# K-Nearest Neighbours
model=KNeighborsClassifier(n_neighbors=3)
model.fit(train_X, train_y)
prediction = model.predict(test_X)
print('The accuracy of the KNN is: {0}'.format(metrics.accuracy_score(prediction,test_y)))
The accuracy of the KNN is: 1.0

??上面使用了數(shù)據(jù)集的所有特征牛郑,下面將分別使用花瓣和花萼的尺寸:

petal = iris[['PetalLengthCm', 'PetalWidthCm', 'Species']]
train_p,test_p=train_test_split(petal,test_size=0.3,random_state=0) 
train_x_p=train_p[['PetalWidthCm','PetalLengthCm']]
train_y_p=train_p.Species
test_x_p=test_p[['PetalWidthCm','PetalLengthCm']]
test_y_p=test_p.Species

sepal = iris[['SepalLengthCm', 'SepalWidthCm', 'Species']]
train_s,test_s=train_test_split(sepal,test_size=0.3,random_state=0)
train_x_s=train_s[['SepalWidthCm','SepalLengthCm']]
train_y_s=train_s.Species
test_x_s=test_s[['SepalWidthCm','SepalLengthCm']]
test_y_s=test_s.Species
model=svm.SVC()

model.fit(train_x_p,train_y_p) 
prediction=model.predict(test_x_p) 
print('The accuracy of the SVM using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s,train_y_s) 
prediction=model.predict(test_x_s) 
print('The accuracy of the SVM using Sepal is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))
The accuracy of the SVM using Petals is: 0.9777777777777777
The accuracy of the SVM using Sepal is: 0.8
model = LogisticRegression()

model.fit(train_x_p, train_y_p) 
prediction = model.predict(test_x_p) 
print('The accuracy of the Logistic Regression using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s, train_y_s) 
prediction = model.predict(test_x_s) 
print('The accuracy of the Logistic Regression using Sepals is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))
The accuracy of the Logistic Regression using Petals is: 0.6888888888888889
The accuracy of the Logistic Regression using Sepals is: 0.6444444444444445
model=DecisionTreeClassifier()

model.fit(train_x_p, train_y_p) 
prediction = model.predict(test_x_p) 
print('The accuracy of the Decision Tree using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s, train_y_s) 
prediction = model.predict(test_x_s) 
print('The accuracy of the Decision Tree using Sepals is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))
The accuracy of the Decision Tree using Petals is: 0.9555555555555556
The accuracy of the Decision Tree using Sepals is: 0.6666666666666666
model=KNeighborsClassifier(n_neighbors=3) 

model.fit(train_x_p, train_y_p) 
prediction = model.predict(test_x_p) 
print('The accuracy of the KNN using Petals is: {0}'.format(metrics.accuracy_score(prediction,test_y_p)))

model.fit(train_x_s, train_y_s) 
prediction = model.predict(test_x_s) 
print('The accuracy of the KNN using Sepals is: {0}'.format(metrics.accuracy_score(prediction,test_y_s)))
The accuracy of the KNN using Petals is: 0.9777777777777777
The accuracy of the KNN using Sepals is: 0.7333333333333333

??從中不難看出怠肋,使用花瓣的尺寸來訓(xùn)練數(shù)據(jù)較花萼更準(zhǔn)確。正如在探索性分析的熱圖中所看到的那樣井濒,花萼的寬度和長(zhǎng)度之間的相關(guān)性非常低灶似,而花瓣的寬度和長(zhǎng)度之間的相關(guān)性非常高列林。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市酪惭,隨后出現(xiàn)的幾起案子希痴,更是在濱河造成了極大的恐慌,老刑警劉巖春感,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件砌创,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡鲫懒,警方通過查閱死者的電腦和手機(jī)嫩实,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來窥岩,“玉大人甲献,你說我怎么就攤上這事∷桃恚” “怎么了晃洒?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)朦乏。 經(jīng)常有香客問我球及,道長(zhǎng),這世上最難降的妖魔是什么呻疹? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任吃引,我火速辦了婚禮,結(jié)果婚禮上刽锤,老公的妹妹穿的比我還像新娘镊尺。我一直安慰自己,他們只是感情好姑蓝,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布鹅心。 她就那樣靜靜地躺著,像睡著了一般纺荧。 火紅的嫁衣襯著肌膚如雪旭愧。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天宙暇,我揣著相機(jī)與錄音输枯,去河邊找鬼。 笑死占贫,一個(gè)胖子當(dāng)著我的面吹牛桃熄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播型奥,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼瞳收,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼碉京!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起螟深,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤谐宙,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后界弧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凡蜻,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年垢箕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了划栓。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡条获,死狀恐怖忠荞,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情月匣,我是刑警寧澤钻洒,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布奋姿,位于F島的核電站锄开,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏称诗。R本人自食惡果不足惜萍悴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望寓免。 院中可真熱鬧癣诱,春花似錦、人聲如沸袜香。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蜈首。三九已至实抡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間欢策,已是汗流浹背吆寨。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留踩寇,地道東北人啄清。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像俺孙,于是被迫代替她去往敵國(guó)和親辣卒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子掷贾,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容