簡體   English   中英

使用igraph包的鄰接矩陣

[英]Adjacency matrix using igraph package

我在R使用igraph軟件包進行社交網絡分析。 當我想使用鄰接矩陣時,我決定使用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,mode = mode,weighted = weighted,:中的錯誤
at structure_generators.c:274:非平方矩陣,非平方矩陣
另外:警告消息:
在mde(x)中:強制引入的NA

數據集有問題嗎?

好的,使用https://grouplens.org/datasets/movielens/中的小型數據集,其維度為9125x3

下載數據(如果使用Windows,則可能需要調整download.filemode

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 提出的 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM