【问题标题】:Good Example to model SVM String Kernel in Caret?在 Caret 中建模 SVM 字符串内核的好例子?
【发布时间】:2020-11-09 08:39:14
【问题描述】:

这里我尝试使用 Caret 对 SVM 字符串内核建模

使用数据集:

library(caret)
library(mlbench)
library(dplyr)
data("HouseVotes84")
dummy_data_classif <- HouseVotes84[,2:length(colnames(HouseVotes84))] %>% 
  mutate_if(is.factor, as.numeric)
dummy_data_classif <- data.frame(cbind(Class=HouseVotes84[,1], dummy_data_classif))
dummy_data_classif[is.na(dummy_data_classif)] <- 0
dummy_data_classif <- as.matrix(dummy_data_classif)
dummy_y_classif <- as.matrix(dummy_data_classif[,which(colnames(dummy_data_classif) == "Class")])
colnames(dummy_y_classif) <- "Class"
dummy_x_classif <- dummy_data_classif[,-which(colnames(dummy_data_classif) == "Class")]

data("cars") #available from caret
dummy_data_regr <- cars
dummy_data_regr <- dummy_data_regr %>%
mutate_if(is.numeric, as.character)
dummy_data_regr <- dummy_data_regr %>%
mutate_if(is.integer, as.character)
dummy_data_regr <- as.matrix(dummy_data_regr)
dummy_y_regr <- as.matrix(dummy_data_regr[,which(colnames(dummy_data_regr) == "Price")])
colnames(dummy_y_classif) <- "Price"
dummy_x_regr <- dummy_data_regr[,-which(colnames(dummy_data_regr) == "Price")]

使用重采样

resampling <- trainControl(method = "cv",
                               number = 5,
                               allowParallel = FALSE) 

我尝试用 3 种方法测试这些:svmBoundrangeString, svmExpoString, svmSpectrumString

test_method <- c("svmBoundrangeString", "svmExpoString", "svmSpectrumString")
model_reg <- caret::train(x=dummy_x_regr,
                      y=dummy_y_regr, 
                      data = dummy_data, 
                      method = test_method[1], 
                      trControl = resampling)

model_cls <- caret::train(x=dummy_x_classif,
                      y=dummy_y_classif, 
                      data = dummy_data, 
                      method = test_method[1], 
                      trControl = resampling)

但这不起作用,缺少指标,如果我尝试对这些方法做:

Something is wrong; all the Accuracy metric values are missing

 Accuracy       Kappa    
 Min.   : NA   Min.   : NA  
 1st Qu.: NA   1st Qu.: NA  
 Median : NA   Median : NA  
 Mean   :NaN   Mean   :NaN  
 3rd Qu.: NA   3rd Qu.: NA  
 Max.   : NA   Max.   : NA  
 NA's   :9     NA's   :9  

我该怎么做才能让它发挥作用?或者这些方法可能需要特定的数据框?

【问题讨论】:

    标签: r string svm r-caret


    【解决方案1】:

    这三种方法是基于string kernel,我不太确定它如何用于回归,但在分类中,你会将文本作为自变量。对于kernlab,您可以将其作为列表提供,参见this vignette too

    library(kernlab)
    data(reuters)
    
    head(reuters[1:2])
    [[1]]
    [1] "Computer Terminal Systems Inc said \nit has completed the sale of 200,000 shares of its common \nstock, and warrants to acquire an additional one mln shares, to \n<Sedio N.V.> of Lugano, Switzerland for 50,000 dlrs. \n    The company said the warrants are exercisable for five \nyears at a purchase price of .125 dlrs per share. \n    Computer Terminal said Sedio also has the right to buy \nadditional shares and increase its total holdings up to 40 pct \nof the Computer Terminal's outstanding common stock under \ncertain circumstances involving change of control at the \ncompany. \n    The company said if the conditions occur the warrants would \nbe exercisable at a price equal to 75 pct of its common stock's \nmarket price at the time, not to exceed 1.50 dlrs per share. \n    Computer Terminal also said it sold the technolgy rights to \nits Dot Matrix impact technology, including any future \nimprovements, to <Woodco Inc> of Houston, Tex. for 200,000 \ndlrs. But, it said it would continue to be the exclusive \nworldwide licensee of the technology for Woodco. \n    The company said the moves were part of its reorganization \nplan and would help pay current operation costs and ensure \nproduct delivery. \n    Computer Terminal makes computer generated labels, forms, \ntags and ticket printers and terminals. \n Reuter"
    
    [[2]]
    [1] "Ohio Mattress Co said its first \nquarter, ending February 28, profits may be below the 2.4 mln \ndlrs, or 15 cts a share, earned in the first quarter of fiscal \n1986. \n    The company said any decline would be due to expenses \nrelated to the acquisitions in the middle of the current \nquarter of seven licensees of Sealy Inc, as well as 82 pct of \nthe outstanding capital stock of Sealy. \n    Because of these acquisitions, it said, first quarter sales \nwill be substantially higher than last year's 67.1 mln dlrs. \n    Noting that it typically reports first quarter results in \nlate march, said the report is likely to be issued in early \nApril this year. \n    It said the delay is due to administrative considerations, \nincluding conducting appraisals, in connection with the \nacquisitions. \n Reuter"
    
     str(rlabels)
     Factor w/ 2 levels "acq","crude": 1 1 1 1 1 1 1 1 1 1 ...
    
    mdl <- ksvm(reuters,rlabels,kernel="stringdot",kpar=list(length=5,type = "boundrange"),C=3)
    

    现在,如果您为此使用插入符号,您可以看到它是如何使用getModelInfo("svmBoundrangeString") 调用的,并且本质上,您将自变量作为具有 1 列和列名称的矩阵提供(我在下面使用了 cbind):

    mdl = train(x=cbind(reuters=reuters),y=rlabels,
    method="svmBoundrangeString",trControl=trainControl(method="cv"))
    
    Support Vector Machines with Boundrange String Kernel 
    
    40 samples
     1 predictor
     2 classes: 'acq', 'crude' 
    
    No pre-processing
    Resampling: Cross-Validated (10 fold) 
    Summary of sample sizes: 36, 36, 36, 36, 36, 36, ... 
    Resampling results across tuning parameters:
    
      length  C     Accuracy  Kappa
      2       0.25  0.775     0.55 
      2       0.50  0.775     0.55 
      2       1.00  0.775     0.55 
      3       0.25  0.800     0.60 
      3       0.50  0.800     0.60 
      3       1.00  0.800     0.60 
      4       0.25  0.825     0.65 
      4       0.50  0.825     0.65 
      4       1.00  0.825     0.65 
    
    Accuracy was used to select the optimal model using the largest value.
    The final values used for the model were length = 4 and C = 0.25.
    

    【讨论】:

    • 哇,再次感谢@StupidWolf,真的很佩服,我觉得我从你那里学到了很多:0,那么基于字符串内核的方法实际上是一种文本分类方法吗?如果预测变量是非因子,这会起作用吗?
    • 你可以让它以某种方式工作,但这没有意义。内核用于字符串
    猜你喜欢
    • 2014-03-07
    • 2016-01-24
    • 2018-08-18
    • 2016-01-21
    • 2020-03-19
    • 1970-01-01
    • 2014-09-17
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多