The goal of KMediansR is to group a set of objects in such a way that objects in the same group (called a cluster) are more similar (in some sense or another) to each other than to those in other groups (clusters). In k-medians clustering, we partition
n observations into
k clusters. It calculates the median for each cluster to determine its centroid. The
kmedians package performs k-medians clustering on the dataset entered by the users and returns clustered data. This can prove to be an extremely beneficial package as k-medians is more robust to outliers than the arithmetic mean(k-means).
The three main functions in the package are :
The function helps to calculate the Manhattan distance between each pair of the two collection inputs. This function takes as input
mxn array of
m original observations in an
poriginal observations in an
k-dimensional space It returns a
mxpdistance matrix. For each
j, the mteric
distance(u=X[i], v=Y[j])is computed and stored in the
A quick implementation of k-medians. It takes as input
A 2D array of data
kmediansfunction on the input data. It returns a dataframe that contains information about the model run such as the number of clusters, the number of points in each cluster, the inter and intra cluster distance
Simple example demonstrating the functionality of this package:
# load package library(KMediansR) # toy data with two clusters toy_data <- matrix( c(1,1,1,2,2,1,100,100,101,100,100,101), nrow = 6, ncol = 2, byrow = TRUE) # initialize the cluster centers m <- matrix( c(1,1,100,100), nrow = 2, ncol = 2, byrow = TRUE) # calculate Manhanttan distance between the medians and data points manhanttan_distance <- distance(X = toy_data, medians = m) [,1] [,2] [1,] 0 198 [2,] 1 197 [3,] 1 197 [4,] 198 0 [5,] 199 1 [6,] 199 1 # cluster the data points clustered <- kmedians(X = toy_data, num_clusters = 2) [] [,1] [,2] [1,] 1 1 [2,] 100 100 []  1 1 1 2 2 2 # generate summary results report <- summary(X = toy_data, medians = clustered[], labels = clustered[]) Cluster.Label Median.Coordinates Number.of.Points.in.Cluster Average.Distance Minimum.Distance Maximum.Distance 1 1 1,1 3 0.6666667 0 1 2 2 100,100 3 0.6666667 0 1
kGmedian : This is a fast k-medians clustering based on recursive averaged stochastic gradient algorithms. The advantage of the
kGmedian algorithm compared to
kmeans strategy is that it deals with sum of norms instead of sum of squared norms, ensuring a more robust behaviour against outlying values.
The above ideas are presented as a part of the initial proposal. However, they could be subject to change in the following milestones based on the project timeline or technical complexity.
R Version 3.5.2
You can install the released version of KMediansR from CRAN with:
library(dplyr) library(magrittr) devtools::install_github("UBC-MDS/KMediansR")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.