python 交互式流程圖
Python中的數據可視化 (Data Visualization in Python)
R vs Python is a constant tussle when it comes to what is the best language, according to data scientists. Though each language has it’s strengths, R, in my opinion has one cutting-edge trick that is hard to beat — R has fantastic tools to communicate results through visualization.
根據數據科學家的說法,R還是Python一直是爭論最佳語言的話題。 盡管每種語言都有自己的長處,但我認為R具有一個難以克服的尖端技巧-R具有出色的工具,可以通過可視化傳達結果。
This particular point stood out to me this week, when I was trying to find an appealing way to visualize the correlation between features in my data. I stumbled upon CHORD Diagrams!(Which we will get to, in a minute) I had seen a few R examples to generate Chord Diagrams using Circlize where you could just pass the properly shaped data to the chordDiagram() function and ta-da!
本周,當我試圖找到一種吸引人的方式來可視化數據中要素之間的相關性時,這一點對我而言尤為突出。 我偶然發現了和弦圖表!(我們將拿地,在一分鐘內),我已經看到了一些[R例子使用Circlize在那里你可以只在適當形狀的數據傳遞給chordDiagram()函數和當當生成和弦圖表!
You should have seen the look on my face when I found the Python Plotly implementation of the Chord Diagram. Even to get a basic figure, one had to put in a lot of effort. The end result simply did not seem worth the effort. I was almost dropping the idea of using a Chord Diagram, when I stumbled upon chord on pypi.
當我發現Chord Diagram的Python Plotly實現時,您應該已經看到了我的表情。 即使要獲得一個基本的數字,也必須付出很多努力。 最終結果似乎根本不值得付出努力。 當我偶然發現pypi的和弦時,我幾乎放棄了使用和弦圖的想法。
好的,和弦圖是什么? (Okay, What is a Chord Diagram?)
A Chord Diagram represents the flows between a set of distinct items. These items known as nodes are displayed all around a circle and the flows are shown as connections between the nodes, shown as arcs.
和弦圖表示一組不同項目之間的流程。 這些稱為節點的項目顯示在整個圓周圍,并且流程顯示為節點之間的連接,顯示為圓弧。
If that did not explain it clearly, let’s take a look at an example:
如果那不能清楚地解釋它,讓我們看一個例子:

The above Chord Diagram, visualizes the number of times two entities(Cities in this case) occur together in the itinerary of a traveler, it allows us to study the flow between them.
上面的弦圖,可視化了兩個實體(在這種情況下為城市)在一個旅行者的行程中一起出現的次數,它使我們能夠研究它們之間的流動。
如何以最小的努力創建漂亮的和弦圖? (How to create a beautiful Chord Diagram with minimum effort?)
Let me take you through the process of data preparation and then the creation of the Chord Diagram.
讓我引導您完成數據準備過程,然后創建和弦圖。
安裝: (Installation:)
Assuming Pandas is already installed, You need to install the chord package from pypi, using —
假設已經安裝了Pandas,則需要使用以下方法從pypi安裝和弦包:
pip install chord
數據準備: (Data Preparation:)
I am using the Boston House Prices Dataset, which can be downloaded from here.
我使用的是波士頓房屋價格數據集,可從此處下載。
# importing Pandas libary
import pandas as pd# reading data from csv
df = pd.read_csv("housing.csv")
My goal, here is to visualize the correlation between the feature in the dataset. So, for the sake of brevity, I will drop a few of the columns. I will be left with only 6 features. (You can skip this if you wish)
我的目標是可視化數據集中要素之間的相關性。 因此,為了簡潔起見,我將刪除一些專欄文章。 我將只剩下6個功能。 (如果愿意,可以跳過此步驟)
# List of columns to delete and then dropping them.
delete = ['ZN', 'INDUS', 'CHAS', 'DIS','RAD','PTRATIO','B','LSTAT']df.drop(delete, axis=1, inplace=True)
Now let’s create the correlation matrix using Pandas corr() function.
現在,讓我們使用Pandas corr()函數創建相關矩陣。
# Now, matrix contains a 6x6 matrix of the values.
matrix = df.corr()# Replacing negative values with 0’s, as features can be negatively correlated.
matrix[matrix < 0] = 0# Multiplying all values by 100 for clarity, since correlation values lie b/w 0 and 1.
matrix = matrix.multiply(100).astype(int)# Converting the DataFrame to a 2D List, as it is the required input format.
matrix = matrix.values.tolist()
This data is now perfect for our plotting!
現在,該數據非常適合我們的繪圖!
繪制圖表: (Plotting the Chart Diagram:)
The only step left before plotting, is storing the names of the entities as a list. In my case, these are the names of the features.
繪制之前剩下的唯一步驟是將實體名稱存儲為列表。 就我而言,這些是功能的名稱。
# Names of the features.
names = ["Crime Rate","N-Oxide","Number of rooms","Older buildings","Property Tax","Median Price"]
Now, all we have to do is import the package —
現在,我們要做的就是導入包-
from chord import Chord
Then pass the matrix and the names to the Chord() function.
然后將矩陣和名稱傳遞給Chord()函數。
Chord(matrix, names).show()#Note: The show() function works only with Jupyter Labs.
# (Not Jupyter notebook)
This will be your output:
這將是您的輸出:

Before we go further and explore the other style and output settings available in the Chord library, let’s take a look at what the output represents.
在進一步探討Chord庫中可用的其他樣式和輸出設置之前,讓我們看一下輸出所代表的含義。
As you can see, when you hover on the Crime rate, you can see that it is connected to Property Tax, Older Buildings and level of N-Oxide, but has no connections with the Median Price or the Number of Rooms. You can now hover on the connection and you will see the correlation value between these features.
如您所見,當您將鼠標懸停在犯罪率上時,您會看到它與物業稅,舊建筑物和N-氧化物水平相關,但與中位數價格或房間數沒有關系。 您現在可以將鼠標懸停在連接上,您將看到這些功能之間的相關性值。
You might notice that the Median Price is 100% correlated with itself, which is the case with all the features. That happens because we get a perfect correlation value when we compare a feature against itself. We can fix this with a single line of code, if you wish.
您可能會注意到,中位數價格與其自身100%相關,所有功能都是這種情況。 發生這種情況的原因是,當我們將特征與自身進行比較時,我們獲得了完美的相關值。 如果您愿意,我們可以用一行代碼來解決。
# Operate on the data before converting it into a 2D List# We are just converting all Perfect correlation 100's(Basically the 1’s) to 0 as well.
matrix[matrix == 100] = 0
matrix = matrix.values.tolist()
Here is your output, a much cleaner Chord Diagram:
這是您的輸出,更清晰的和弦圖:

將和弦圖導出為HTML: (Export the Chord Diagram as HTML:)
Since the package uses d3-chord at it’s core, it also gives us the option to output the ChordDiagram as a completely editable HTML file! How cool is that?
由于該軟件包的核心使用d3-chord,因此它還為我們提供了將ChordDiagram輸出為完全可編輯HTML文件的選項! 多么酷啊?
Again, a single method call will do it for you —
同樣,一個方法調用將為您完成—
Chord(matrix, names).to_html()# This will create a file 'out.html' in your current directory.
You can open the HTML in a browser to find the same interactive Chord Diagram or you can open the .html in a code editor and customize the rest of your page!
您可以在瀏覽器中打開HTML以找到相同的交互式和弦圖,也可以在代碼編輯器中打開.html并自定義頁面的其余部分!
Here’s my output,
這是我的輸出,

What I have done is extremely basic. The point is, output as a HTML opens up a myriad of possibilities to use the Chord Diagram.
我所做的工作非常基礎。 關鍵是,以HTML格式輸出將打開使用和弦圖的多種可能性。
樣式和自定義: (Styling and Customization:)
顏色: (Colors:)
You can change the colors of the Chord Diagram by passing any colors from the d3 categorical palette. You can find samples of the outputs on the Official Guide. But here are a couple of examples:
您可以通過傳遞d3分類調色板中的任何顏色來更改和弦圖的顏色。 您可以在《 官方指南》中找到輸出示例。 但是這里有幾個例子:
# Just add the colors parameter and pass the value.
Chord(matrix, names, colors="d3.schemeDark2").show()

# Just add the colors parameter and pass the value.
Chord(matrix, names, colors="d3.schemeAccent").show()

# Add all the colors to a list.
coloursList = ["#f50057", "#2196f3", "#00e676", "#ff5722", "#00000", "#ff9100"]# Pass the list to the colors parameter.
Chord(matrix, names, colors=coloursList).show()

Other customization's:
其他定制:
You can customize the labels and the opacity as well, checkout the official guide for that.
您也可以自定義標簽和不透明度,請查看官方指南。
結論: (Conclusions:)
Creating visualizations is almost always a part of a Data Scientist’s work. Part is the keyword here, because it means you cannot spend a lot of time to get them in shape and that is why we look for options that provide a simple yet functional implementation. That’s what I try to explore in this article, by creating an effective Chord Diagram with minimal effort.
創建可視化幾乎總是一個數據科學家的工作的一部分 。 part是這里的關鍵字,因為它意味著您不能花費大量時間來使它們成形,因此這就是我們尋找提供簡單但功能性實現的選項的原因。 這就是我通過最小的努力創建有效的Chord Diagram來嘗試的方法。
This is my first work of technical writing and I have attempted to embed all the best practice that I have come across in my years of reading excellent content from this community. I’d appreciate feedback about any aspects of my work.
這是我的第一篇技術寫作著作,我試圖將多年來閱讀該社區優秀內容所遇到的所有最佳實踐嵌入其中。 感謝您對我的工作的各個方面的反饋。
其他資源: (Additional resources:)
[1] Official Guide — Shahin Rostami’s blog(Author of the library)
[1] 官方指南 -Shahin Rostami的博客(圖書館作者)
[2] chord on PyPi — You can download the package here.
[2] PyPi上的和弦 —您可以在此處下載軟件包。
翻譯自: https://towardsdatascience.com/create-beautiful-and-interactive-chord-diagrams-using-python-cb5ecb092a7c
python 交互式流程圖
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/389167.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/389167.shtml 英文地址,請注明出處:http://en.pswp.cn/news/389167.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!