【发布时间】:2016-09-08 02:26:34
【问题描述】:
我正在尝试在 R 中实现内核岭回归。
公式为:
alpha <- ((lambda.I + K)^(-1)) * y
Lambda = 0.1。 I = 与 K 大小相同的单位矩阵。y 是与 K 具有相同行数的特征向量。
所以我在 R 中尝试了这个:
I <- diag(nrow(df_matrix)
lambda <- 0.1
alpha <- (lambda * I + df_matrix) ^ (-1) * df_vector
我收到以下错误
Error in (0.1 * I + df_matrix)^(-1) * df_vector : non-conformable arrays
这是关于我的数据集的一些信息
> nrow(df_matrix)
[1] 8222
> ncol(df_matrix)
[1] 8222
> nrow(df_vector)
[1] 8222
> nrow(I)
[1] 8222
> ncol(I)
[1] 8222
> class(df_matrix)
[1] "matrix"
> class(df_vector)
[1] "matrix"
【问题讨论】:
-
R 中矩阵求逆的语法不是
^(-1)。而*不是 R 中的矩阵乘法。 -
什么是 ncol(df_vector)?如果您假设它是 8222 或 1,那么代码似乎可以工作