Cluster analysis is used to find a group of observations that have similar observations. This model is concerned with finding group cases that are closer to each other and different from others and is used if one wants to know if there are groups of similar cases on some of the variables. In this example, we looked at how many clusters adequately explains considerable variability using K-Means.
In K-Means there are no real dependent variables. All variables are of equal interesting
Importing Python libraries
Underneath, key Python libraries were imported. These libraries gallantly helped us create arrays and profile tables, compute graphs and make accurate predictions.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set("talk","ticks",font_scale=1,font="sans-serif",color_codes=True)
from sklearn import metrics
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from mpl_toolkits.mplot3d import Axes3D
from pylab import rcParams
plt.rcParams["figure.figsize"] = [10,10]
import warnings
warnings.filterwarnings("ignore")
Loading data
df = pd.read_csv(r"C:\Users\Tshepo\Downloads\xclara.csv")
df.head()
We called the pd.read_csv() function to load obtained csv file into a pandas data frame. See underneath table to examine the data structure.
| V1 | V2 | |
|---|---|---|
| 0 | 2.072345 | -3.241693 |
| 1 | 17.936710 | 15.784810 |
| 2 | 1.083576 | 7.319176 |
| 3 | 11.120670 | 14.406780 |
| 4 | 23.711550 | 2.557729 |
Detect missing values
The visible presence of missing values result in measurement errors that negatively impact conclusions. To studiously avoid such as a dilemma, data was carefully examined to unanimously determine whether there were any missing values. See underneath figure for a visual representation of the possible presence of missing values.
sns.heatmap(df.isnull(),cmap="Blues")
plt.title("Detect Mising Values")
plt.show()

There were no missing values detected.
Descriptive analysis
fig, (ax1,ax2) = plt.subplots(1,2,figsize=(16,7))
sns.boxplot(df["V1"],ax=ax1)
ax1.set_title("V1 Box Plot")
sns.boxplot(df["V2"],ax=ax2)
ax2.set_title("V2 Box Plot")
plt.show()

There were no outliers detected on both box plots. V1’s box plot seem to follow a normal distribution and V2’s box plot is slightly skewed to the left. Let us now confirm that using a histogram.
fig, (ax1,ax2) = plt.subplots(1,2,figsize=(16,7))
sns.distplot(df["V1"],ax=ax1)
ax1.set_title("V1 Histogram")
ax1.set_ylabel("Related V1 Frequency")
sns.distplot(df["V2"],ax=ax2)
ax2.set_title("V2 Histogram")
ax2.set_ylabel("Related V2 Frequency")
plt.show()

V1’s distribution follows a normal distribution, however, that cannot be stated with certainty, as the distribution is not entirely a bell-shaped curve. V2’s distribution takes a binomial shape. There are no strict requirements of variables being normally distributed in cluster analysis.
Creating x and y array
X = df[["V1"]]
Y = df[["V2"]]
Above we created x and y array. Please note there is no independent variable in this scenario, all variables are of equal interest.
Determining number of clusters
Above we created x and y array. Please note there is no independent variable in this scenario, all variables were of equal interest.
Nc = range(1,20)
kmeans = [KMeans(n_clusters=i) for i in Nc]
kmeans
score = [kmeans[i].fit(Y).score(Y) for i in range(len(kmeans))]
score
fig, ax = plt.subplots(figsize=(10,10))
plt.plot(Nc,score)
plt.title("Elbow Curve")
plt.ylabel("Scores")
plt.xlabel("Number of clustering")
plt.show()

The curve sharply bends at 3. Consequently, we used 3 clusters train our model.
Principal Component Analysis
Principal Component Analysis (PCA) uses eigenvalues to correctly determine where considerable variation comes from. PCA looks a cumulative proportion. Underneath we used PCA to simplify the structure of the set of variables. Components will be calculated as linear combinations of the original values.
pca = PCA(n_components=1).fit(Y)
pca_d = pca.transform(X)
pca_c = pca.transform(Y)
K-Means
kmeans = KMeans(n_clusters=3)
kmeans_output = kmeans.fit(Y)
kmeans_output.labels_
array([2, 1, 1, …, 2, 2, 2])
np.unique(kmeans_output.labels_)
array([0, 1, 2])
fig = plt.figure(figsize=(10,10))
ax = Axes3D(fig)
ax.scatter(pca_c[:,0],pca_d[:,0],c=kmeans_output.labels_, s=3, cmap="viridis")
plt.title("KMeans clustering")
plt.xlabel("V1")
plt.ylabel("V2")
plt.show()

There are groups of similar cases with similar observations.