日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 导入数据集并画图_python matplotlib画图教程学习:(三)IRIS数据集作图

發布時間:2023/12/20 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 导入数据集并画图_python matplotlib画图教程学习:(三)IRIS数据集作图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在開始我們今天的正題之前,有一個基礎的知識先補充一下,即matplotblo所有的畫圖函數接受的數據類型是numpy.array,所以在畫圖之前最好將數據類型轉化成numpy.array,否則可能會有意外的錯誤。

例如, 將pandas.DataFrame在轉化成np.arraya = pandas.DataFrame(np.random.rand(4,5), columns = list('abcde'))a_asndarray = a.values

或者將np.matrix轉化成np.array

b = np.matrix([[1,2],[3,4]])b_asarray = np.asarray(b)

好了,今天我們將通過前兩節掌握的方法,制作IRIS數據集相關圖形,從事數據分析和挖掘工作的小伙伴們一定都聽說過這個數據集,那么現在開始吧。

首先引入IRIS數據集和基本畫圖包,并打印查看數據結構import matplotlib.pyplot as pltimport pandas as pdimport numpy as npfrom sklearn import datasets iris = datasets.load_iris() # print(type(iris)) # iris的數據類型是sklearn.utils.Bunch;A Bunch is a Python dictionary that provides attribute-style access。for key, _ in iris.items(): # 查看iris所有的key print(key)#結果如下#data#target#target_names#DESCR#feature_names#filename# 打印數據集DESCR print(DESCR)

通過打印數據集DESCR,可以了解數據集的一些重要信息如下: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.

由此知道數據集data的變量依次表示sepal length、sepal width、petal length、petal width(或者也可以由feature_names知曉),數據集target的0代表Iris-Setosa、1代表Iris-Versicolour、2代表Iris-Virginica(或者也可以由target_names知曉)。

以下是部分data和target輸出結果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) # 結果是150x4的二維矩陣(150,4),即150條數據,4列變量print(iris['target']# 是一個1維矩陣# array([0,0,...,1,2,...])

OK,基本的數據結構已經了解了,我們想通過圖形找出petal length、petal width和花的種類之間的關系圖。腦海中大概想作出下圖^_^

可以從圖中清晰看出,setosa的花瓣寬度和長度都偏小,其次是versicolor,最大的是virginca。

那么,接來下就一步步給出作圖的具體步驟

第一步,獲取petal_length和petal_width數據petal_length=iris['data'][:,2] # 獲取petal_length數據,2對應著第三列# print(petal_length)petal_width=iris['data'][:,3] # 獲取petal_width數據,3對應著第四列# print(petal_width)

第二步,創建figure和axes對象fig = plt.figure()ax = fig.add_subplot(111)

第三步,繪制散點圖的關鍵一步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是點d的形狀;注意zip函數的使用方法

第四步,添加x/y軸的變量描述說明ax.set_xlabel('petal length')ax.set_ylabel('petal width')

最后一步,繪制legend。定義了兩個空list---x,y,所以只繪制了legend,沒有生成其他額外數據。這是繪制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()

好了,今天就寫到這,更多精彩內容,歡迎繼續關注噢~^_^

本文僅代表作者個人觀點,不代表SEO研究協會網官方發聲,對觀點有疑義請先聯系作者本人進行修改,若內容非法請聯系平臺管理員,郵箱cxb5918@163.com。更多相關資訊,請到SEO研究協會網www.seoxiehui.cn學習互聯網營銷技術請到巨推學院www.jutuiedu.com。

總結

以上是生活随笔為你收集整理的python 导入数据集并画图_python matplotlib画图教程学习:(三)IRIS数据集作图的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。