使用matplotlib中的scatter繪圖函數分析iris數據集中的特徵屬性

iris數據集導入有好幾種方式,本文采用的是從sklearn包中直接導入使用,並選取期中兩列的屬性,用散點圖分析,
代碼參考了《機器學習實戰》
# import numpy as np
# from numpy import *
# import  operator
from sklearn.datasets import load_iris
# import matplotlib
import matplotlib.pyplot as plt
def huitu(a,b):
    #加載數據,獲取數據的特徵屬性列表、數據、分類信息
    dataSet=load_iris()
    #獲取屬性標籤
    labels=dataSet.feature_names
    #獲取屬性數據
    features=dataSet.data
    #獲取分類信息
    classInfo=dataSet.target

    #分別獲取對應類的a和b的屬性數據
    type1_x=[]
    type1_y=[]
    type2_x=[]
    type2_y=[]
    type3_x=[]
    type3_y=[]
    fig=plt.figure()
    ax=fig.add_subplot(111)
    for i in range(len(classInfo)):
        if classInfo[i]==0:
            type1_x.append(features[i][a])
            type1_y.append(features[i][b])
        if classInfo[i]==1:
            type2_x.append(features[i][a])
            type2_y.append(features[i][b])
        if classInfo[i]==2:
            type3_x.append(features[i][a])
            type3_y.append(features[i][b])
    
   #數據繪圖
   type1=ax.scatter(type1_x,type1_y,s=30,c='g')
    type2=ax.scatter(type2_x, type2_y, s=30, c='r')
    type3=ax.scatter(type3_x, type3_y, s=30, c='b')
    #設置橫座標和縱座標
    ax.set_xlabel(labels[a])
    ax.set_ylabel(labels[b])
    ax.legend((type1,type2,type3),(u'setosa',u'versicolor',u'virginica'),loc='best')
    plt.show()
huitu(2,0)
【結果展示】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章