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.mzph.cn/news/389167.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!