2

我想比较两种不同的分类方法,分别是库party和c50中的ctree和C5.0,比较是为了测试它们对初始起点的敏感性。每次计算错误分类项目的数量并存储在一个向量中,然后使用 t-test 进行测试,我希望看看它们是否真的不同。

library("foreign"); # for read.arff
library("party") # for ctree 
library("C50") # for C5.0 

trainTestSplit <- function(data, trainPercentage){
    newData <- list();
    all <- nrow(data);
    splitPoint <- floor(all * trainPercentage);
    newData$train <- data[1:splitPoint, ];
    newData$test <- data[splitPoint:all, ];
    return (newData);

}

ctreeErrorCount <- function(st,ss){
    set.seed(ss);
    model <- ctree(Class ~ ., data=st$train);
    class <- st$test$Class;
    st$test$Class <- NULL;
    pre = predict(model, newdata=st$test, type="response");
    errors <- length(which(class != pre)); # counting number of miss classified items
    return(errors);
}
C50ErrorCount <- function(st,ss){
    model <- C5.0(Class ~ ., data=st$train, seed=ss);
    class <- st$test$Class;
    pre = predict(model, newdata=st$test, type="class");
    errors <- length(which(class != pre)); # counting number of miss classified items
    return(errors);
}

compare <- function(n = 30){
    data <- read.arff(file.choose());

    set.seed(100);
    errors = list(ctree = c(), c50 = c());
    seeds <- floor(abs(rnorm(n) * 10000));
    for(i in 1:n){
        splitData <- trainTestSplit(data, 0.66);
        errors$ctree[i] <- ctreeErrorCount(splitData, seeds[i]);
        errors$c50[i] <- C50ErrorCount(splitData, seeds[i]);
    }

    cat("\n\n");
    cat("============= ctree Vs C5.0 =================\n");
    cat(paste(errors$ctree, "            ", errors$c50, "\n"))
    tt <- t.test(errors$ctree, errors$c50);
    print(tt);

}

显示的程序据说是在做比较的工作,但是由于向量中的错误数量没有改变,所以 t.test 函数会产生错误。我在 R 中使用了 iris(但将类更改为类)和温彻斯特乳腺癌数据,可以在此处下载来测试它,但任何数据都可以使用,只要它具有类属性

但是我遇到的问题是,当我更改随机种子时,这两种方法的结果保持不变并且不会改变,理论上,如他们的文档中所述,这两个函数都使用随机种子,ctree 使用set.seed(x)而 C5.0 使用参数叫seed来设置种子,可惜我找不到效果。

你能告诉我如何控制这些功能的首字母吗

4

1 回答 1

3

在您将 ctrees 配置为使用随机选择的输入变量(即 ctree_control 中的 mtry > 0)的情况下,ctrees 仅依赖于随机种子。请参阅http://cran.r-project.org/web/packages/party/party.pdf(第 11 页)

对于 C5.0-trees,种子是这样使用的:

  ctrl = C5.0Control(sample=0.5, seed=ss);
  model <- C5.0(Class ~ ., data=st$train, control = ctrl);

请注意,种子用于选择数据样本,而不是在算法本身内。请参阅http://cran.r-project.org/web/packages/C50/C50.pdf(第 5 页)

于 2014-03-14T10:24:49.463 回答