以下是来自软件包kohonen 2.0.19版的文档的样本副本。您可以在https://www.rdocumentation.org/packages/kohonen/versions/2.0.19/topics/xyf
上看到它但是我尝试过却遇到了一些问题。
1。当我使用xyf()时,factor(wine.classes [training])中的错误:找不到对象'wine.classes'
2。然后,当尝试使用predict()时,找不到对象“ xyf.wines”
3。我将xyf()更改为 xyf.wines <-xyf(Xtraining,vintages [training],grid = somgrid(5,5,“ hexagonal”))它有效,我得到了xyf.wines
4。但是当我试图预测时,我又失败了。 Rstudio说FUN(X [[i]],...)中的错误: 不允许的数据类型:应为矩阵或因子 5.由于第4步失败,我无法使用table()
library(kohonen)
data(wines)
set.seed(7)
wine.classes<-vintages
training <- sample(nrow(wines), 120)
Xtraining <- scale(wines[training,])
Xtest <- scale(wines[-training,],center = attr(Xtraining, "scaled:center"),
scale = attr(Xtraining, "scaled:scale"))
xyf.wines <- xyf(Xtraining,factor(wine.classes[training]),
grid = somgrid(5, 5, "hexagonal"))
xyf.prediction <- predict(xyf.wines, newdata=Xtest)
table(wine.classes[-training], xyf.prediction$prediction)