r - R 中的矩阵乘法 : requires numeric/complex matrix/vector arguments

标签 r matrix matrix-multiplication multiplication

我正在使用 mlbench 包中的数据集 BreastCancer,并且我尝试执行以下矩阵乘法作为逻辑回归的一部分。

我获取了前 10 列中的特征,并创建了一个名为 theta 的参数向量:

X <- BreastCancer[, 1:10]
theta <- data.frame(rep(1, 10))

然后我进行了以下矩阵乘法:

constant <- as.matrix(X) %*% as.vector(theta[, 1])

但是,我收到以下错误:

Error in as.matrix(X) %*% as.vector(theta[, 1]) : 
  requires numeric/complex matrix/vector arguments

我需要先使用 as.numeric(X) 将矩阵转换为 double 吗? X 中的值看起来像字符串,因为它们带有双引号。

最佳答案

矩阵乘法运算符/函数,如 "%*%"crossprodtcrossprod 期望矩阵具有“numeric”、“complex”或“逻辑”模式。但是,您的矩阵具有“字符”模式。

library(mlbench)
data(BreastCancer)
X <- as.matrix(BreastCancer[, 1:10])
mode(X)
#[1] "character"

您可能会感到惊讶,因为数据集似乎包含数字数据:

head(BreastCancer[, 1:10])
#       Id Cl.thickness Cell.size Cell.shape Marg.adhesion Epith.c.size
#1 1000025            5         1          1             1            2
#2 1002945            5         4          4             5            7
#3 1015425            3         1          1             1            2
#4 1016277            6         8          8             1            3
#5 1017023            4         1          1             3            2
#6 1017122            8        10         10             8            7
#  Bare.nuclei Bl.cromatin Normal.nucleoli Mitoses
#1           1           3               1       1
#2          10           3               2       1
#3           2           3               1       1
#4           4           3               7       1
#5           1           3               1       1
#6          10           9               7       1

但是你被打印风格误导了。 这些列实际上是字符或因素:

lapply(BreastCancer[, 1:10], class)
#$Id
#[1] "character"
#
#$Cl.thickness
#[1] "ordered" "factor" 
#
#$Cell.size
#[1] "ordered" "factor" 
#
#$Cell.shape
#[1] "ordered" "factor" 
#
#$Marg.adhesion
#[1] "ordered" "factor" 
#
#$Epith.c.size
#[1] "ordered" "factor" 
#
#$Bare.nuclei
#[1] "factor"
#
#$Bl.cromatin
#[1] "factor"
#
#$Normal.nucleoli
#[1] "factor"
#
#$Mitoses
#[1] "factor"

当您执行as.matrix时,这些列都被强制转换为“字符”(请参阅​​R: Why am I not getting type or class "factor" after converting columns to factor?以获取完整的解释)。

因此,要进行矩阵乘法,我们需要正确地将这些列强制为“数字”。

<小时/>
dat <- BreastCancer[, 1:10]

## character to numeric
dat[[1]] <- as.numeric(dat[[1]])

## factor to numeric
dat[2:10] <- lapply( dat[2:10], function (x) as.numeric(levels(x))[x] )

## get the matrix
X <- data.matrix(dat)
mode(X)
#[1] "numeric"

现在您可以进行矩阵向量乘法等操作。

## some possible matrix-vector multiplications
beta <- runif(10)
yhat <- X %*% beta

## add prediction back to data frame
dat$prediction <- yhat

但是,我怀疑这是否是获取逻辑回归模型预测值的正确方法,因为当您使用因子构建模型时,模型矩阵不是上面的 X 而是一个虚拟矩阵。我强烈建议您使用预测

<小时/>

This line also worked for me: as.matrix(sapply(dat, as.numeric))

看来你很幸运。数据集的因子水平恰好与数值相同。一般来说,将因子转换为数字应该使用我所做的方法。比较

f <- gl(4, 2, labels = c(12.3, 0.5, 2.9, -11.1))
#[1] 12.3  12.3  0.5   0.5   2.9   2.9   -11.1 -11.1
#Levels: 12.3 0.5 2.9 -11.1

as.numeric(f)
#[1] 1 1 2 2 3 3 4 4

as.numeric(levels(f))[f]
#[1] 12.3  12.3  0.5   0.5   2.9   2.9   -11.1 -11.1

文档页面 ?factor 对此进行了介绍。

关于r - R 中的矩阵乘法 : requires numeric/complex matrix/vector arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325165/

相关文章:

r - 黑客在 Haven::read_sav() 的文件路径中包含特殊字符

r - 合并两个具有重复列的数据框

c - 尝试填充结构中包含的矩阵时出现段错误

Python Numpy - 矩阵替换全局矩阵中定义的矩阵..类似于 Matlab

python - 将两个具有不同维度、标签的矩阵相加并在总和矩阵中保留标签

python - Pytorch 跨不同数组的行进行点积

c++ - 命名空间 'std' 中的“函数”未命名模板类型

c++ - 在CUDA中乘以矢量化二维方阵和压缩三对角矩阵

python - Pytorch 中的批量矩阵乘法 - 与输出维度的处理混淆

r - 矩阵乘法向量 - R 与 Matlab