如何使用Graph :: Easy将频率分数的权重分配给图形的边缘(How can I assign a weight for frequency score to a graph's ed

编程入门 行业动态 更新时间:2024-10-05 11:27:09
如何使用Graph :: Easy将频率分数的权重分配给图形的边缘(How can I assign a weight for frequency score to a graph's edge using Graph::Easy)

如何使用Graph :: Easy为图形边缘指定权重或频率分数?

我有一个双字母组合及其频率列表。 我可以使用Graph :: Easy轻松创建仅包含bigrams的(数学)图形。 输出正是我想要的。 但是当我尝试“set_attribute”时,我得到一个错误,上面写着“'频率'不是一个有效的属性名称”。 我究竟做错了什么? 使用Graph :: Easy,如何使频率成为有效属性?

#!/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

How do I assign a weight or frequency score to an graph's edge using Graph::Easy?

I have a list of bigrams and their frequencies. I can easily create a (mathematical) graph of only the bigrams using Graph::Easy. The output is exactly what I desire. But when I try to "set_attribute" I get an error that says, "'frequency' is not a valid attribute name". What am I doing wrong? Using Graph::Easy, how do I make frequency a valid attribute?

#!/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

最满意答案

我用这个模块玩了一下,它似乎不接受任意“属性”,只接受一组预定义的属性。 显然'频率'不是它们。

我从文档中选择了一个样本并替换了你的

$edge->set_attribute( 'frequency', $frequency );

$edge->set_attribute( 'label', $frequency );

因为他们经常在例子中提到label 。

print $graph->as_ascii();

然后打印:

+--------------+ 2 +--------------+ | 14 | ---> | million | +--------------+ +--------------+ +--------------+ 2 +--------------+ 2 +----------+ | digital | ---> | humanities | ---> | research | +--------------+ +--------------+ +----------+ +--------------+ 2 +--------------+ 3 +----------+ | participants | ---> | may | ---> | want | +--------------+ +--------------+ +----------+ ...

这就是你追求的吗?


最终我找到了Graph :: Easy的完整文档 。 “ 属性”部分列出了允许的属性。 我很确定有一种方法可以使用自定义属性,因为模块有一个方法get_custom_attributes 。

The "best" answer for me is to not use Graph::Easy but instead use a Python module, NetworkX:

#!/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()

更多推荐

本文发布于:2023-07-09 10:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:权重   如何使用   分数   频率   图形

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!