[英]use xyf() function in Kohonen package to do some prediction in R code
以下是 package kohonen 2.0.19 版文檔的示例副本。 你可以在https://www.rdocumentation.org/packages/kohonen/versions/2.0.19/topics/xyf看到它
但是我已經嘗試過並遇到了一些問題。
1.When I use xyf(),Error in factor(wine.classes[training]): object'wine.classes' not found 問題
2.然后當嘗試預測()時,找不到object 'xyf.wines'
3.我將 xyf() 更改為 xyf.wines <- xyf(Xtraining, vintages[training], grid = somgrid(5, 5, "hexagonal")) 它起作用了,我得到了 xyf.wines
4.但是當我試圖預測時,我又失敗了。 Rstudio said Error in FUN(X[[i]], ...): Data type not allowed: should be a matrix or a factor 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)
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"))
Xtraining_data <- list(measurements = Xtraining,
vintages = vintages[training])
Xtest_data <- list(measurements = Xtest,
vintages = vintages[-training])
xyf.wines <- xyf(Xtraining,vintages[training],
grid = somgrid(5, 5, "hexagonal"))
xyf.prediction <- predict(xyf.wines, newdata=Xtest_data)
table(vintages[-training], xyf.prediction$predictions[[2]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.