【发布时间】:2018-07-24 13:37:57
【问题描述】:
我有兴趣从无监督拟合 SOM 模型预测新数据集,但我不确定自己是否走在正确的道路上。感谢您的指导
目标: 使用先前在训练集上拟合的集群组对新数据集进行分类。
我不确定的:
一个。测试集中分配的集群组是否与训练集类似。即测试集中的第 1 组必须与训练集中的第 1 组具有相同的特征。
b. Iris 无监督拟合似乎不太合适。
library('kohonen')
set.seed(1)
idx_n <- sample(nrow(iris),120)
train <- iris[idx_n,]
row.names(train) <- NULL
test <- iris[-idx_n,]
row.names(test) <- NULL
#preprocess
train.sc <- scale(train[,-5])
#train model
som_grid <- somgrid(xdim = 5
,ydim=5
,topo="hexagonal"
,toroidal = F)
som.iris<- som(train.sc
,grid=som_grid
,rlen=200
,alpha=c(0.05,0.01)
,keep.data = TRUE )
set_cluster <- 3
## use hierarchical clustering to cluster the codebook vectors
som.iris.hc <- cutree(hclust(dist(som.iris$codes[[1]])), set_cluster)
# --------- Predict new dataset ----------
#scale test set acording to fitted model data
test.sc <- scale(test[,-5],
center = attr(som.iris$data[[1]], "scaled:center"),
scale = attr(som.iris$data[[1]], "scaled:scale"))
test.pred <- predict(som.iris,
newdata = test.sc)
set_cluster <- 3
## use hierarchical clustering to cluster the codebook vectors
som.iris.hc_test <- cutree(hclust(dist(test.pred$predictions[[1]])), set_cluster)
#attach cluster groups. Am I doing this right?
train_final <- cbind(train,cluster=som.iris.hc[som.iris$unit.classif])
test_final <- cbind(test,cluster=som.iris.hc_test)
#explore each clusters
by(train_final, train_final$cluster, summary)
by(test_final, test_final$cluster, summary)
#results - Not very Spectacular
table(train_final$Species,train_final$cluster)
【问题讨论】:
标签: r cluster-analysis prediction unsupervised-learning som