本文介绍了在R中不将平方加权邻接矩阵转换为igraph对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tf_idf值来确定网页之间的相似性.到现在我有了tf_idf矩阵,它不是正方形的,因为有很多关键字,但是只有36个文档.相同的模式投影.

I am using tf_idf value to determine similarity between webpages.Till now I have my tf_idf matrix which is not square as there are many keywords but only 36 document .I want to convert this matrix to graph object so that i can take one mode projection for the same.

所以我正在用这个 ig <- graph.adjacency(tf_idf,mode="undirected",weighted=TRUE).我希望对此加权,即tf_idf值.

So i am using this ig <- graph.adjacency(tf_idf,mode="undirected",weighted=TRUE).I want this to be weighted which is it's tf_idf value.

但是,当我这样做时,它会引发错误,

But,when i do this it throw an error,

您能帮我决定如何进行操作吗?

Can you please help me in to decide how to proceed then.

我有类似这样的矩阵,其中x,y,z是关键字,A,b是网页

I have matrix something like this where x,y,z is keyword and A,b is webpage

mat = matrix(c(0.1, 0.5, 0.9, 0.4, 0.3, 0.5), nc=3,
           dimnames=list(c("A", "B"), c("x", "y", "z")),
           byrow=TRUE)

      x    y   z
A     0.1 0.5 0.9
B     0.4 0.3 0.5

推荐答案

也许有更好的方法,但是如果要将矩阵扩展为成熟的邻接矩阵,可以使用以下函数:

There might be a better way, but if you want to expand your matrix to a full-fledged adjacency matrix, you can use the following function:

expand.matrix <- function(A){
  m <- nrow(A)
  n <- ncol(A)
  B <- matrix(0,nrow = m, ncol = m)
  C <- matrix(0,nrow = n, ncol = n)
  cbind(rbind(B,t(A)),rbind(A,C))
}

例如:

 A <- rbind(c(0,1,0),c(1,0,1))
> A
     [,1] [,2] [,3]
[1,]    0    1    0
[2,]    1    0    1
> expand.matrix(A)
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    0    1    0
[2,]    0    0    1    0    1
[3,]    0    1    0    0    0
[4,]    1    0    0    0    0
[5,]    0    1    0    0    0

这篇关于在R中不将平方加权邻接矩阵转换为igraph对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:43