自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

講道理,Python數(shù)據(jù)可視化是優(yōu)雅的藝術(shù)

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

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

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

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

 

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

1. 畫散點圖

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

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

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

 

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

畫出散點圖如下:

講道理,Python數(shù)據(jù)可視化是優(yōu)雅的藝術(shù)

2. 多項式擬合并畫出擬合曲線

## 多項式擬合

 

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

效果圖:

講道理,Python數(shù)據(jù)可視化是優(yōu)雅的藝術(shù)

3. 畫多個子圖

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

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

  • sepal length (cm)——花萼長度
  • sepal width (cm)——花萼寬度
  • petal length (cm)——花瓣長度
  • petal width (cm)——花瓣寬度

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

 

  1. # -*- coding=utf-8 -*-  
  2. from matplotlib import pyplot as plt  
  3. from sklearn.datasets import load_iris  
  4. import numpy as np  
  5. import itertools 
  6.  data = load_iris()  
  7. #print(data.data)  
  8. #print(data.feature_names)  
  9. #print(data.target)  
  10. features = data['data' 
  11. feature_names = data['feature_names' 
  12. target = data['target' 
  13. labels = data['target_names'][data['target']]  
  14. print(data.data)  
  15. print(data.feature_names)123456789101112131415161718123456789101112131415161718 

這里有一個排列組合參考代碼,***是取出了兩兩組合的情況。

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

 

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

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

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

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

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

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

 

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

這里的可視化效果如下:

講道理,Python數(shù)據(jù)可視化是優(yōu)雅的藝術(shù)

4. 畫水平線和垂直線

比如在上面***一幅圖中,找到了一種方法可以把三種鳶尾花分出來,這是我們需要畫出模型(一條直線)。這個時候怎么畫呢?

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

 

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

此時可視化效果如下:

講道理,Python數(shù)據(jù)可視化是優(yōu)雅的藝術(shù)

5. 動態(tài)畫圖

plt.ion()打開交互模式。plt.show()不再阻塞程序運行。

注意plt.axis()的用法。

 

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

可視化效果:

講道理,Python數(shù)據(jù)可視化是優(yōu)雅的藝術(shù)

責(zé)任編輯:未麗燕 來源: 36大數(shù)據(jù)
相關(guān)推薦

2017-06-29 11:26:08

Python數(shù)據(jù)可視化

2017-07-12 16:07:49

大數(shù)據(jù)數(shù)據(jù)可視化

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2019-01-21 15:10:11

佩奇可視化數(shù)據(jù)

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2017-10-31 09:38:53

大數(shù)據(jù)數(shù)據(jù)可視化Python

2022-08-26 09:15:58

Python可視化plotly

2012-05-08 16:13:36

iPhone

2015-09-09 13:15:05

2022-02-23 09:50:52

PythonEchartspyecharts

2020-05-26 11:34:46

可視化WordCloud

2020-09-02 13:56:03

Python可視化數(shù)據(jù)

2018-11-30 10:28:44

Python反爬網(wǎng)頁

2017-02-23 09:42:53

大數(shù)據(jù)數(shù)據(jù)可視化技術(shù)誤區(qū)

2024-08-20 18:16:49

數(shù)據(jù)可視化Python

2023-03-09 15:11:30

數(shù)據(jù)可視化工具matplotlib

2021-08-30 11:40:06

PythonSeaborn可視化

2022-07-05 15:11:42

Python數(shù)據(jù)可視化機(jī)器學(xué)習(xí)

2015-08-20 10:00:45

可視化

2018-03-07 11:35:49

Python可視化數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號