使用SAC的共现矩阵? [英] Co-occurrence matrix using SAC?
问题描述
我有以下数据框"x"
I have the following data frame 'x'
id,item,volume
a,c1,2
a,c2,3
a,c3,2
a,c4,1
a,c5,4
b,c6,6
b,c1,2
b,c3,1
b,c2,6
b,c4,4
c,c2,5
c,c8,6
c,c9,2
d,c1,1
e,c3,7
e,c2,3
e,c1,2
e,c9,5
e,c4,1
f,c1,7
f,c3,1
第一列是客户的ID,第二列是客户购买的商品的ID,第三列是购买的商品的数量.我正在尝试创建一个共现矩阵,它是一个具有8行和8列的正方形矩阵,其中8是不同项目的数量.
The first column is the id of a customer, the second column is the id of an item that customer bought and the third column is the number of those items bought. I'm trying to create a co-occurrence matrix which is a square matrix with 8 rows and columns, 8 being the number of distinct items.
n = length(unique(x$cid))
这可以通过SAC范例来完成吗?对于每个id,我需要通过为每个组合添加+1来更新上述矩阵.例如,对于具有项目c1,c2,c3,c4,c6的用户"b",矩阵中第2、3、4和第2列的第一行;对于所有用户,应将6递增1,依此类推.我无法将其投放到此框架中.任何帮助表示赞赏.
Could this be done through a SAC paradigm? For every id, I need to update the above matrix by adding +1 for every combination. For example for user 'b' with items c1,c2,c3,c4,c6, the 1st row in the matrix for columns 2,3,4 & 6 should be incremented by 1 and so on for all users. I'm unable to cast it into this framework. Any help much appreciated.
推荐答案
正如@joran提到的那样,不清楚您真正想要的是什么.但是,如果我理解正确(可能无法正确理解),我想提出一条替代路线.我想您想将商品矩阵转化为商品,并用数字表示商品共享客户的次数?
As @joran mentioned its kind of unclear what you really want. But If I understand correctly(probably not) I would like to propose an alternate route. I think you want a matrix of items to items with numbers representing the number of times the items share a customer?
如果是这样,
library(igraph)
library(tnet)
id_item<-cbind(
i=c(1,1,2,2,2,2,2,3,4,5,5,5,6,6,7,8), #items
p=c(1,2,1,2,3,4,5,2,3,4,5,6,6,7,8,8)) #customers
item_item<-projecting_tm(id_item, method="sum")
item_item <- tnet_igraph(item_item,type="weighted one-mode tnet")
itemmat<-get.adjacency(item_item,attr="weight")
itemmat #8x8 martrix of items to items
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,] 0 2 1 0 0 0 0 0
#[2,] 2 0 1 1 2 0 0 0
#[3,] 1 1 0 0 0 0 0 0
#[4,] 0 1 0 0 0 0 0 0
#[5,] 0 2 0 0 0 1 0 0
#[6,] 0 0 0 0 1 0 0 0
#[7,] 0 0 0 0 0 0 0 1
#[8,] 0 0 0 0 0 0 1 0
如果您还需要考虑购买商品(数量)的次数,那么可以轻松地将其添加到此代码中.
If you need to take number of times the item was bought (volume) into account too then this is easily added into this code.
这篇关于使用SAC的共现矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!