我需要计算数据框中每行之间的杰卡德距离。返回需要是表示距离的矩阵/数据框。
像这样:
1 2 3 ..
1 0 0.2 1
2 0.2 0 0.4
3 1 0.4 0
.
.
Run Code Online (Sandbox Code Playgroud)
我的数据:
dput(项目[1:10,])
structure(list(Drama = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L), Comedy = c(0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), Crime = c(0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), SciFi = c(1L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), Kids = c(1L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 0L), Classic = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L,
0L), Foreign = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L), Thriller = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Action = c(0L, 0L, 0L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), Adventure = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L), Animation = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L), Adult = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), History = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Documentry = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), Nature = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L), Horror = c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L,
0L), Show = c(0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L), Series = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), BlackWhite = c(0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("Drama", "Comedy", "Crime",
"SciFi", "Kids", "Classic", "Foreign", "Thriller", "Action",
"Adventure", "Animation", "Adult", "History", "Documentry", "Nature",
"Horror", "Show", "Series", "BlackWhite"), row.names = c(NA,
10L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
我的代码:
Jaccard_dist <- dist(items, items, method = "Jaccard")
write.csv(Jaccard_dist,'Jaccard_dist.csv')
Run Code Online (Sandbox Code Playgroud)
你知道有一种方法可以在不使用两个 for 循环的情况下做到这一点吗?
不知道为什么需要两个 for 循环。
您可以尝试该库proxy并使用:
proxy::dist(dft, by_rows = TRUE, method = "Jaccard")
Run Code Online (Sandbox Code Playgroud)
这将返回:
#
1 2 3 4 5 6 7 8 9
#2 1.0000000
#3 1.0000000 0.6666667
#4 0.8000000 0.8000000 1.0000000
#5 1.0000000 0.8000000 0.6666667 0.8000000
#6 1.0000000 1.0000000 1.0000000 0.6666667 0.6666667
#7 1.0000000 1.0000000 1.0000000 0.7500000 0.7500000 0.5000000
#8 0.5000000 1.0000000 1.0000000 0.5000000 0.8000000 0.6666667 0.7500000
#9 1.0000000 1.0000000 1.0000000 0.6666667 0.6666667 0.0000000 0.5000000 0.6666667
#10 1.0000000 1.0000000 1.0000000 0.7500000 0.7500000 0.5000000 0.6666667 0.7500000 0.5000000
Run Code Online (Sandbox Code Playgroud)
小智 5
看起来 R 原生 dist() 函数的“二元”方法实际上确实提供了杰卡德距离,而没有具体命名。描述符合(“向量被视为二进制位,因此非零元素为 \xe2\x80\x98on\xe2\x80\x99,零元素为 \xe2\x80\x98off\xe2\x80\x99。距离是其中至少有一个打开的位中只有一个打开的位的比例。”)输出也是如此(与接受的答案完全相同):
\n\n> dist(data, method = "binary")\n 1 2 3 4 5 6 7 8 9\n2 1.0000000 \n3 1.0000000 0.6666667 \n4 0.8000000 0.8000000 1.0000000 \n5 1.0000000 0.8000000 0.6666667 0.8000000 \n6 1.0000000 1.0000000 1.0000000 0.6666667 0.6666667 \n7 1.0000000 1.0000000 1.0000000 0.7500000 0.7500000 0.5000000 \n8 0.5000000 1.0000000 1.0000000 0.5000000 0.8000000 0.6666667 0.7500000 \n9 1.0000000 1.0000000 1.0000000 0.6666667 0.6666667 0.0000000 0.5000000 0.6666667 \n10 1.0000000 1.0000000 1.0000000 0.7500000 0.7500000 0.5000000 0.6666667 0.7500000 0.5000000\nRun Code Online (Sandbox Code Playgroud)\n