我在igraph
中使用R
软件包进行社交网络分析。当我想使用邻接矩阵时,我决定使用Movielens Dataset (Movies Section)
,也加载了igraph Library
。
数据集已成功加载,并且这些代码是我的。
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)
我遇到此错误:
graph.adjacency.dense(adjmatrix,模式=模式,加权=加权,:
在structure_generators.c:274处:非平方矩阵,非平方矩阵
另外:警告消息:
在mde(x)中:强制引入的NA
数据集有问题吗?
答案 0 :(得分:0)
好的,使用https://grouplens.org/datasets/movielens/中的小型数据集,其维度为9125x3
下载数据(如果使用Windows,则可能需要在mode
中调整download.file
)
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)
加载一些库
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)
按流派为电影形成二进制矩阵(必须使用MrFlick的suggestion of 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), "\\|"))))
创建邻接矩阵
# this looks for agreement across the genres
# you could use tcrossprod for similarities on the films
adj <- crossprod_simple_triplet_matrix(dtm)
创建图形
g <- graph_from_adjacency_matrix(adj, mode="undirected", weighted=TRUE)