散點圖繪制:
'''
需要使用的包:
from pylab import plot, show #plot詳見拓展1
參數(shù)ft為特征,msample為樣本,數(shù)據(jù)導(dǎo)入時創(chuàng)建
col為指定要分析的特征列,即花萼的長(0),花瓣的長(1),花萼的寬(2),花瓣的寬
'''
def scatter_plot(ft,msample,col1,col2):
plot(ft[msample=='setosa',col1],ft[msample=='setosa',col2],'bo') #山鳶尾,ft[]中參數(shù)是條件索引
plot(ft[msample=='versicolor',col1],ft[msample=='versicolor',col2],'ro') #變色鳶尾
plot(ft[msample=='virginica',col1],ft[msample=='virginica',col2],'go') #維吉尼亞鳶尾
show()
使用第一和第三維度(花萼的長和寬),結(jié)果如下圖所示:

直方圖繪制:
'''
需要使用的包:
from pylab import figure, subplot, hist, xlim, show
參數(shù)ft為特征,msample為樣本,數(shù)據(jù)導(dǎo)入時創(chuàng)建
col為指定要分析的特征列,即花萼的長(0),花瓣的長(1),花萼的寬(2),花瓣的寬(2)
'''
def hist_plot(ft,msample,col):
xmin = min(ft[:,col])
xmax = max(ft[:,col])
figure()
subplot(411) # distribution of the setosa class (1st, on the top)
hist(ft[msample=='setosa',col],color='b',alpha=.8) #山鳶尾
xlim(xmin,xmax)
subplot(412) # distribution of the versicolor class (2nd)
hist(ft[msample=='versicolor',col],color='r',alpha=.8) #變色鳶尾
xlim(xmin,xmax)
subplot(413) # distribution of the virginica class (3rd)
hist(ft[msample=='virginica',col],color='g',alpha=.8) #維吉尼亞鳶尾
xlim(xmin,xmax)
subplot(414) # global histogram (4th, on the bottom)
hist(ft[:,col],color='y',alpha=.8)
xlim(xmin,xmax)
show()
選擇第二維度即選擇花萼的寬做分析,情況如下:

根據(jù)上圖的直方圖,我們可以根據(jù)樣本類型區(qū)分理解樣本的特征。例如,我們可以觀察到,山鳶尾的平均花萼寬度小于變色鳶尾和維吉尼亞鳶尾,變色鳶尾的平均花萼寬度小于維吉尼亞鳶尾,山鳶尾與變色鳶尾的平均花萼寬度之差要大于變色鳶尾與維吉尼亞鳶尾的平均花萼寬度之差,說明山鳶尾的花萼最窄,和其他兩種鳶尾的花萼寬度差異明顯。
拓展: