import pandas as pd
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn import metrics
from sklearn import preprocessing

# reading
dataset = pd.read_csv('CC.csv')
print(dataset)

# fill null values
dataset.fillna(dataset.mean(), inplace=True)
nulls = dataset.isnull().sum()
print(nulls)
print(dataset)

x = dataset.iloc[:, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]]
y = dataset.iloc[:, -1]
# Elbow
wcss = []
for i in range(1, 10):
     kmeans = KMeans(n_clusters=i, max_iter=300, random_state=0)
     kmeans.fit(x)
wcss.append(kmeans.inertia_)


# Kmeans
nclusters = 3
km = KMeans(n_clusters=nclusters)
km.fit(x)
# predict the cluster for each data point
y_cluster_kmeans = km.predict(x)
score = metrics.silhouette_score(x, y_cluster_kmeans)
print("Score: ", score)

# Processing
scaler = preprocessing.StandardScaler()
scaler.fit(x)
X_scaled_array = scaler.transform(x)
X_scaled = pd.DataFrame(X_scaled_array, columns=x.columns)

# Kmeans again
nclusters = 3
km = KMeans(n_clusters=nclusters)
km.fit(X_scaled)
# predict the cluster for each data point
y_cluster_kmeans = km.predict(X_scaled)
score = metrics.silhouette_score(X_scaled, y_cluster_kmeans)
print("Score After Scaling: ", score)

# PCA
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
# Fit on training set only.
scaler.fit(x)
x_scaler = scaler.transform(x)
pca = PCA(2)
x_pca = pca.fit_transform(x_scaler)
df2 = pd.DataFrame(data=x_pca)
finaldf = pd.concat([df2, dataset[['TENURE']]], axis=1)

# Kmeans again
nclusters = 3
km = KMeans(n_clusters=nclusters)
km.fit(x_scaler)
# predict the cluster for each data point
y_cluster_kmeans = km.predict(x_scaler)
score = metrics.silhouette_score(x_scaler, y_cluster_kmeans)
print("Score After PCA: ", score)


