#================================================================================================================# #========== Jacek Wallusch ==========# #========== AQM: Clustering Procedures ==========# #========== Lecture 8: Mean-Shift ==========# #================================================================================================================# # Package: library(MeanShift) #----------------------------------------------------------------------------------------------------------------- # Data # Quants and Prices: use the data generated for the k-means algorith MShift <- data.frame(c(cusA,cusB,cusC), c(totA,totB,totC), c(PriA, PriB, PriC)) colnames(MShift) <- c("Cust", "Quants", "Prices") #----------------------------------------------------------------------------------------------------------------- # Plot the data plot_ly(x = PriA, y = totA, type = "scatter", mode = "markers", name = "A") %>% add_trace(x = PriB, y = totB, type = "scatter", mode = "markers", name = "B") %>% add_trace(x = PriC, y = totC, type = "scatter", mode = "markers", name = "C") %>% layout(xaxis = list(title = "Prices"), yaxis = list(title = "Quants")) #----------------------------------------------------------------------------------------------------------------- # Define number of cores used for performing the procedure options(mc.cores=3) #----------------------------------------------------------------------------------------------------------------- # Mean-Shift MScluPQ <- msClustering(t(matPQ), h = 0.75, # bandwith parameter: large h -> few and large clusters kernel = "gaussianKernel", # select kernel: epanechnikovKernel, cubicKernel, exponentialKernel or Gaussian tol.stop = 0.00001, # tolerance parameter tol.epsilon = 0.001, # tolerance parameter multi.core = TRUE # algoritm paralelised if TRUE, one core used if FALSE ) print(MScluPQ) #----------------------------------------------------------------------------------------------------------------- # Blurring Mean-Shift BMScuPQ <- bmsClustering(t(matPQ), h = 0.75, kernel = "gaussianKernel", tol.stop = 0.0001, max.iter = 100, # max number of iterations tol.epsilon = 0.01 ) print(BMScuPQ) #----------------------------------------------------------------------------------------------------------------- # Cluster Graph obj_clu <- MScluPQ$labels #BMScuPQ dfBMS <- data.frame(MShift, obj_clu) colnames(dfBMS) <- c("Cust", "Quants", "Prices", "Cluster") colclus <- ifelse(dfBMS$Cluster == 1, "red", ifelse(dfBMS$Cluster == 2, "green", ifelse(dfBMS$Cluster == 3, "grey", ifelse(dfBMS$Cluster == 4, "black", ifelse(dfBMS$Cluster == 5, "brown", "yellow"))))) plot_ly(x = dfBMS$Prices, y = dfBMS$Quants, type = "scatter", mode = "markers", marker = list(color = colclus, line = list(width = 0.5, color = "black")), hoverinfo = "text", text = paste("Customer: ", dfBMS$Cust, "
", "Price: \u00A3", round(dfBMS$Prices,2), "
", "Quants: ", dfBMS$Quants, "
", "Cluster: ", dfBMS$Cluster))