聚类导致非常集中的聚类

时间:2018-05-25 15:35:09

标签: r cluster-analysis distance correlation

要了解我的问题,您需要整个数据集:https://pastebin.com/82paf0G8

预处理:我有一个订单列表和696个唯一商品编号,并希望根据每对商品的订购频率进行聚类。我计算了每对项目,同一顺序内出现的频率数。即,两个项目之间的最高出现次数是489。然后我通过以下方式“计算”相似性/相关性:频率/“所有对的最大频率”(489)。现在我有我上传的数据集。

相似性/相关性:我不知道在这种情况下我的相似性方法是否最佳。我也尝试了一种名为“Jaccard系数/指数”的东西,但得到了几乎相同的结果。

数据集:数据集包含材料编号V1和V2。 N是0-1之间两个材料数之间的相关性。

在另一个人的帮助下,我设法创建了一个距离矩阵并使用PAM聚类。

为什么PAM聚类?数据科学家建议:你有超过95%的没有信息的配对,这使得所有这些材料处于相同的距离而且一个集群非常分散。使用PAM算法可以解决这个问题,但是你仍然会有一个非常集中的群体。另一种解决方案是增加除1之外的距离的权重。

问题1:矩阵仅为567x567。我认为对于聚类,我需要696x696全矩阵,即使它们中的很多都是零。但我不确定。

问题2:群集效果不佳。我得到非常集中的集群。许多项目都聚集在第一个集群中。此外,根据您验证PAM群集的方式,我的群集结果很差。是否由于相似性分析?我还应该使用什么?是因为95%的数据是零?我应该将零更改为其他内容吗?

整个代码和结果:

#Suppose X is the dataset
df <- data.table(X)
ss <- dcast(rbind(df, df[, .(V1 = V2, V2 = V1, N)]), V1~V2, value.var = "N")[, -1]
ss <- ss/max(ss, na.rm = TRUE)
ss[is.na(ss)] <- 0
diag(ss) <- 1

现在使用PAM集群

dd2 <- as.dist(1 - sqrt(ss))
pam2 <- pam(dd2, 4)
summary(as.factor(pam2$clustering))

但我得到了非常集中的群集,如:

1   2   3   4 
382 100  23  62

2 个答案:

答案 0 :(得分:0)

我不确定你从哪里得到696号码。你rbind后,你有V1和V2 567项唯一的数值数据框中,然后执行dcast,与基体最终预期567 X 567聚类明智我看不出问题,你的集群。

dim(df) # [1] 7659    3

test <- rbind(df, df[, .(V1 = V2, V2 = V1, N)])
dim(test) # [1] 15318     3

length(unique(test$V1)) # 567
length(unique(test$V2)) # 567

test2 <- dcast(test, V1~V2, value.var = "N")[,-1]
dim(test2) # [1] 567 567

答案 1 :(得分:0)

@Mayo,忘记数据科学家对PAM所说的话。既然你提到这项工作是为了论文。那么从学术角度来看,你目前为什么需要PAM的理由并没有任何价值。从本质上讲,您需要证明或证明为什么PAM是您案例研究的必要条件。鉴于数据集V1, V2, N中(连续)变量的性质,我没有看到PAM适用于此的逻辑(就像我在评论中提到的那样,PAM最适合混合变量)。 继续,请参阅此post关于R中的相关性检测;

# Objective: Detect Highly Correlated variables, visualize them and remove them
data("mtcars")
my_data <- mtcars[, c(1,3,4,5,6,7)]
# print the first 6 rows
head(my_data, 6)
# compute correlation matrix using the cor()
res<- cor(my_data)
round(res, 2) # Unfortunately, the function cor() returns only the correlation coefficients between variables. 
# Visualize the correlation
# install.packages("corrplot")
library(corrplot)
corrplot(res, type = "upper", order = "hclust", 
         tl.col = "black", tl.srt = 45)
# Positive correlations are displayed in blue and negative correlations in red color. Color intensity and the size of the circle are proportional to the correlation coefficients. In the right side of the correlogram, the legend color shows the correlation coefficients and the corresponding colors.
# tl.col (for text label color) and tl.srt (for text label string rotation) are used to change text colors and rotations.

#Apply correlation filter at 0.80,
#install.packages("caret", dependencies = TRUE)
library(caret)
highlyCor <- colnames(my_data)[findCorrelation(res, cutoff = 0.80, verbose = TRUE)]
# show highly correlated variables
highlyCor
[1] "disp" "mpg" 

High Correlation

removeHighCor<- findCorrelation(res, cutoff = 0.80) # returns indices of highly correlated variables
# remove highly correlated variables from the dataset
my_data<- my_data[,-removeHighCor]
[1] 32  4

希望这会有所帮助。