r - 如何使用 SOM 算法进行分类预测

标签 r classification prediction som

我想看看 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

最佳答案

这可能有帮助:

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

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

不过,对于这个特定的数据集,模型很快就会过度拟合: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)

关于r - 如何使用 SOM 算法进行分类预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45098820/

相关文章:

r - 以交互方式处理占用大量内存的列表对象

r - Mac的BiodiversityR软件包的R错误消息

c++ - 如何尽可能均匀地分类(或分配)结构?

machine-learning - 模型不平衡,不知道要采取什么步骤

machine-learning - 用于预测的 Bootstrap 置信区间

algorithm - 根据过去发生的事件预测下一个事件的发生

r - `sym()` 对 tidyeval 做了什么?

R twitter 经纬度显示 NA

scala - 为什么 Scala Maven Artifact 为每个 Scala 版本都有一个 Artifact ,而不是每个 Scala 版本都有一个分类器?

c# - 使用ID3算法、Accord.Net框架进行预测