日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
再見,可視化!你好,Pandas!

用Python做數(shù)據(jù)分析離不開pandas,pnadas更多的承載著處理和變換數(shù)據(jù)的角色,pands中也內(nèi)置了可視化的操作,但效果很糙。

我們提供的服務有:成都網(wǎng)站建設、網(wǎng)站建設、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、招遠ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的招遠網(wǎng)站制作公司

因此,大家在用Python做數(shù)據(jù)分析時,正常的做法是用先pandas先進行數(shù)據(jù)處理,然后再用Matplotlib、Seaborn、Plotly、Bokeh等對dataframe或者series進行可視化操作。

但是說實話,每個可視化包都有自己獨特的方法和函數(shù),經(jīng)常忘,這是讓我一直很頭疼的地方。

好消息來了!從最新的pandas版本0.25.3開始,不再需要上面的操作了,數(shù)據(jù)處理和可視化完全可以用pandas一個就全部搞定。

pandas現(xiàn)在可以使用Plotly、Bokeh作為可視化的backend,直接實現(xiàn)交互性操作,無需再單獨使用可視化包了。

下面我們一起看看如何使用。

1. 激活backend

在import了pandas之后,直接使用下面這段代碼激活backend,比如下面要激活plotly。

 
 
 
 
  1. pd.options.plotting.backend = 'plotly' 

目前,pandas的backend支持以下幾個可視化包。

  •  Plotly
  •  Holoviews
  •  Matplotlib
  •  Pandas_bokeh
  •  Hyplot

2. Plotly backend

Plotly的好處是,它基于Javascript版本的庫寫出來的,因此生成的Web可視化圖表,可以顯示為HTML文件或嵌入基于Python的Web應用程序中。

下面看下如何用plotly作為pandas的backend進行可視化。

如果還沒安裝Plotly,則需要安裝它pip intsall plotly。如果是在Jupyterlab中使用Plotly,那還需要執(zhí)行幾個額外的安裝步驟來顯示可視化效果。

首先,安裝IPywidgets。

 
 
 
 
  1. pip install jupyterlab "ipywidgets>=7.5" 

然后運行此命令以安裝Plotly擴展。

 
 
 
 
  1. jupyter labextension install jupyterlab-plotly@4.8.1 

示例選自openml.org的的數(shù)據(jù)集,鏈接如下:

數(shù)據(jù)鏈接:https://www.openml.org/d/187

這個數(shù)據(jù)也是Scikit-learn中的樣本數(shù)據(jù),所以也可以使用以下代碼將其直接導入。

 
 
 
 
  1. import pandas as pd  
  2. import numpy as np  
  3. from sklearn.datasets import fetch_openml  
  4. pd.options.plotting.backend = 'plotly'  
  5. X,y = fetch_openml("wine", version=1, as_frame=True, return_X_y=True)  
  6. data = pd.concat([X,y], axis=1)  
  7. data.head() 

該數(shù)據(jù)集是葡萄酒相關的,包含葡萄酒類型的許多功能和相應的標簽。數(shù)據(jù)集的前幾行如下所示。

下面使用Plotly backend探索一下數(shù)據(jù)集。

繪圖方式與正常使用Pandas內(nèi)置的繪圖操作幾乎相同,只是現(xiàn)在以豐富的Plotly顯示可視化效果。

下面的代碼繪制了數(shù)據(jù)集中兩個要素之間的關系。

 
 
 
 
  1. fig = data[['Alcohol', 'Proline']].plot.scatter(y='Alcohol', x='Proline')  
  2. fig.show() 

如果將鼠標懸停在圖表上,可以選擇將圖表下載為高質(zhì)量的圖像文件。

我們可以結合Pandas的groupby函數(shù)創(chuàng)建一個條形圖,總結各類之間Hue的均值差異。

 
 
 
 
  1. data[['Hue','class']].groupby(['class']).mean().plot.bar() 

將class添加到我們剛才創(chuàng)建的散點圖中。通過Plotly可以輕松地為每個類應用不同的顏色,以便直觀地看到分類。

 
 
 
 
  1. fig = data[['Hue', 'Proline', 'class']].plot.scatter(x='Hue', y='Proline', color='class', title='Proline and Hue by wine class')  
  2. fig.show() 

3. Bokeh backend

Bokeh是另一個Python可視化包,也可提供豐富的交互式可視化效果。Bokeh還具有streaming API,可以為比如金融市場等流數(shù)據(jù)創(chuàng)建實時可視化。

pandas-Bokeh的GitHub鏈接如下:

https://github.com/PatrikHlobil/Pandas-Bokeh

老樣子,用pip安裝即可,pip install pandas-bokeh。

為了在Jupyterlab中顯示Bokeh可視化效果,還需要安裝兩個新的擴展。

 
 
 
 
  1. jupyter labextension install @jupyter-widgets/jupyterlab-manager  
  2. jupyter labextension install @bokeh/jupyter_bokeh 

下面我們使用Bokeh backend重新創(chuàng)建剛剛plotly實現(xiàn)的的散點圖。

 
 
 
 
  1. pd.options.plotting.backend = 'pandas_bokeh'  
  2. import pandas_bokeh  
  3. from bokeh.io import output_notebook  
  4. from bokeh.plotting import figure, show   
  5. output_notebook()  
  6. p1 = data.plot_bokeh.scatter(x='Hue',   
  7.                               y='Proline',   
  8.                               category='class',  
  9.                               title='Proline and Hue by wine class',  
  10.                               show_figure=False)  
  11. show(p1) 

關鍵語句就一行代碼,非常快捷,交互式效果如下。

Bokeh還具有plot_grid函數(shù),可以為多個圖表創(chuàng)建類似于儀表板的布局,下面在網(wǎng)格布局中創(chuàng)建了四個圖表。

 
 
 
 
  1. output_notebook()  
  2. p1 = data.plot_bokeh.scatter(x='Hue',   
  3.                               y='Proline',   
  4.                               category='class',  
  5.                               title='Proline and Hue by wine class',  
  6.                               show_figure=False)  
  7. p2 = data[['Hue','class']].groupby(['class']).mean().plot.bar(title='Mean Hue per Class')  
  8. df_hue = pd.DataFrame({  
  9.     'class_1': data[data['class'] == '1']['Hue'],  
  10.     'class_2': data[data['class'] == '2']['Hue'],  
  11.     'class_3': data[data['class'] == '3']['Hue']},  
  12.     columns=['class_1', 'class_2', 'class_3'])   
  13. p3 = df_hue.plot_bokeh.hist(title='Distribution per Class: Hue')  
  14. df_proline = pd.DataFrame({  
  15.     'class_1': data[data['class'] == '1']['Proline'],  
  16.     'class_2': data[data['class'] == '2']['Proline'],  
  17.     'class_3': data[data['class'] == '3']['Proline']},  
  18.     columns=['class_1', 'class_2', 'class_3'])   
  19. p4 = df_proline.plot_bokeh.hist(title='Distribution per Class: Proline')  
  20. pandas_bokeh.plot_grid([[p1, p2],   
  21.                         [p3, p4]], plot_width=450) 

可以看到,可視化的部分都是在pandas的dataframe基礎上一行代碼搞定,最后plot_grid完成布局。

4. 總結

在內(nèi)置的Pandas繪圖功能增加多個第三方可視化backend,大大增強了pandas用于數(shù)據(jù)可視化的功能,今后可能真的不需再去學習眾多可視化操作了,使用pandas也可以一擊入魂!


網(wǎng)頁標題:再見,可視化!你好,Pandas!
本文來源:http://m.5511xx.com/article/dpipggd.html