PYTHON可视化

散点图绘制:
'''
需要使用的包:
from pylab import plot, show   #plot详见拓展1
参数ft为特征,msample为样本,数据导入时创建
col为指定要分析的特征列,即花萼的长(0),花瓣的长(1),花萼的宽(2),花瓣的宽
'''
def scatter_plot(ft,msample,col1,col2):
    plot(ft[msample=='setosa',col1],ft[msample=='setosa',col2],'bo')  #山鸢尾,ft[]中参数是条件索引
    plot(ft[msample=='versicolor',col1],ft[msample=='versicolor',col2],'ro') #变色鸢尾
    plot(ft[msample=='virginica',col1],ft[msample=='virginica',col2],'go') #维吉尼亚鸢尾
    show()

使用第一和第三维度(花萼的长和宽),结果如下图所示:


PYTHON可视化_第1张图片
直方图绘制:
'''
需要使用的包:
from pylab import figure, subplot, hist, xlim, show
参数ft为特征,msample为样本,数据导入时创建
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()

选择第二维度即选择花萼的宽做分析,情况如下:


PYTHON可视化_第2张图片

根据上图的直方图,我们可以根据样本类型区分理解样本的特征。例如,我们可以观察到,山鸢尾的平均花萼宽度小于变色鸢尾和维吉尼亚鸢尾,变色鸢尾的平均花萼宽度小于维吉尼亚鸢尾,山鸢尾与变色鸢尾的平均花萼宽度之差要大于变色鸢尾与维吉尼亚鸢尾的平均花萼宽度之差,说明山鸢尾的花萼最窄,和其他两种鸢尾的花萼宽度差异明显。

拓展:

  1. plot:绘图,和matlab用法类似
    http://matplotlib.org/api/pyplot_api.html
    http://blog.csdn.net/ywjun0919/article/details/8692018

你可能感兴趣的:(PYTHON可视化)