python 导入数据集并画图_python matplotlib画图教程学习:(三)IRIS数据集作图
在開始我們今天的正題之前,有一個基礎(chǔ)的知識先補(bǔ)充一下,即matplotblo所有的畫圖函數(shù)接受的數(shù)據(jù)類型是numpy.array,所以在畫圖之前最好將數(shù)據(jù)類型轉(zhuǎn)化成numpy.array,否則可能會有意外的錯誤。
例如, 將pandas.DataFrame在轉(zhuǎn)化成np.arraya = pandas.DataFrame(np.random.rand(4,5), columns = list('abcde'))a_asndarray = a.values
或者將np.matrix轉(zhuǎn)化成np.array
b = np.matrix([[1,2],[3,4]])b_asarray = np.asarray(b)
好了,今天我們將通過前兩節(jié)掌握的方法,制作IRIS數(shù)據(jù)集相關(guān)圖形,從事數(shù)據(jù)分析和挖掘工作的小伙伴們一定都聽說過這個數(shù)據(jù)集,那么現(xiàn)在開始吧。
首先引入IRIS數(shù)據(jù)集和基本畫圖包,并打印查看數(shù)據(jù)結(jié)構(gòu)import matplotlib.pyplot as pltimport pandas as pdimport numpy as npfrom sklearn import datasets iris = datasets.load_iris() # print(type(iris)) # iris的數(shù)據(jù)類型是sklearn.utils.Bunch;A Bunch is a Python dictionary that provides attribute-style access。for key, _ in iris.items(): # 查看iris所有的key print(key)#結(jié)果如下#data#target#target_names#DESCR#feature_names#filename# 打印數(shù)據(jù)集DESCR print(DESCR)
通過打印數(shù)據(jù)集DESCR,可以了解數(shù)據(jù)集的一些重要信息如下:Iris plants dataset--------------------**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.
由此知道數(shù)據(jù)集data的變量依次表示sepal length、sepal width、petal length、petal width(或者也可以由feature_names知曉),數(shù)據(jù)集target的0代表Iris-Setosa、1代表Iris-Versicolour、2代表Iris-Virginica(或者也可以由target_names知曉)。
以下是部分data和target輸出結(jié)果print(iris['data']) # 是一個多維矩陣# array([[5.1, 3.5, 1.4, 0.2],[4.9, 3. , 1.4, 0.2],[4.7, 3.2, 1.3, 0.2]...])print(iris['data'].shape) # 結(jié)果是150x4的二維矩陣(150,4),即150條數(shù)據(jù),4列變量print(iris['target']# 是一個1維矩陣# array([0,0,...,1,2,...])
OK,基本的數(shù)據(jù)結(jié)構(gòu)已經(jīng)了解了,我們想通過圖形找出petal length、petal width和花的種類之間的關(guān)系圖。腦海中大概想作出下圖^_^
可以從圖中清晰看出,setosa的花瓣寬度和長度都偏小,其次是versicolor,最大的是virginca。
那么,接來下就一步步給出作圖的具體步驟
第一步,獲取petal_length和petal_width數(shù)據(jù)petal_length=iris['data'][:,2] # 獲取petal_length數(shù)據(jù),2對應(yīng)著第三列# print(petal_length)petal_width=iris['data'][:,3] # 獲取petal_width數(shù)據(jù),3對應(yīng)著第四列# print(petal_width)
第二步,創(chuàng)建figure和axes對象fig = plt.figure()ax = fig.add_subplot(111)
第三步,繪制散點(diǎn)圖的關(guān)鍵一步markers=[ 's' if i == 0 else 'o' if i==1 else 'd' for i in iris.target]colors=['pink' if i==0 else 'skyblue' if i==1 else 'lightgreen' for i in iris.target] for x, y, c, m in zip(petal_length, petal_width, colors, markers): ax.scatter(x, y, c=c, marker=m) # c=color;marker是點(diǎn)d的形狀;注意zip函數(shù)的使用方法
第四步,添加x/y軸的變量描述說明ax.set_xlabel('petal length')ax.set_ylabel('petal width')
最后一步,繪制legend。定義了兩個空list---x,y,所以只繪制了legend,沒有生成其他額外數(shù)據(jù)。這是繪制legend的一般方法,多試試改改就能更好理解了。# 制作legendx=[]y=[]ax.scatter(x,y,marker='s',label='setosa',color='pink') # 繪制setosa的legendax.scatter(x,y,marker='o',label='versicolor',color='skyblue') # 繪制versicolor的legendax.scatter(x,y,marker='d',label='virginica',color='lightgreen') # 繪制virginica的legendplt.legend()plt.show()
好了,今天就寫到這,更多精彩內(nèi)容,歡迎繼續(xù)關(guān)注噢~^_^
本文僅代表作者個人觀點(diǎn),不代表SEO研究協(xié)會網(wǎng)官方發(fā)聲,對觀點(diǎn)有疑義請先聯(lián)系作者本人進(jìn)行修改,若內(nèi)容非法請聯(lián)系平臺管理員,郵箱cxb5918@163.com。更多相關(guān)資訊,請到SEO研究協(xié)會網(wǎng)www.seoxiehui.cn學(xué)習(xí)互聯(lián)網(wǎng)營銷技術(shù)請到巨推學(xué)院www.jutuiedu.com。
總結(jié)
以上是生活随笔為你收集整理的python 导入数据集并画图_python matplotlib画图教程学习:(三)IRIS数据集作图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android串口通信apk源码
- 下一篇: 你知道kernel version的实现