在R中进行矩阵乘法时的非共形阵列

在R中进行矩阵乘法时的非共形阵列,r,matrix,R,Matrix,我试图在R中实现内核岭回归 公式是: alpha <- ((lambda.I + K)^(-1)) * y 以下是关于我的数据集的一些信息 > 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" > cl

我试图在R中实现内核岭回归

公式是:

alpha <- ((lambda.I + K)^(-1)) * y
以下是关于我的数据集的一些信息

> 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"

我打赌你想在这里进行矩阵求逆,即
solve(m)
,而不是按元素(
m^(-1)
)。另外,矩阵乘法(
%*%
)代替了元素乘法(
*
)。所以,总的来说是

alpha <- solve(lambda * I + df_matrix) %*% df_vector

alpha您需要使用矩阵乘法,
%*%
。此外,您还需要使用
求解
来计算逆,因为增加到幂-1只会进行元素的倒数。e、 g:

K <- matrix(runif(9),3)
y <- matrix(runif(3),nrow=3)

solve(lambda*diag(nrow(K))+K) %*% y
            [,1]
[1,]  0.50035075
[2,] -0.04985508
[3,]  0.74944867

K要转换矩阵,矩阵必须是二次矩阵,行列式必须不同于零。如果矩阵df_矩阵满足这些要求,则

alpha <- solve(lambda * I + df_matrix) %*% df_vector

alpha在R中反转矩阵的语法不是
^(-1)
。而
*
不是R中的矩阵乘法。什么是ncol(df_向量)?如果假设它是8222或1,那么代码似乎有效,当然,
solve(lambda*I+df\u矩阵,df\u向量)
会更合理/有效。
K <- matrix(runif(9),3)
y <- matrix(runif(3),nrow=3)

solve(lambda*diag(nrow(K))+K) %*% y
            [,1]
[1,]  0.50035075
[2,] -0.04985508
[3,]  0.74944867
alpha <- solve(lambda * I + df_matrix) %*% df_vector