使用 `Matrix::sparseMatrix` 制作稀疏矩阵时列顺序错误 [R]
Columns in a wrong order when making a sparse matrix with `Matrix::sparseMatrix` [R]
我有一个类似于以下的数据框:
sparsed <-
structure(list(Movie = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4,
4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8), User = c(32, 2, 56, 34,
56, 89, 4, 2, 46, 89, 67, 56, 12, 35, 89, 2, 90, 12, 5, 78, 69,
32, 64, 56, 2), Rating = c(1L, 3L, 2L, 4L, 5L, 3L, 2L, 3L, 4L,
5L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 4L, 3L, 3L, 2L, 2L, 1L, 1L
)), .Names = c("Movie", "User", "Rating"), row.names = c(NA,
-25L), class = "data.frame")
将值放入稀疏矩阵的逻辑是什么?为了将它变成正确维度 (8 x 15) 的稀疏矩阵,我必须进行以下奇数转换,否则它会变成 8 x 90。
library(Matrix)
sparsed$Movie <- as.factor(as.character(sparsed$Movie))
sparsed$User <- as.factor(as.character(sparsed$User))
sparse <- sparseMatrix(i = as.numeric(sparsed$Movie),
j = as.numeric(sparsed$User),
x = as.numeric(sparsed$Rating))
#8 x 15 sparse Matrix of class "dgCMatrix"
#[1,] . 3 1 . . . . . 2 . . . . . .
#[2,] . . . 4 . . . . 5 . . . . 3 .
#[3,] . 3 . . . 2 4 . . . 2 . . 5 .
#[4,] 5 . . . . . . . 3 . . . . . .
#[5,] 5 3 . . 1 . . 4 . . . . 3 2 4
#[6,] . . 2 . . . . . . . . 3 . . .
#[7,] . . . . . . . . 1 2 . . . . .
#[8,] . 1 . . . . . . . . . . . . .
我现在对维度很满意但是矩阵列的顺序不正确。例如,第一列对应于电影 12 而不是 2。行的顺序是正确的数字顺序。谁能解释一下?有什么好的方法可以让列按正确的顺序排列吗?
sparseMatrix
没有做错什么。
在最终将 sparsed
列转换为因子之前,Movie
和 User
是数字,因此 2 是 2,12 是 12。max(Movie)
是 8 并且max(User)
是 90,所以你会得到一个 8 x 90 矩阵。
将这些列转换为因子后,您是否知道因子水平?
levels(sparsed$Movie)
#[1] "1" "2" "3" "4" "5" "6" "7" "8"
levels(sparsed$User)
#[1] "12" "2" "32" "34" "35" "4" "46" "5" "56" "64" "67" "69" "78" "89" "90"
级别不符合数字顺序,例如,12 在 2 之前。如果您对这些因素进行 as.numeric
,则第一个级别“12”将出现在第一个矩阵列中,级别“32” " 将出现在第 3 列中。如果您希望它们以正确的数字顺序排列,请使用 factor
而不是 as.factor
:
来控制级别
## take your original `sparsed` data frame, where `User` is numeric
sparsed$User <- as.numeric(
factor(as.character(sparsed$User),
levels = sort(unique(sparsed$User)))
)
## no need to do anything with `Movie` at it's already contiguous numeric from 1
sparse <- sparseMatrix(i = sparsed$Movie,
j = sparsed$User,
x = sparsed$Rating)
#8 x 15 sparse Matrix of class "dgCMatrix"
#
#[1,] 3 . . . 1 . . . 2 . . . . . .
#[2,] . . . . . 4 . . 5 . . . . 3 .
#[3,] 3 2 . . . . . 4 . . 2 . . 5 .
#[4,] . . . 5 . . . . 3 . . . . . .
#[5,] 3 . 4 5 . . 1 . . . . . 3 2 4
#[6,] . . . . 2 . . . . . . 3 . . .
#[7,] . . . . . . . . 1 2 . . . . .
#[8,] 1 . . . . . . . . . . . . . .
我有一个类似于以下的数据框:
sparsed <-
structure(list(Movie = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4,
4, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8), User = c(32, 2, 56, 34,
56, 89, 4, 2, 46, 89, 67, 56, 12, 35, 89, 2, 90, 12, 5, 78, 69,
32, 64, 56, 2), Rating = c(1L, 3L, 2L, 4L, 5L, 3L, 2L, 3L, 4L,
5L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L, 4L, 3L, 3L, 2L, 2L, 1L, 1L
)), .Names = c("Movie", "User", "Rating"), row.names = c(NA,
-25L), class = "data.frame")
将值放入稀疏矩阵的逻辑是什么?为了将它变成正确维度 (8 x 15) 的稀疏矩阵,我必须进行以下奇数转换,否则它会变成 8 x 90。
library(Matrix)
sparsed$Movie <- as.factor(as.character(sparsed$Movie))
sparsed$User <- as.factor(as.character(sparsed$User))
sparse <- sparseMatrix(i = as.numeric(sparsed$Movie),
j = as.numeric(sparsed$User),
x = as.numeric(sparsed$Rating))
#8 x 15 sparse Matrix of class "dgCMatrix"
#[1,] . 3 1 . . . . . 2 . . . . . .
#[2,] . . . 4 . . . . 5 . . . . 3 .
#[3,] . 3 . . . 2 4 . . . 2 . . 5 .
#[4,] 5 . . . . . . . 3 . . . . . .
#[5,] 5 3 . . 1 . . 4 . . . . 3 2 4
#[6,] . . 2 . . . . . . . . 3 . . .
#[7,] . . . . . . . . 1 2 . . . . .
#[8,] . 1 . . . . . . . . . . . . .
我现在对维度很满意但是矩阵列的顺序不正确。例如,第一列对应于电影 12 而不是 2。行的顺序是正确的数字顺序。谁能解释一下?有什么好的方法可以让列按正确的顺序排列吗?
sparseMatrix
没有做错什么。
在最终将 sparsed
列转换为因子之前,Movie
和 User
是数字,因此 2 是 2,12 是 12。max(Movie)
是 8 并且max(User)
是 90,所以你会得到一个 8 x 90 矩阵。
将这些列转换为因子后,您是否知道因子水平?
levels(sparsed$Movie)
#[1] "1" "2" "3" "4" "5" "6" "7" "8"
levels(sparsed$User)
#[1] "12" "2" "32" "34" "35" "4" "46" "5" "56" "64" "67" "69" "78" "89" "90"
级别不符合数字顺序,例如,12 在 2 之前。如果您对这些因素进行 as.numeric
,则第一个级别“12”将出现在第一个矩阵列中,级别“32” " 将出现在第 3 列中。如果您希望它们以正确的数字顺序排列,请使用 factor
而不是 as.factor
:
## take your original `sparsed` data frame, where `User` is numeric
sparsed$User <- as.numeric(
factor(as.character(sparsed$User),
levels = sort(unique(sparsed$User)))
)
## no need to do anything with `Movie` at it's already contiguous numeric from 1
sparse <- sparseMatrix(i = sparsed$Movie,
j = sparsed$User,
x = sparsed$Rating)
#8 x 15 sparse Matrix of class "dgCMatrix"
#
#[1,] 3 . . . 1 . . . 2 . . . . . .
#[2,] . . . . . 4 . . 5 . . . . 3 .
#[3,] 3 2 . . . . . 4 . . 2 . . 5 .
#[4,] . . . 5 . . . . 3 . . . . . .
#[5,] 3 . 4 5 . . 1 . . . . . 3 2 4
#[6,] . . . . 2 . . . . . . 3 . . .
#[7,] . . . . . . . . 1 2 . . . . .
#[8,] 1 . . . . . . . . . . . . . .