【问题标题】:How can I use SOM algorithm for classification prediction如何使用 SOM 算法进行分类预测
【发布时间】:2017-12-19 07:26:30
【问题描述】:

我想看看SOM算法是否可以用于分类预测。 我曾经在下面编码,但我发现分类结果远非正确。例如,在测试数据集中,我得到的不仅仅是训练目标变量中的 3 个值。如何创建与训练目标变量一致的预测模型?

library(kohonen)
    library(HDclassif)
    data(wine)
    set.seed(7)

    training <- sample(nrow(wine), 120)
    Xtraining <- scale(wine[training, ])
    Xtest <- scale(wine[-training, ],
                   center = attr(Xtraining, "scaled:center"),
                   scale = attr(Xtraining, "scaled:scale"))

    som.wine <- som(Xtraining, grid = somgrid(5, 5, "hexagonal"))


som.prediction$pred <- predict(som.wine, newdata = Xtest,
                          trainX = Xtraining,
                          trainY = factor(Xtraining$class))

结果:

$unit.classif

 [1]  7  7  1  7  1 11  6  2  2  7  7 12 11 11 12  2  7  7  7  1  2  7  2 16 20 24 25 16 13 17 23 22
[33] 24 18  8 22 17 16 22 18 22 22 18 23 22 18 18 13 10 14 15  4  4 14 14 15 15  4

【问题讨论】:

    标签: r classification prediction som


    【解决方案1】:

    这可能会有所帮助:

    • SOM 是一种无监督分类算法,因此您不应期望它在包含分类器标签的数据集上进行训练(如果您这样做,它将需要此信息才能工作,并且对于未标记的数据集将无用)
    • 这个想法是,它将输入数字向量“转换”为网络单元号(尝试使用 1 per 3 网格再次运行您的代码,您将获得预期的输出)
    • 然后您需要将这些网络单元编号转换回您要查找的类别(这是您的代码中缺少的关键部分)

    下面的可重现示例将输出经典分类错误。它包括原始帖子中缺少的“转换回”部分的一种实现选项。

    不过,对于这个特定的数据集,模型很快就会过拟合:3 个单位给出了最好的结果。

    #Set and scale a training set (-1 to drop the classes)
    data(wine)
    set.seed(7)
    training <- sample(nrow(wine), 120)
    Xtraining <- scale(wine[training, -1])
    
    #Scale a test set (-1 to drop the classes)
    Xtest <- scale(wine[-training, -1],
                   center = attr(Xtraining, "scaled:center"),
                   scale = attr(Xtraining, "scaled:scale"))
    
    #Set 2D grid resolution
    #WARNING: it overfits pretty quickly
    #Errors are 36% for 1 unit, 63% for 2, 93% for 3, 89% for 4
    som_grid <- somgrid(xdim = 1, ydim=3, topo="hexagonal")
    
    #Create a trained model
    som_model <- som(Xtraining, som_grid)
    
    #Make a prediction on test data
    som.prediction <- predict(som_model, newdata = Xtest)
    
    #Put together original classes and SOM classifications
    error.df <- data.frame(real = wine[-training, 1],
                           predicted = som.prediction$unit.classif)
    
    #Return the category number that has the strongest association with the unit
    #number (0 stands for ambiguous)
    switch <- sapply(unique(som_model$unit.classif), function(x, df){
      cat <- as.numeric(names(which.max(table(
        error.df[error.df$predicted==x,1]))))
      if(length(cat)<1){
        cat <- 0
      }
      return(c(x, cat))
    }, df = data.frame(real = wine[training, 1], predicted = som_model$unit.classif))
    
    #Translate units numbers into classes
    error.df$corrected <- apply(error.df, MARGIN = 1, function(x, switch){
      cat <- switch[2, which(switch[1,] == x["predicted"])]
      if(length(cat)<1){
        cat <- 0
      }
      return(cat)
    }, switch = switch)
    
    #Compute a classification error
    sum(error.df$corrected == error.df$real)/length(error.df$real)
    

    【讨论】:

    • 感谢@Kevin Dallaporta 提供代码示例。我确实有 2 个问题,首先我使用了 trainY = factor(Xtraining$class) 但我在您的预测函数中没有看到它。第二,如何将类预测结果附加到测试数据集?
    • 我很高兴它有帮助!似乎trainY = factor 参数存在于kohonen 的V2.X 中,而在V3.X 中消失了。我不知道它应该做什么,但无论有没有,退货都是相同的,并且 2017 年 3 月的文档中没有任何痕迹。在我提供的代码中,预测结果在 error.df$corrected 中,因此您可以附加到测试中:test$predicted &lt;- error.df$corrected
    猜你喜欢
    • 2019-12-20
    • 2017-06-10
    • 1970-01-01
    • 2020-04-01
    • 2018-02-17
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多