【问题标题】:Unsupervised Classification: Assign classes to to data [closed]无监督分类:将类分配给数据[关闭]
【发布时间】:2018-02-25 16:18:26
【问题描述】:

我有一组钻孔数据,每 2 米包含有关不同地质力学特性的信息。我正在尝试创建地质力学域,并将每个点分配给不同的域。

我正在尝试使用随机森林分类,但不确定如何将邻近矩阵(或 randomForest 函数的任何结果)与标签相关联。

到目前为止,我的简陋代码如下:

dh <- read.csv("gt_1_classification.csv", header = T)

#replace all N/A with 0
dh[is.na(dh)] <- 0
library(randomForest)
dh_rf <- randomForest(dh, importance=TRUE, proximity=FALSE, ntree=500, type=unsupervised, forest=NULL)

我希望分类器自行决定域。

任何帮助都会很棒!

【问题讨论】:

  • 如果有足够的数据来测试代码和演示结果,这可能是一个有用的问题。因为它似乎太模糊了,无法进一步努力。
  • @42- 例如,可以使用 Iris 数据集完成相同的挑战。如果您删除物种列并尝试编写一个分类器,该分类器将查看数据并在此处为物种分配其自身 - 同样的问题。
  • 更合适的响应是提供加载 iris 并执行您想象会构建对象以供进一步分析的操作的代码。
  • @CHopp 像这样想 - 一个人提出问题/赞成做一次创建可重现示例的工作是否更合理,或者对于可能尝试的 N 个人中的每个人来说更合理为了帮助他解决他的问题,做一个可重复的例子的工作?这意味着工作要完成 N 次,其中 N 可能是几到几十甚至几百。哲学和经济学的答案显然是请求者应该做 1 次工作。更不用说 StackOverflow 上的 MCVE 规则了...stackoverflow.com/help/mcve
  • 无论如何,随机森林是有监督的。因此,您必须做的是使用聚类分析、启发式或类似方法创建自己的标签。然后在创建的标签上训练 RF。另见stats.stackexchange.com/questions/72370/…

标签: r machine-learning random-forest unsupervised-learning multilabel-classification


【解决方案1】:

Hack-R 是正确的——首先有必要使用一些聚类(无监督学习)方法来探索数据。我提供了一些示例代码,使用 R 内置的 mtcars 数据作为演示:

# Info on the data
?mtcars
head(mtcars)
pairs(mtcars)    # Matrix plot

# Calculate the distance between each row (car with it's variables)
# by default, Euclidean distance = sqrt(sum((x_i - y_i)^2)
?dist
d <- dist(mtcars)
d # Potentially huge matrix

# Use the distance matrix for clustering
# First we'll try hierarchical clustering
?hclust
c <- hclust(d)
c

# Plot dendrogram of clusters
plot(c)

# We might want to try 3 clusters
# need to specify either k = # of groups
groups3 <- cutree(c, k = 3) # "g3" = "groups 3"
# cutree(hcmt, h = 230) will give same result
groups3
# Or we could do several groups at once
groupsmultiple <- cutree(c, k = 2:5)
head(groupsmultiple)

# Draw boxes around clusters
rect.hclust(c, k = 2, border = "gray")
rect.hclust(c, k = 3, border = "blue")
rect.hclust(c, k = 4, border = "green4")
rect.hclust(c, k = 5, border = "darkred")

# Alternatively we can try K-means clustering
# k-means clustering
?kmeans
km <- kmeans(mtcars, 5)
km

# Graph based on k-means
install.packages("cluster")
require(cluster)
clusplot(mtcars, # data frame
     km$cluster, # cluster data
     color = TRUE, # color
     lines = 3, # Lines connecting centroids
     labels = 2) # Labels clusters and cases

在您自己的数据上运行后,请考虑哪种集群定义能够捕捉您感兴趣的相似度。然后,您可以为每个集群创建一个具有“级别”的新变量,然后为此创建一个监督模型。

这是一个使用相同 mtcars 数据的决策树示例。请注意,这里我使用 mpg 作为响应——您可能希望使用基于集群的新变量。

install.packages("rpart")
library(rpart)
?rpart
# grow tree 
tree_mtcars <- rpart(mpg ~ ., method = "anova", data = mtcars)
tree_mtcars <- rpart(mpg ~ ., data = mtcars)

tree_mtcars

summary(tree_mtcars) # detailed summary of splits

# Get R-squared
rsq.rpart(tree_mtcars)
?rsq.rpart

# plot tree 
plot(tree_mtcars, uniform = TRUE, main = "Regression Tree for mpg ")
text(tree_mtcars, use.n = TRUE, all = TRUE, cex = .8)
text(tree_mtcars, use.n = TRUE, all = TRUE, cex = .8)

请注意,虽然信息量很大,但基本决策树通常不适用于预测。如果需要预测,还应探索其他模型。

【讨论】:

  • 非常好的答案,非常感谢!!这非常有用且内容丰富 - 通过您的示例学到了很多,并且能够分配课程
猜你喜欢
  • 2017-05-01
  • 2016-05-02
  • 2012-05-10
  • 2019-05-09
  • 2016-08-20
  • 2019-06-04
  • 1970-01-01
  • 2015-05-12
  • 2010-12-17
相关资源
最近更新 更多