<jupyter_start><jupyter_text>Market segmentation example  Import the relevant libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code>data = pd.read_csv ('3.12. Example.csv')
data<jupyter_output><empty_output><jupyter_text>Plot the data<jupyter_code>plt.scatter(data['Satisfaction'],data['Loyalty'])
plt.xlabel('Satisfaction')
plt.ylabel('Loyalty')<jupyter_output><empty_output><jupyter_text>Select the features<jupyter_code>x = data.copy()<jupyter_output><empty_output><jupyter_text>Clustering<jupyter_code>kmeans = KMeans(2)
kmeans.fit(x)<jupyter_output><empty_output><jupyter_text>Clustering results<jupyter_code>clusters = x.copy()
clusters['cluster_pred']=kmeans.fit_predict(x)
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<jupyter_code>from sklearn import preprocessing
x_scaled = preprocessing.scale(x)
x_scaled<jupyter_output><empty_output><jupyter_text>Take advantage of the Elbow method<jupyter_code>wcss =[]

for i in range(1,10):
    kmeans = KMeans(i)
    kmeans.fit(x_scaled)
    wcss.append(kmeans.inertia_)
    
wcss
plt.plot(range(1,10),wcss)
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1303763440860215, 'fraction_numerical': 0.006720430107526882, 'mean_word_length': 10.912, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '48407', 'license': 'Apache-2.0', 'n_tokens_mistral': 598, 'n_tokens_neox': 568, 'n_words': 75, 'path': '15 - Advanced Statistical Methods in Python/7_K-Means Clustering/11_Market Segmentation with Cluster Analysis (Part 1) (6:03)/Market segmentation example.ipynb', 'repo_name': 'olayinka04/365-data-science-courses', 'size': '1488'}