[英]Adjacency matrix using igraph package
I use igraph
package in R
for Social Network Analysis. 我在
R
使用igraph
软件包进行社交网络分析。 I decide to work with Movielens Dataset (Movies Section)
, I also loaded the igraph Library
, when I wanted to work with adjacency matrix. 当我想使用邻接矩阵时,我决定使用
Movielens Dataset (Movies Section)
,还加载了igraph Library
。
The dataset loaded successfully, and these r my codes. 数据集已成功加载,并且这些代码是我的。
ff = read.csv("D:/TMU/DataSet/MovieLens/movies.csv", header = TRUE)
ff
mtr = as.matrix(ff)
gr = graph.adjacency(mtr, mode = "undirected", weighted = NULL, diag = FALSE)
I faced with this error : 我遇到了这个错误:
Error in graph.adjacency.dense(adjmatrix, mode = mode, weighted = weighted, :
graph.adjacency.dense(adjmatrix,mode = mode,weighted = weighted,:中的错误
At structure_generators.c:274 : Non-square matrix, Non-square matrixat structure_generators.c:274:非平方矩阵,非平方矩阵
In addition: Warning message:另外:警告消息:
In mde(x) : NAs introduced by coercion在mde(x)中:强制引入的NA
is there a problem with dataset or what ? 数据集有问题吗?
Okay, using the small dataset from https://grouplens.org/datasets/movielens/ which has dimension 9125x3 好的,使用https://grouplens.org/datasets/movielens/中的小型数据集,其维度为9125x3
Download the data (you may need to tweak the mode
in the download.file
if you are using windows) 下载数据(如果使用Windows,则可能需要调整
download.file
的mode
)
pth <- "http://files.grouplens.org/datasets/movielens/ml-latest-small.zip"
download.file(pth, destfile=temp<-tempfile())
#unzip(temp, list=TRUE) # see what files?
unzip(temp, exdir=td<-tempdir())
# read movies dataset
movies <- read.csv(file.path(td, "ml-latest-small/movies.csv"),
header=TRUE, stringsAsFactors = FALSE)
Load some libraries 加载一些库
library(tm) # to form the binary matrix: best to keep things sparse
library(slam) # for the crossproduct of the simple_triplet_matrix returned by tm::DocumentTermMatrix
library(igraph)
Form binary matrix for movies by genres (had to use MrFlick's suggestion of VCorpus otherwise "(no genres listed)" and "film-noir" were split into the individual words 按流派形成电影的二进制矩阵(必须使用MrFlick 提出的 VCorpus的建议,否则将“(未列出流派)”和“黑电影”拆分为单个词
# split the genres string and create binary matrix for presence of genre
corp <- VCorpus(VectorSource(movies$genres))
dtm <- DocumentTermMatrix(corp,
control = list(tokenize = function(x)
unlist(strsplit(as.character(x), "\\|"))))
Create adjacency matrix 创建邻接矩阵
# this looks for agreement across the genres
# you could use tcrossprod for similarities on the films
adj <- crossprod_simple_triplet_matrix(dtm)
Create graph 创建图
g <- graph_from_adjacency_matrix(adj, mode="undirected", weighted=TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.