#pr7

import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans, AgglomerativeClustering
import matplotlib.pyplot as plt
import seaborn as sns

data = pd.read_csv("Mall_Customers.csv")
print(data.head())

data['Genre'] = data['Genre'].astype(str).str.strip().str.capitalize()
data['Genre'] = data['Genre'].map({'Male': 1, 'Female': 0})
print(data.isnull().sum())


data['Genre']

X = data[['Annual Income (k$)', 'Spending Score (1-100)']]
X

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

X_scaled

kmeans = KMeans(n_clusters=5, random_state=42)
labels_kmeans = kmeans.fit_predict(X_scaled)
data['KMeans_Cluster'] = labels_kmeans
data


agglo = AgglomerativeClustering(n_clusters=5)
labels_agglo = agglo.fit_predict(X_scaled)
data['Agglo_Cluster'] = labels_agglo
data

plt.figure(figsize=(18,5))

# KMeans
plt.subplot(1,2,1)
sns.scatterplot(x=X_scaled[:,0], y=X_scaled[:,1], hue=labels_kmeans, palette='Set1')
plt.title("KMeans Clustering")
plt.xlabel("Annual Income (scaled)")
plt.ylabel("Spending Score (scaled)")

# Agglomerative
plt.subplot(1,2,2)
sns.scatterplot(x=X_scaled[:,0], y=X_scaled[:,1], hue=labels_agglo, palette='Set2')
plt.title("Agglomerative Clustering")
plt.xlabel("Annual Income (scaled)")
plt.ylabel("Spending Score (scaled)")
     