久草视频2-久草视-久草社区视频-久草色在线-久草色视频-久草软件

Python優(yōu)雅地可視化數(shù)據(jù)

我是創(chuàng)始人李巖:很抱歉!給自己產(chǎn)品做個(gè)廣告,點(diǎn)擊進(jìn)來(lái)看看。  

Python優(yōu)雅地可視化數(shù)據(jù)

作者:冰不語(yǔ)

最近看《機(jī)器學(xué)習(xí)系統(tǒng)設(shè)計(jì)》…前兩章。學(xué)到了一些用Matplotlib進(jìn)行數(shù)據(jù)可視化的方法。在這里整理一下。

聲明:由于本文的代碼大部分是參考書(shū)中的例子,所以不提供完整代碼,只提供示例片段,也就是只能看出某一部分用法,感興趣的需要在自己的數(shù)據(jù)上學(xué)習(xí)測(cè)試。

最開(kāi)始,當(dāng)然還是要導(dǎo)入我們需要的包:

				# -*- coding=utf-8 -*-
				from matplotlib import pyplot as plt
				from sklearn.datasets import load_iris
				import numpy as np
				import itertools1234512345
			

1. 畫(huà)散點(diǎn)圖

畫(huà)散點(diǎn)圖用plt.scatter(x,y)。畫(huà)連續(xù)曲線(xiàn)在下一個(gè)例子中可以看到,用到了plt.plot(x,y)。

plt.xticks(loc,label)可以自定義x軸刻度的顯示,第一個(gè)參數(shù)表示的是第二個(gè)參數(shù)label顯示的位置loc。

plt.autoscale(tight=True)可以自動(dòng)調(diào)整圖像顯示的最佳化比例 。

				plt.scatter(x,y)
				plt.title("Web traffic")
				plt.xlabel("Time")
				plt.ylabel("Hits/hour")
				plt.xticks([w*7*24 for w in range(10)],['week %i' %w for w in range(10)])
				plt.autoscale(tight=True)
				plt.grid()
				##plt.show()1234567812345678
			

畫(huà)出散點(diǎn)圖如下:

Python優(yōu)雅地可視化數(shù)據(jù)
2. 多項(xiàng)式擬合并畫(huà)出擬合曲線(xiàn)

## 多項(xiàng)式擬合

				fp2 = np.polyfit(x,y,3)
				f2 = np.poly1d(fp2)
				fx = np.linspace(0,x[-1],1000)
				plt.plot(fx,f2(fx),linewidth=4,color='g')
				## f2.order: 函數(shù)的階數(shù)
				plt.legend(["d=%i" % f2.order],loc="upper right")
				plt.show()123456789123456789
			

效果圖:

Python優(yōu)雅地可視化數(shù)據(jù)
3. 畫(huà)多個(gè)子圖

這里用到的是sklearn的iris_dataset(鳶尾花數(shù)據(jù)集)。

此數(shù)據(jù)集包含四列,分別是鳶尾花的四個(gè)特征:

sepal length (cm)——花萼長(zhǎng)度

sepal width (cm)——花萼寬度

petal length (cm)——花瓣長(zhǎng)度

petal width (cm)——花瓣寬度

這里首先對(duì)數(shù)據(jù)進(jìn)行一定的處理,主要就是對(duì)特征名稱(chēng)進(jìn)行兩兩排列組合,然后任兩個(gè)特征一個(gè)一個(gè)做x軸另一個(gè)做y軸進(jìn)行畫(huà)圖。

				# -*- coding=utf-8 -*-
				from matplotlib import pyplot as plt
				from sklearn.datasets import load_iris
				import numpy as np
				import itertools
				data = load_iris()
				#print(data.data)
				#print(data.feature_names)
				#print(data.target)
				features = data['data']
				feature_names = data['feature_names']
				target = data['target']
				labels = data['target_names'][data['target']]
				print(data.data)
				print(data.feature_names)123456789101112131415161718123456789101112131415161718
			

這里有一個(gè)排列組合參考代碼,最后是取出了兩兩組合的情況。

排列組合的結(jié)果是feature_names_2包含了排列組合的所有情況,它的每一個(gè)元素包含了一個(gè)排列組合的所有情況,比如第一個(gè)元素包含了所有單個(gè)元素排列組合的情況,第二個(gè)元素包含了所有的兩兩組合的情況……所以這里取出了第二個(gè)元素,也就是所有的兩兩組合的情況

				feature_names_2 = []
				#排列組合
				for i in range(1,len(feature_names)+1):
				iter = itertools.combinations(feature_names,i)
				feature_names_2.append(list(iter))
				print(len(feature_names_2[1]))
				for i in feature_names_2[1]:
				print(i)123456789123456789
			

下面是在for循環(huán)里畫(huà)多個(gè)子圖的方法。對(duì)我來(lái)說(shuō),這里需要學(xué)習(xí)的有不少。比如

for i,k in enumerate(feature_names_2[1]):這一句老是記不住。

比如從列表中取出某元素所在的索引的方法:index1 = feature_names.index(k[0]),也即index = list.index(element)的形式。

比如for循環(huán)中畫(huà)子圖的方法:plt.subplot(2,3,1+i)

比如for循環(huán)的下面這用法:for t,marker,c in zip(range(3),”>ox”,”rgb”):

				plt.figure(1)
				for i,k in enumerate(feature_names_2[1]):
				index1 = feature_names.index(k[0])
				index2 = feature_names.index(k[1])
				plt.subplot(2,3,1+i)
				for t,marker,c in zip(range(3),">ox","rgb"):
				plt.scatter(features[target==t,index1],features[target==t,index2],marker=marker,c=c)
				plt.xlabel(k[0])
				plt.ylabel(k[1])
				plt.xticks([])
				plt.yticks([])
				plt.autoscale()
				plt.tight_layout()
				plt.show()12345678910111213141234567891011121314
			

這里的可視化效果如下:

Python優(yōu)雅地可視化數(shù)據(jù)
4. 畫(huà)水平線(xiàn)和垂直線(xiàn)

比如在上面最后一幅圖中,找到了一種方法可以把三種鳶尾花分出來(lái),這是我們需要畫(huà)出模型(一條直線(xiàn))。這個(gè)時(shí)候怎么畫(huà)呢?

下面需要注意的就是plt.vlines(x,y_min,y_max)和plt.hlines(y,x_min,x_max)的用法。

				plt.figure(2)
				for t,marker,c in zip(range(3),">ox","rgb"):
				plt.scatter(features[target==t,3],features[target==t,2],marker=marker,c=c)
				plt.xlabel(feature_names[3])
				plt.ylabel(feature_names[2])
				# plt.xticks([])
				# plt.yticks([])
				plt.autoscale()
				plt.vlines(1.6, 0, 8, colors = "c",linewidth=4,linestyles = "dashed")
				plt.hlines(2.5, 0, 2.5, colors = "y",linewidth=4,linestyles = "dashed")
				plt.show() 12345678910111234567891011
			

此時(shí)可視化效果如下:

Python優(yōu)雅地可視化數(shù)據(jù)
5. 動(dòng)態(tài)畫(huà)圖

plt.ion()打開(kāi)交互模式。plt.show()不再阻塞程序運(yùn)行。

注意plt.axis()的用法。

				plt.axis([0, 100, 0, 1])
				plt.ion()
				for i in range(100):
				y = np.random.random()
				plt.autoscale()
				plt.scatter(i, y)
				plt.pause(0.01)1234567812345678
			

可視化效果:

Python優(yōu)雅地可視化數(shù)據(jù)

End.

轉(zhuǎn)載請(qǐng)注明來(lái)自36大數(shù)據(jù)(36dsj.com): 36大數(shù)據(jù) ? Python優(yōu)雅地可視化數(shù)據(jù)

本文被轉(zhuǎn)載1次

首發(fā)媒體 36大數(shù)據(jù) | 轉(zhuǎn)發(fā)媒體

隨意打賞

python 數(shù)據(jù)挖掘python 數(shù)據(jù)分析python 可視化大數(shù)據(jù)可視化工具數(shù)據(jù)可視化軟件數(shù)據(jù)可視化分析地圖數(shù)據(jù)可視化數(shù)據(jù)可視化案例數(shù)據(jù)可視化工具大數(shù)據(jù)可視化
提交建議
微信掃一掃,分享給好友吧。
主站蜘蛛池模板: 午夜视频一区 | 6个老师的尿奴 | 视频一区二区在线 | 男女性潮高片无遮挡禁18 | 手机在线免费观看视频 | 福利社在线免费观看 | 色就色欧美综合偷拍区a | 26uuu成人人网图片 | 污污的动态图合集 | 日本精品一区二区在线播放 | 精品综合久久久久久88小说 | www.精品视频| 四虎影院在线免费观看 | 国内自拍第1页 | 二次元美女脱裤子让男人桶爽 | 日本无吗免费一二区 | 欧美日韩国产一区二区三区伦 | 91伊人网| 四虎国产精品视频免费看 | 国产麻豆精品视频 | 无码AV精品一区二区三区 | 91久久偷偷做嫩草影院免费看 | 亚洲九九精品 | 色中色破解版 | 久久国产精品无码视欧美 | 喜欢老头吃我奶躁我的动图 | 暖暖 免费 高清 日本 在线 | 国产成人精品视频一区二区不卡 | 三体动漫在线观看免费完整版2022 | coolgay男男gayxxx chinese壮直男gay老年人 chinese野外gay军人 | 国产成人精品系列在线观看 | 欧美一级片在线免费观看 | 521色香蕉网站在线观看 | 亚洲国产精品高清在线 | 亚洲成人第一页 | 午夜久久免影院欧洲 | 91日本 | 欧美一卡2卡三卡4卡5卡免费观看 | 黄色大片免费网站 | 久久99精品涩AV毛片观看 | 久久99热狠狠色AV蜜臀 |