text_html_output_2.png
from sklearn.cluster import KMeans

import pandas as pd

import plotly.express as px

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

df.isna().sum()

df_copy = df.copy()

df1 = df.drop(['CustomerID', 'Gender'], axis=1)
df1

fig = px.scatter(df, x="Annual Income (k$)", y="Spending Score (1-100)", color='Gender')
fig.show()

kmeans = KMeans(n_clusters=4, max_iter=1000)
kmeans.fit(df1)

kmeans.cluster_centers_

clusters = kmeans.predict(df1)
clusters[50:60]

fig = px.scatter(df1, x='Annual Income (k$)', y='Spending Score (1-100)', color=clusters)
fig.show()