<jupyter_start><jupyter_text>(1) KMeans Implementation for Only Numeric Variables<jupyter_code># Now implement KMeans algorithm for different numbers of clusters and then plot the scree plot 
# Since we have more than 2 numeric variables, we will use Uniform Manifold Approximation and Projection for Dimension Reduction (UMAP)

inertia = [] # It is the sum of distances of each observation to its cluster center
distortion = [] # It is the average of distances of all observations to its respective cluster center

numiter = range(1,51)
# Iterate over the number of clusters
for i in numiter:
    KMeans_model = KMeans(n_clusters = i, init='k-means++').fit(Xnumeric)
    inertia.append(KMeans_model.inertia_)
    distortion.append(sum(np.min(distance.cdist(Xnumeric, KMeans_model.cluster_centers_, 'euclidean'), axis = 1))/len(Xnumeric))
    
# Now plot the inertia and distortions values
fig = plt.figure()
ax1 = plt.subplot(121)
ax1.plot(numiter, inertia, 'bx-')
ax1.set_title('Inertia', fontsize = 15)
ax1.set_xlabel('Number of clusters', fontsize = 10)
ax2 = plt.subplot(122)
ax2.plot(numiter, distortion, 'bx-')
ax2.set_title('Distortion', fontsize = 15)
ax2.set_xlabel('Number of clusters', fontsize = 10)
plt.suptitle('Elbow Method to Determine Optimal Number of Clusters', fontsize = 17, y = 1.05)
plt.show()
## Now conduct the clustering with only 4 clusters and then plot the cluters 

KMeans_model = KMeans(n_clusters = 10, init = 'k-means++').fit(Xnumeric)
KMeans_label = KMeans_model.labels_

# Now visualize clusters in 2 dimensions using PCA (Could also be done using UMAP)
pca = PCA(n_components = 2).fit_transform(Xnumeric)
pca = pd.DataFrame(pca, columns = ['Component_1','Component_2'])
pca['Cluster'] = KMeans_label

# Now plot using scatter plot
g = sns.scatterplot(data = pca, x = 'Component_1', y = 'Component_2', hue = 'Cluster', palette = 'deep')
g.set_title('Cluster Representation along principal components')<jupyter_output><empty_output><jupyter_text>(2) KMeans Implementation for Numeric Variables + One-Hot Encoded Categorical Variables<jupyter_code># Concatenate the standardized numeric variables with the standardied one-hot encoded categorical variables

Xnumeric_alt = pd.concat([pd.DataFrame(Xnumeric), pd.DataFrame(StandardScaler().fit_transform(pd.get_dummies(Xcategorical)))], axis = 1)

inertia = [] # It is the sum of distances of each observation to its cluster center
distortion = [] # It is the average of distances of all observations to its respective cluster center

numiter = range(1,51)
# Iterate over the number of clusters
for i in numiter:
    KMeans_model = KMeans(n_clusters = i, init='k-means++').fit(Xnumeric_alt)
    inertia.append(KMeans_model.inertia_)
    distortion.append(sum(np.min(distance.cdist(Xnumeric_alt, KMeans_model.cluster_centers_, 'euclidean'), axis = 1))/len(Xnumeric_alt))
    
# Now plot the inertia and distortions values
fig = plt.figure()
ax1 = plt.subplot(121)
ax1.plot(numiter, inertia, 'bx-')
ax1.set_title('Inertia', fontsize = 15)
ax1.set_xlabel('Number of clusters', fontsize = 10)
ax2 = plt.subplot(122)
ax2.plot(numiter, distortion, 'bx-')
ax2.set_title('Distortion', fontsize = 15)
ax2.set_xlabel('Number of clusters', fontsize = 10)
plt.suptitle('Elbow Method to Determine Optimal Number of Clusters', fontsize = 17, y = 1.05)
plt.show()
# Now show the cluster representation for acertain number of clusters again along two main principal components

KMeans_model = KMeans(n_clusters = 6, init = 'k-means++').fit(Xnumeric_alt)
KMeans_label = KMeans_model.labels_

# Now visualize clusters in 2 dimensions using PCA (Could also be done using UMAP)
pca = PCA(n_components = 2).fit_transform(Xnumeric_alt)
pca = pd.DataFrame(pca, columns = ['Component_1','Component_2'])
pca['Cluster'] = KMeans_label

# Now plot using scatter plot
g = sns.scatterplot(data = pca, x = 'Component_1', y = 'Component_2', hue = 'Cluster', palette = 'deep')
g.set_title('Cluster Representation along principal components')<jupyter_output><empty_output><jupyter_text>(3) K-Prototype Implementation for both Numeric and Categorical Data<jupyter_code>## First concatenate the two types of data and then find the index of the columns of the categorical variables

X_alt = pd.concat([pd.DataFrame(Xnumeric), pd.DataFrame(Xcategorical)], axis = 1)
catColumnsLoc = [X_alt.columns.get_loc(col) for col in list(X_alt.select_dtypes('category').columns)]

# Now convert the dataframe into a matrix
X_alt_matrix = X_alt.to_numpy()

# Now use the elbow method
cost = []
for cluster in range(1,21):
    kprototype = KPrototypes(n_jobs = -1, n_clusters = cluster, init = 'Huang', random_state = 0)
    kprototype.fit_predict(X_alt_matrix, categorical = catColumnsLoc)
    cost.append(kprototype.cost_)
    print('Cluster Initialization: {}'.format(cluster))

        
# Plotting the cost function with the number of clusters chosen 

costK = pd.DataFrame({'Cluster':range(1,21),'Cost':cost})
plotnine.options.figure_size = (8,4.8)
(
    ggplot(data = costK)+
    geom_line(aes(x = 'Cluster',
                  y = 'Cost'))+
    geom_point(aes(x = 'Cluster',
                   y = 'Cost'))+
    geom_label(aes(x = 'Cluster',
                   y = 'Cost',
                   label = 'Cluster'),
               size = 10,
               nudge_y = 1000) +
    labs(title = 'Optimal number of cluster with Elbow Method')+
    xlab('Number of Clusters k')+
    ylab('Cost')+
    theme_minimal()
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1141552511415525, 'fraction_numerical': 0.018447488584474887, 'mean_word_length': 4.881847475832438, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '126445', 'license': 'MIT', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1763, 'n_words': 519, 'path': 'notebooks/Employee_Churn_Predicttion_IBM_Clustering.ipynb', 'repo_name': 'SouravSinhas/Employee-Turnover-Prediction', 'size': '5475'}