统计学习方法——感知机算法(基于R语言)

来源:互联网 发布:和邻居做了 知乎 编辑:程序博客网 时间:2024/04/30 02:32

算法2.1

参考(原文基于python实现,改写为R的实现,对作者表示感谢!)

算法2.1的第二种R实现方式看这里

train <- function(mat) {  nr <- nrow(mat)  nc <- ncol(mat)  w0 <- matrix(0,nc - 1,1)  b0 <- 0        n <- 1       #学习率  study_count <- 0  nochange_count <- 0  nochange_upper_limit <- 100000       #连续分类正确次数上限  study_total <- 10000                 #学习次数总数  while (TRUE) {    nochange_count <- nochange_count + 1    if (nochange_count > nochange_upper_limit) {break}    #连续分类正确次数大于上限时,将跳出while循环,认为训练完成    index <- sample(nr,1,replace = FALSE)    img <- mat[index,1:(nc-1)]    yi <- mat[index,nc]    result <- (img %*% w0 + b0) *yi    if (result <= 0) {      img <- matrix(mat[index,1:(nc-1)],nc-1,1)      w0 <- w0 + n * yi * img      b0 <- b0 + n * yi      study_count <- study_count + 1      if (study_count > study_total) {break}    ##学习次数超过10000次,将跳出while循环,认为训练完成      nochange_count <- 0                       ##分类错误的话,nochange_count重归0    }    #print(index)   }  wb <- list(w0 = w0,b0 = b0)  return (wb)}mat <- matrix(c(3,3,1,4,3,1,1,1,-1),nrow = 3,byrow = TRUE)#例题mat# debug(train) #调试# train(mat)# undebug(train)wb <- train(mat)wb#$w0#     [,1]#[1,]    1#[2,]    1#$b0#[1] -3###predict functionpredict <- function(test_mat) {  result <- NULL  for ( i in 1:nrow(test_mat)){    result_1 <-  test_mat[i,] %*% wb$w0 + wb$b0     result_1 <- ifelse(result_1 > 0,1,-1)    result <- rbind(result,result_1)  }  return(result)}test_ <- matrix(seq(-100,99,by = 1),100,2,byrow = TRUE)predict(test_)

算法2.2

#计算gram矩阵caculate_gram <- function(mat) {  mat_g <- mat[,-ncol(mat)]    ##最后一列为标签值  #mat_g  mat_gr <- matrix(0,nrow(mat_g),nrow(mat_g))  for (i in 1:nrow(mat_g)) {    for (j in 1:nrow(mat_g)) {      mat_gr[i,j] <- mat_g[i,] %*% mat_g[j,]    }  }  return(mat_gr)}matr <- matrix(seq(1:1000),100,10)caculate_gram(matr)#计算gram矩阵结束mat <- matrix(c(3,3,1,4,3,1,1,1,-1),nrow = 3,byrow = TRUE)#例题gram_mat <- caculate_gram(mat)      #计算gram矩阵train_2 <- function(mat) {  nr <- nrow(mat)  nc <- ncol(mat)  alpha <- matrix(0,nr,1)  b0 <- 0  n <- 1     #学习率  study_count <- 0  nochange_count <- 0  nochange_upper_limit <- 100000       #连续分类正确次数上限  study_total <- 10000                 #学习次数总数  while (TRUE) {    nochange_count <- nochange_count + 1    if (nochange_count > nochange_upper_limit) {break}    #连续分类正确次数大于上限时,将跳出while循环,认为训练完成    index <- sample(nr,1,replace = FALSE)    yi <- mat[index,nc]    result <- yi * (sum(alpha * mat[,nc] * gram_mat[index,]) + b0)    if (result <= 0) {      alpha[index,1] <- alpha[index,1] + n      b0 <- b0 + n * yi      study_count <- study_count + 1      if (study_count > study_total) break      ##学习次数超过10000次,将跳出while循环,认为训练完成         nochange_count <- 0                       ##分类错误的话,nochange_count重归0    }    print(index)  }  #计算w  w <- 0  for (k in 1:nr) {    w0 <- alpha[k,1] * mat[k,-nc] * mat[k,nc]    w <- w + w0   }  #print(index)  wb <- list(alpha = alpha,w = w,b0 = b0, nochange_count = nochange_count,study_count = study_count)  return (wb)}wb <- train_2(mat)wb         #算法有多个解#$w#[1] 2 1#$b0#[1] -5#$nochange_count#[1] 100001#$study_count#[1] 11#predict functionpredict <- function(test_mat) {  result <- NULL  for ( i in 1:nrow(test_mat)){    result_1 <-  test_mat[i,] %*% wb$w + wb$b0     result_1 <- ifelse(result_1 > 0,1,-1)    result <- rbind(result,result_1)  }  return (result)}test_ <- matrix(seq(-100,99,by = 1),100,2,byrow = TRUE)predict(test_)
原创粉丝点击