<jupyter_start><jupyter_text>${\textbf{Exploratory Data Analysis}}$<jupyter_code>df.head()
df.describe()<jupyter_output><empty_output><jupyter_text>${\textbf{Correlation Estimate}}$<jupyter_code>sns.heatmap(df.corr(), annot= True, cmap = 'coolwarm')
df.skew()<jupyter_output><empty_output><jupyter_text>${\textbf{Transforming}}$<jupyter_code>df['Log_Income'] = np.log(df['Income'])
df = df.drop(['Income'], axis = 1)<jupyter_output><empty_output><jupyter_text>${\textbf{Standardization}}$<jupyter_code>scaler = StandardScaler()
segmentation_std = scaler.fit_transform(df)<jupyter_output><empty_output><jupyter_text>${\textbf{K-means Clustering}}$<jupyter_code>wcss = []
for i in range(1,11):
    kmeans = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
    kmeans.fit(segmentation_std)
    wcss.append(kmeans.inertia_)
plt.figure(figsize = (10,8))
plt.plot(range(1, 11), wcss, marker = 'o', linestyle = '--')
plt.xlabel('Number of Clusters')
plt.ylabel('WCSS')
plt.title('K-means Clustering')
plt.show()
kmeans = KMeans(n_clusters = 4, init = 'k-means++', random_state = 42)
kmeans.fit(segmentation_std)<jupyter_output><empty_output><jupyter_text>${\textbf{Results}}$<jupyter_code>df_segm_kmeans = df.copy()
df_segm_kmeans['Segment K-means'] = kmeans.labels_
df_segm_analysis = df_segm_kmeans.groupby(['Segment K-means']).mean()
df_segm_analysis
df_segm_analysis['N Obs'] = df_segm_kmeans[['Segment K-means','Sex']].groupby(['Segment K-means']).count()
df_segm_analysis
df_segm_analysis.rename({0:'cluster 1',
                         1:'cluster 2',
                         2:'cluster 3',
                         3:'cluster 4'})
df_segm_kmeans['Labels'] = df_segm_kmeans['Segment K-means'].map({0:'cluster 1', 
                                                                  1:'cluster 2',
                                                                  2:'cluster 3', 
                                                                  3:'cluster 4'})
x_axis = df_segm_kmeans['Age']
y_axis = df_segm_kmeans['Log_Income']
plt.figure(figsize = (10, 8))
sns.scatterplot(x_axis, y_axis, hue = df_segm_kmeans['Labels'], palette = ['g', 'r', 'c', 'm'])
plt.title('Segmentation K-means')
plt.show()
pca = PCA()
pca.fit(segmentation_std)
pca.explained_variance_ratio_ # summing all everything will get one
#first component explained around 35 % of the data.

plt.figure(figsize = (12,9))
plt.plot(range(1,8),pca.explained_variance_ratio_.cumsum(),marker = 'o')
plt.title('Explained variance by component')
plt.xlabel('Number of component')
plt.ylabel('Cumulative Explained Variance')
pca = PCA(n_components = 3) # extract the 3 most important component
pca.fit(segmentation_std)
scores_pca = pca.transform(segmentation_std)
scores_pca
pca.components_
df_pca = pd.DataFrame(data = pca.components_,
                           columns = df.columns.values,
                           index = ['Component 1', 'Component 2', 'Component 3'])
df_pca
sns.heatmap(df_pca,
            cmap = 'RdBu',
            annot = True)
plt.yticks([0, 1, 2], 
           ['Component 1', 'Component 2', 'Component 3'])
scores_pca = pca.transform(segmentation_std)
wcss = []
for i in range(1,11):
    kmeans_pca = KMeans(n_clusters = i, init = 'k-means++', random_state = 42)
    kmeans_pca.fit(scores_pca)
    wcss.append(kmeans_pca.inertia_)
plt.figure(figsize = (10,8))
plt.plot(range(1, 11), wcss, marker = 'o', linestyle = '--')
plt.xlabel('Number of Clusters')
plt.ylabel('WCSS')
plt.title('K-means with PCA Clustering')
plt.show()
kmeans_pca = KMeans(n_clusters = 4, init = 'k-means++', random_state = 42)
kmeans_pca.fit(scores_pca)
# We create a new data frame with the original features and add the PCA scores and assigned clusters.
df_segm_pca_kmeans = pd.concat([df.reset_index(drop = True), pd.DataFrame(scores_pca)], axis = 1)
df_segm_pca_kmeans.columns.values[-3: ] = ['Component 1', 'Component 2', 'Component 3']
# The last column we add contains the pca k-means clustering labels.
df_segm_pca_kmeans['Segment K-means PCA'] = kmeans_pca.labels_
df_segm_pca_kmeans
df_segm_pca_kmeans_freq = df_segm_pca_kmeans.groupby(['Segment K-means PCA']).mean()
df_segm_pca_kmeans_freq
# Calculate the size of each cluster and its proportion to the entire data set.
df_segm_pca_kmeans_freq['N Obs'] = df_segm_pca_kmeans[['Segment K-means PCA','Sex']].groupby(['Segment K-means PCA']).count()
df_segm_pca_kmeans_freq = df_segm_pca_kmeans_freq.rename({0:'cluster 1', 
                                                          1:'cluster 2',
                                                          2:'cluster 3', 
                                                          3:'cluster 4'})

df_segm_pca_kmeans_freq
df_segm_pca_kmeans['Legend'] = df_segm_pca_kmeans['Segment K-means PCA'].map({0:'Cluster 1', 
                                                          1:'Cluster 2',
                                                          2:'Cluster 3', 
                                                          3:'Cluster 4'})
# Plot data by PCA components. The Y axis is the first component, X axis is the second.
x_axis = df_segm_pca_kmeans['Component 1']
y_axis = df_segm_pca_kmeans['Component 2']
plt.figure(figsize = (10, 8))
sns.scatterplot(x_axis, y_axis, hue = df_segm_pca_kmeans['Legend'], palette = ['g', 'r', 'c', 'm'])
plt.title('Clusters by PCA Components')
plt.show()
x_axis = df_segm_pca_kmeans['Component 1']
y_axis = df_segm_pca_kmeans['Component 2']
plt.figure(figsize = (10, 8))
sns.scatterplot(x_axis, y_axis, hue = df_segm_pca_kmeans['Legend'], palette = ['g', 'r', 'c', 'm'])
plt.title('Clusters by PCA Components')
plt.show()
x_axis = df_segm_pca_kmeans['Component 2']
y_axis = df_segm_pca_kmeans['Component 3']
plt.figure(figsize = (10, 8))
sns.scatterplot(x_axis, y_axis, hue = df_segm_pca_kmeans['Legend'], palette = ['g', 'r', 'c', 'm'])
plt.title('Clusters by PCA Components')
plt.show()<jupyter_output>C:\Users\<NAME>\anaconda3\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(
{'fraction_non_alphanumeric': 0.14294861533536096, 'fraction_numerical': 0.017288298383223946, 'mean_word_length': 3.4533143264433357, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '621108', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2133, 'n_words': 437, 'path': 'K mean clustering + PCA/Customer Analytics Segmentation.ipynb', 'repo_name': 'CheeKuang/CheeKuang.github.io', 'size': '6256'}