<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
dataframe = pd.read_csv('https://raw.githubusercontent.com/alura-cursos/alura-clustering-validation/base-de-dados/CC%20GENERAL.csv')
dataframe.drop(columns=['CUST_ID', 'TENURE'], inplace=True)
dataframe.fillna(dataframe.median(), inplace=True)
dataframe.head()
from sklearn.preprocessing import Normalizer
values = Normalizer().fit_transform(dataframe.values)
values
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=5, n_init=10, max_iter=300)
y_pred = kmeans.fit_predict(values)
y_pred
from sklearn import metrics
labels = kmeans.labels_
silhouette = metrics.silhouette_score(values, labels, metric='euclidean')
silhouette
dbs = metrics.davies_bouldin_score(values, labels)
dbs
calinski = metrics.calinski_harabasz_score(values, labels)
calinski
def clustering_algorithm(n_clusters, dataset):
    kmeans = KMeans(n_clusters=n_clusters, n_init=10, max_iter=300)
    labels = kmeans.fit_predict(dataset)
    s = metrics.silhouette_score(dataset, labels, metric='euclidean')
    dbs = metrics.davies_bouldin_score(dataset, labels)
    calinski = metrics.calinski_harabasz_score(dataset, labels)
    return s, dbs, calinski
s1, dbs1, calinski1 = clustering_algorithm(3, values)
print(s1, dbs1, calinski1)
s2, dbs2, calinski2 = clustering_algorithm(5, values)
print(s2, dbs2, calinski2)
s3, dbs3, calinski3 = clustering_algorithm(50, values)
print(s3, dbs3, calinski3)
valores = np.array([clustering_algorithm(i, values) for i in range(2, 15)])
plt.figure(figsize=(15,15))
plt.subplot(3, 1, 1)
plt.plot(valores[:,0], label='silhouette_score')
plt.legend()
plt.subplot(3, 1, 2)
plt.plot(valores[:,1], label='davies_bouldin_score')
plt.legend()
plt.subplot(3, 1, 3)
plt.plot(valores[:,2], label='calinski_harabasz_score')
plt.xlabel('n_clusters')

plt.legend();
random_data = np.random.rand(8950,16)
s, dbs, calinski = clustering_algorithm(5, random_data)
print(s, dbs, calinski)
print(s2, dbs2, calinski2)
set1, set2, set3 = np.array_split(values, 3)
s1, dbs1, calinski1 = clustering_algorithm(5, set1)
print(s1, dbs1, calinski1)
s2, dbs2, calinski2 = clustering_algorithm(5, set2)
print(s2, dbs2, calinski2)
s3, dbs3, calinski3 = clustering_algorithm(5, set3)
print(s3, dbs3, calinski3)
plt.figure(figsize=(15,10))
plt.scatter(dataframe.PURCHASES, dataframe.PAYMENTS, c=labels, s=5, cmap='rainbow')
plt.xlabel('Valor total pago')
plt.ylabel('Valor total gasto');
dataframe['cluster'] = labels
dataframe.groupby('cluster').describe().T
centroids = kmeans.cluster_centers_
centroids
max = len(centroids[0])
print('Valores da variância:\n')
for i in range(max):
    print(f'{dataframe.columns.values[i]:32} {centroids[:,i].var():.4f}', )
max = len(centroids[0])
print('Valores da variância acima de 0.009:\n')
for i in range(max):
    if centroids[:,i].var() > 0.009:
        print(f'{dataframe.columns.values[i]:32} {centroids[:,i].var():.4f}', )
description = dataframe.groupby('cluster')[['BALANCE', 'PURCHASES', 'CASH_ADVANCE','CREDIT_LIMIT', 'PAYMENTS']]
n_clients = description.size()
description = description.mean()
description['n_clients'] = n_clients
description
description = dataframe.groupby('cluster')[['BALANCE', 'PURCHASES', 'CASH_ADVANCE','CREDIT_LIMIT', 'PAYMENTS', 'PRC_FULL_PAYMENT']]
n_clients = description.size()
description = description.mean()
description['n_clients'] = n_clients
description<jupyter_output><empty_output><jupyter_text>| cluster | BALANCE	                  | PURCHASES	                                | CASH_ADVANCE                 | CREDIT_LIMIT	           | PAYMENTS	                | Avaliação                                                                    ||:-------:|:-------------------------:|:-----------------------------------------:|:----------------------------:|:-----------------------:|:------------------------:|:----------------------------------------------------------------------------:|| 0       |                           | Menor valor em compras                    | Valor considerável em saques |                         |                          | compram pouco / bom volume de saque / não são bons pagadores / maior cluster || 1       | menores valores em gastos |                                           | Menor valor em saques        | maior limite de crédito | menores pagamentos       | gastam pouco com maior limite de crédito / bons pagadores                    || 2       | maiores valores em gastos |                                           |                              | menor limite de crédito |                          | muito gasto com menor limite de crédito / piores pagadores                   || 3       |                           |                                           | Maior valor em saques        |                         | maiores pagamentos       | foco no saque / maiores pagamentos / menor cluster / melhores pagadores                          || 4       |                           | Maior valor em compras / grande diferença |                              |                         | pagamentos consideráveis | foco em compras / bons pagadores                                         |<jupyter_code>dataframe.columns<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15616748396831384, 'fraction_numerical': 0.022255752546208978, 'mean_word_length': 2.5049570389953733, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '192454', 'license': 'MIT', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1667, 'n_words': 350, 'path': 'Clustering_extraindo_padrões_de_dados.ipynb', 'repo_name': 'Daniel-ASG/Aulas_de_cursos', 'size': '5302'}