簡體   English   中英

如何為 class 創建方法?

[英]How do I create methods for a class?

在一個作業中,我寫了下面的 function (learnvq),它計算一些數據點與其他標記數據點的接近度,然后根據最近的點將這些標簽分配給第一組。 我從這個 function 返回 output 作為 class“learnvq”的列表。

我現在需要為此 class 編寫一個方法,該方法將使用一個新矩陣並根據 function 的輸出預測標簽。 我已經寫了 function 並且它似乎工作正常,但我不知道如何將它添加為 class “learnvq”的方法?

n <- 100
X <- cbind(x1 = runif(n, -1.5, 1.5),
       x2 = runif(n, -1.5, 1.5)) # Generate random points
y <- as.integer(rowSums(X^2)<1) # Determine whether inside the circle#
idx <- sample(100, 10) # Mess up 10 class labels ...
y[idx] <- 1-y[idx] # ... by flipping the label

################################################# ############################################

第 1 部分:定義 class learnvq

learnvq <- function(X,y,K=10, eta=.25, alpha=.5, H=25) {
is <- c(sample(which(y==0),K,replace=TRUE), sample(which(y==1),K,replace=TRUE))
X.prototype <- X[is,]
y.prototype <- y[is]

#Function closest to find the position of my matrix which contains the closest point to a vector
closest <- function(mat,vec) { # function to calculate the closest value
dist <-  0* mat[,1] # creates the empty vector same length as number of rows

for (i in 1:nrow(mat))  # loops through the matrix
{dist[i] <- sum((mat[i,] - vec)^2)} # subtracts the vector from the row and takes the absolute value. 
Assigns the value to dist[i]

min.coord <- which.min(dist) # takes the smallest value- equates to the closest distance
return(min.coord)          # returns the position of the smallest value

}
Covariate.matrix <- X[is,] # copies covariates matrix X for use in the loops below- dont want to 
update the original above- need that too.
###put the loops here ###
for (h in 1:H){
for (i in 1:n){ 
  # finding prototype x.star in k closest to xi
  k <- closest(X.prototype,X[i,])
  #updating the X.prototype and Y.prototype with the information from k:
  if (y[i] == y.prototype[k])
    {
    X.prototype[k,] <- X.prototype[k,] + eta*(X[i,]-X.prototype[k,])
    }
  
  else   {
    X.prototype[k,] <- X.prototype[k,] - eta*(X[i,]-X.prototype[k,])
  }
  }
  }

  object <- list(Covariate.matrix=Covariate.matrix, y.prototypes= y.prototype, X.prototypes.star= 
  X.prototype,y.prototypes.star = y.prototype)
  class(object) <- "learnvq"
  object
  }

  check <- learnvq(X,y)

################################################# ################################################# ###

第 2 部分:預測方法

  predict.learnvq <- function(object, New.Mat) {

  #Function closest to find the position of my matrix which contains the closest point to a vector
  closest <- function(mat,vec) { # function to calculate the closest value
  dist <-  0* mat[,1] # creates the empty vector same length as number of rows

  for (i in 1:nrow(mat))  # loops through the matrix
  {dist[i] <- sum((mat[i,] - vec)^2)} # subtracts the vector from the row. Assigns the value to 
  dist[i]

  min.coord <- which.min(dist) # takes the smallest value- equates to the closest distance
  return(min.coord)          # returns the position of the smallest value

  }

  #extracts the rows from X.new and compares them to the X.prototypes and then calculates the 
  corresponding y label
  if (missing(New.Mat)) # if statement to allow for missing X.new argument
  {New.Mat = object$Covariate.matrix} # if New.Mat is omitted it uses the covariates of the training 
  data- 

  y.proto.test<- 0*nrow(New.Mat)
  for (j in 1:nrow(New.Mat))# loops through the matrix
  {y.proto.test[j] <- closest(check$X.prototypes.star, New.Mat[j,])} # returns the y.prototype 
  position that the new rows are closest to.
  estimated.class.labels <- check$y.prototypes.star[y.proto.test] # contains the row #s of the points 
  closest to the X.new points.
  return(estimated.class.labels)

  }

  X.new <- cbind(x1=runif(10, -1.5, 1.5),  #creates a vector of unclassified points
           x2=runif(10, -1.5, 1.5))
  fitted.y <- predict.learnvq(check$X.prototypes.star, X.new)

我不知道如何將它添加為 class“learnvq”的方法?

你已經做到了。 通過命名您的方法predict.learnqv ,您已經為您的learnqv class 創建了一個predict方法。 您現在可以調用它:

result = predict(learnqv_object, other_arguments…)

…這將自動調用predict.learnqv

如果您在 package 中編寫此代碼,您還需要注意正確導出 S3 方法名稱,但對於“簡單”的 R 代碼,上面的代碼就足夠了。 請閱讀Advanced R有關 S3 的章節,它包含更多相關信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM