text_plain_output_1.png
from fcmeans import FCM

from fcmeans import FCM

from sklearn.metrics import silhouette_score

import itertools

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import pandas as pd

import seaborn as sns

df = pd.read_csv('/kaggle/input/customer-segmentation-tutorial-in-python/Mall_Customers.csv')

df.drop('Gender', inplace=True, axis=1)

df.sample(3)

df.isnull().sum()

X_numerics = df[['Age', 'Annual Income (k$)', 'Spending Score (1-100)']]

ax = sns.pairplot(X_numerics[X_numerics.columns])

data = X_numerics.values

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import silhouette_score
from fcmeans import FCM
min_clusters = 2
max_clusters = 10
silhouette_scores = []
cluster_numbers = []
for n_clusters in range(min_clusters, max_clusters + 1):
    fcm = FCM(n_clusters=n_clusters)
    fcm.fit(data)
    labels = fcm.predict(data)
    silhouette_avg = silhouette_score(data, labels)
    silhouette_scores.append(silhouette_avg)
    cluster_numbers.append(n_clusters)

fcmean = FCM(n_clusters=5)
fcmean.fit(data)
cnt = fcmean.centers
pred = fcmean.predict(data)

import itertools
variable_combinations = list(itertools.combinations(range(data.shape[1]), 2))
colors = ['r', 'b', 'g', 'purple', 'orange']
for i, (x_index, y_index) in enumerate(variable_combinations):
    plt.scatter(data[pred == 0, x_index], data[pred == 0, y_index], s=10, c=colors[0])
    plt.scatter(data[pred == 1, x_index], data[pred == 1, y_index], s=10, c=colors[1])
    plt.scatter(data[pred == 2, x_index], data[pred == 2, y_index], s=10, c=colors[2])
    plt.scatter(data[pred == 3, x_index], data[pred == 3, y_index], s=10, c=colors[3])
    plt.scatter(data[pred == 4, x_index], data[pred == 4, y_index], s=10, c=colors[4])
    plt.scatter(cnt[:, x_index], cnt[:, y_index], s=300, c='black', marker='+')
    plt.xlabel(X_numerics.columns[x_index])
    plt.ylabel(X_numerics.columns[y_index])
    plt.title('Customer Clustering based on ' + X_numerics.columns[x_index] + ' and ' + X_numerics.columns[y_index])
    plt.show()