[英]R matrix transposing error “requires numeric/complex matrix/vector arguments”
[英]Matrix Error: Requires numeric in R
Phi <- as.matrix(cbind(1, train.data)) # add a column of 1 as phi_0
train.len <- 75
eta <- 0.01 # Learning rate
epsilon <- 0.001 #
tau.max <- 75 # Maximum number of iterations
T <- ifelse(train.label == c0, eval(parse(text=c0)),eval(parse(text=c1))) labels
W <- matrix(,nrow=tau.max, ncol=ncol(Phi)) # Empty Weight vector
W[1,] <- runif(ncol(Phi)) # Random initial values for weight vector
error.trace <- matrix(0,nrow=tau.max, ncol=1) # Placeholder for errors
error.trace[1] <- sum((Phi%*%W[1,])*T<0)/train.len*100
train.data
看起來像:
x1 x2 x3 x4 y
5.1 3.5 1.4 0.2 C1
4.7 3.2 1.3 0.2 C1
35.0 3.6 1.4 0.2 C1
問題發生在最后一行。 我檢查了其他一些帖子( 矩陣表達式導致錯誤“需要數字/復雜矩陣/矢量參數”嗎? )。 他們說要確保數據為矩陣格式,我還檢查了它們都是數字( Phi
和W
)。
我得到的錯誤是
Phi%*%W [1,]中的錯誤:需要數字/復雜矩陣/矢量參數
生成一些玩具數據時,沒有錯誤: train.data <- rnorm(75)
。 你的train.data
是什么? 一個數據框? 如果是這樣, sapply(train.data, class)
什么? 我懷疑您有因素/性格。 在這種情況下,您的矩陣Phi
將是一個字符矩陣,而不是數字矩陣。
也許您應該嘗試使用:
Phi <- cbind(1, data.matrix(train.data))
閱讀?data.matrix
了解更多。 另一種方法是使用:
Phi <- cbind(1, sapply(train.data, as.numeric))
更新資料
確實,您有y
列,它可以是因子列或字符列。 我上面的解決方案將起作用。 但是,我不知道您的上下文,因此您應該問自己:包含y
列進行計算是否有意義。 如果您不想要它,請將其放下並使用:
Phi <- cbind(1, as.matrix(train.data[,1:4]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.