Python 如何将频率分数的权重分配给图表';s边使用图形::简单
如何使用graph::Easy将权重或频率分数分配给图表的边缘 我有一个双曲线图及其频率的列表。我可以使用graph::Easy轻松地创建一个仅包含bigram的(数学)图。输出正是我想要的。但是当我尝试“设置_属性”时,我得到一个错误,它说,“'frequency'不是一个有效的属性名”。我做错了什么?使用Graph::Easy,如何使频率成为有效属性Python 如何将频率分数的权重分配给图表';s边使用图形::简单,python,perl,graph,Python,Perl,Graph,如何使用graph::Easy将权重或频率分数分配给图表的边缘 我有一个双曲线图及其频率的列表。我可以使用graph::Easy轻松地创建一个仅包含bigram的(数学)图。输出正是我想要的。但是当我尝试“设置_属性”时,我得到一个错误,它说,“'frequency'不是一个有效的属性名”。我做错了什么?使用Graph::Easy,如何使频率成为有效属性 #!/usr/bin/perl # graph.pl - given a list of bigrams and their frequen
#!/usr/bin/perl
# graph.pl - given a list of bigrams and their frequencies, output graphml
# require
use Graph::Easy;
use strict;
# initialize
my $graph = Graph::Easy->new;
# process the data
while ( <DATA> ) {
# parse
chop;
my ( $nodes, $frequency ) = split( "\t", $_ );
my ( $source, $target ) = split( ' ', $nodes );
# update the graph
my $edge = $graph->add_edge( $source, $target );
# error happen here
$edge->set_attribute( 'frequency', $frequency );
}
# output & done
print $graph->as_graphml();
exit;
# a set of bigrams and their frequencies
__DATA__
cds classroom 4
maximum registration 4
may want 3
anomalies within 2
resulting analysis 2
participants may 2
corpus without 2
journal articles 2
quickly learn 2
active reading 2
text mining 2
literally count 2
find patterns 2
14 million 2
digital humanities 2
humanities research 2
#/usr/bin/perl
#graph.pl-给定一个bigram及其频率列表,输出graphml
#要求
使用图形::简单;
严格使用;
#初始化
my$graph=graph::Easy->new;
#处理数据
而(){
#解析
印章;
我的($nodes,$frequency)=拆分(“\t”,$”;
我的($source,$target)=分割(“”,$nodes);
#更新图表
my$edge=$graph->add_edge($source,$target);
#这里发生了错误
$edge->set_属性('frequency',$frequency);
}
#输出&完成
打印$graph->as_graphml();
出口
#一组二元图及其频率
__资料__
CD教室4
最高注册人数4
我想要3个
2小时内的异常情况
结果分析2
与会者5月2日
不含2的语料库
期刊文章2
快速学习2
主动阅读2
文本挖掘2
数到2
查找模式2
1400万2
数字人文2
人文研究2
我使用了这个模块,它似乎不接受任意的“属性”,而只接受一组预定义的属性。
显然,“频率”不是其中之一
我从样品中挑选了一个样品,并替换了你的样品
$edge->set_attribute( 'frequency', $frequency );
与
正如他们在示例中经常提到的标签
print $graph->as_ascii();
然后打印:
+--------------+ 2 +--------------+
| 14 | ---> | million |
+--------------+ +--------------+
+--------------+ 2 +--------------+ 2 +----------+
| digital | ---> | humanities | ---> | research |
+--------------+ +--------------+ +----------+
+--------------+ 2 +--------------+ 3 +----------+
| participants | ---> | may | ---> | want |
+--------------+ +--------------+ +----------+
...
这就是你想要的吗
最终,我发现了for Graph::Easy。列表中列出了允许的属性。我非常确定有一种方法可以拥有自定义属性,因为模块有一个方法 对我来说,“最佳”答案是不要使用Graph::Easy,而是使用Python模块:
唉,在与开发人员交换了一点电子邮件之后,我认为不可能使用Graph::Easy向边添加任意属性。根据我的经验,Graph::Easy更多地是关于输出图形,而不是生成graphml/XML。Python模块----可能更适合我的要求。@ericleasemorgan啊,好的。这看起来很合理。如前所述,我只是玩了一下,不知道您想要创建XML。属性在这里是有意义的。不要介意。感谢您为我展示了另一个有趣的模块。:-)
+--------------+ 2 +--------------+
| 14 | ---> | million |
+--------------+ +--------------+
+--------------+ 2 +--------------+ 2 +----------+
| digital | ---> | humanities | ---> | research |
+--------------+ +--------------+ +----------+
+--------------+ 2 +--------------+ 3 +----------+
| participants | ---> | may | ---> | want |
+--------------+ +--------------+ +----------+
...
#!/usr/bin/python
# graph.py - given a CSV file of a specific shape, output graphml
# configure
DATA = './data.csv'
GRAPH = './data.graphml'
LABEL = 'frequency'
# require
import networkx as nx
import csv
# initialize
g = nx.DiGraph()
# read the data
with open( DATA, 'r' ) as f :
# initialize
r = csv.reader( f, delimiter='\t')
# process each record
for source, target, frequency in r :
# sanity check
if len( source ) == 0 or len( target ) == 0 : continue
# update the graph
g.add_edge( source, target )
g[ source ][ target ][ LABEL ] = frequency
# output & done
nx.write_graphml( g, GRAPH )
exit()