<jupyter_start><jupyter_text>Market segmentation example In this notebook we explore a bit more sophisticated example of clustering  Import the relevant libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# Set the styles to Seaborn
sns.set()
# Import the KMeans module so we can perform k-means clustering with sklearn
from sklearn.cluster import KMeans
#import preprocessing so we can stadradize
from sklearn import preprocessing<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code># Load the data
data = pd.read_csv ('3.12. Example.csv')
# Check what's inside
data<jupyter_output><empty_output><jupyter_text>Plot the data Create a preliminary plot to see if you can spot something<jupyter_code># We are creating a scatter plot of the two variables
plt.scatter(data['Satisfaction'],data['Loyalty'])
# Name your axes 
plt.xlabel('Satisfaction')
plt.ylabel('Loyalty')<jupyter_output><empty_output><jupyter_text>Select the features<jupyter_code># Select both features by creating a copy of the data variable
x = data.copy()<jupyter_output><empty_output><jupyter_text>Clustering<jupyter_code># Create an object (which we would call kmeans)
# The number in the brackets is K, or the number of clusters we are aiming for
kmeans = KMeans(4)
# Fit the data
kmeans.fit(x)<jupyter_output><empty_output><jupyter_text>Clustering results<jupyter_code># Create a copy of the input data
clusters = x.copy()
# Take note of the predicted clusters 
clusters['cluster_pred']=kmeans.fit_predict(x)
# Plot the data using the longitude and the latitude
# c (color) is an argument which could be coded with a variable 
# The variable in this case has values 0,1, indicating to plt.scatter, that there are two colors (0,1)
# All points in cluster 0 will be the same colour, all points in cluster 1 - another one, etc.
# cmap is the color map. Rainbow is a nice one, but you can check others here: https://matplotlib.org/users/colormaps.html
plt.scatter(clusters['Satisfaction'],clusters['Loyalty'],c=clusters['cluster_pred'],cmap='rainbow')
plt.xlabel('Satisfaction')
plt.ylabel('Loyalty')<jupyter_output><empty_output><jupyter_text>Standardize the variables Let's standardize and check the new result<jupyter_code># Scale the inputs
# preprocessing.scale scales each variable (column in x) with respect to itself
# The new result is an array
x_scaled = preprocessing.scale(x)
x_scaled<jupyter_output><empty_output><jupyter_text>Take advantage of the Elbow method<jupyter_code># Createa an empty list
wcss =[]

# Create all possible cluster solutions with a loop
# We have chosen to get solutions from 1 to 9 clusters; you can ammend that if you wish
for i in range(1,10):
    # Clsuter solution with i clusters
    kmeans = KMeans(i)
    # Fit the STANDARDIZED data
    kmeans.fit(x_scaled)
    # Append the WCSS for the iteration
    wcss.append(kmeans.inertia_)
    
# Check the result
wcss
# Plot the number of clusters vs WCSS
plt.plot(range(1,10),wcss)
# Name your axes
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')<jupyter_output><empty_output><jupyter_text>Explore clustering solutions and select the number of clusters<jupyter_code># Fiddle with K (the number of clusters)
kmeans_new = KMeans(4)
# Fit the data
kmeans_new.fit(x_scaled)
# Create a new data frame with the predicted clusters
clusters_new = x.copy()
clusters_new['cluster_pred'] = kmeans_new.fit_predict(x_scaled)
# Check if everything seems right
clusters_new
# Plot
plt.scatter(clusters_new['Satisfaction'],clusters_new['Loyalty'],c=clusters_new['cluster_pred'],cmap='rainbow')
plt.xlabel('Satisfaction')
plt.ylabel('Loyalty')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08709764674060048, 'fraction_numerical': 0.005139302136867731, 'mean_word_length': 6.208576998050682, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '207709', 'license': 'MIT', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1178, 'n_words': 400, 'path': 'Part_5_Advanced_Statistical_Methods_(Machine_Learning)/Clustering/Market Segmentation/Market segmentation example_with_comments.ipynb', 'repo_name': 'cyork95/The-Data-Science-Course-2020', 'size': '3697'}