<jupyter_start><jupyter_text>Aula 2<jupyter_code>import pandas as pd
path = 'https://raw.githubusercontent.com/HenriqueCCdA/FormacaoAluraML/main/Clustering/dados/CC_GENERAL.csv'

dataframe = pd.read_csv(path)

dataframe.head()
dataframe.drop(columns=["CUST_ID", "TENURE"], inplace=True)
dataframe.head()
missing = dataframe.isna().sum()
print(missing)
dataframe.fillna(dataframe.median(), inplace=True)
missing = dataframe.isna().sum()
print(missing)
from sklearn.preprocessing import Normalizer

values = Normalizer().fit_transform(dataframe.values)
print(values)
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters = 5, n_init=10, max_iter=300)
y_pred = kmeans.fit_predict(values)<jupyter_output><empty_output><jupyter_text>Aula 5<jupyter_code>from sklearn import metrics
labels = kmeans.labels_
silhouete = metrics.silhouette_score(values, labels, metric='euclidean')
print('silhouette:', silhouete)
dbs = metrics.davies_bouldin_score(values, labels)
print('davies_bouldin:', dbs)
calinski = metrics.calinski_harabasz_score(values, labels)
print('calinski_harabasz:', calinski)<jupyter_output>calinski_harabasz: 3431.782548172111<jupyter_text>Aula 6<jupyter_code>def clustering_algorithm(n_clusters, dataset):
    kmeans = KMeans(n_clusters=n_clusters, n_init=10, max_iter=100)
    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
for n in [2, 3, 5, 10, 20, 50, 100]:
    s, dbs, calinski = clustering_algorithm(n, values)
    print(f'{n:3} {s:.2f} {dbs:.2f} {calinski:.2f}')
dataframe.count()
import numpy as np
random_data = np.random.rand(8950, 16)
s, dbs, calinski = clustering_algorithm(n, values)
sa, dbsa, calinskia = clustering_algorithm(5, random_data)
print(f'Conjuto aleatorio     : {sa:.2f} {dbsa:.2f} {calinskia:.2f}')
print(f'Conjuto nao aleatorio : {s:.2f} {dbs:.2f} {calinski:.2f}')
set1, set2, set3 = np.array_split(values, 3)
print(clustering_algorithm(5, set1))
print(clustering_algorithm(5, set2))
print(clustering_algorithm(5, set3))<jupyter_output>(0.3689709458416189, 1.0581656052369075, 1204.0560966145404)
(0.3533093838590228, 1.142502270067299, 1194.9475421190975)
(0.36713104970461446, 1.0991014125845049, 1167.5075975296127)<jupyter_text>Aula 7<jupyter_code>import matplotlib.pyplot as plt

plt.scatter(dataframe['PURCHASES'], dataframe['PAYMENTS'], c=labels, s=5, cmap='rainbow')
plt.xlabel("Valor total pago")
plt.ylabel("Valor total gasto")
plt.show()
import seaborn as sns

dataframe["cluster"] = labels

sns.pairplot(dataframe[0:], hue="cluster")

plt.show()<jupyter_output><empty_output><jupyter_text>Aula 8<jupyter_code>dataframe.groupby('cluster').describe()
centroids = kmeans.cluster_centers_
print(centroids)
max = len(centroids[0])

for i in range(max):
  print(f'{dataframe.columns.values[i]:35}  {centroids[:, i].var():.6f}')

description = dataframe.groupby("cluster")[["BALANCE", "PURCHASES", "CASH_ADVANCE", "CREDIT_LIMIT", "PAYMENTS"]]
n_clients = description.size()
description = description.mean()
description['n_clients'] = n_clients
description<jupyter_output><empty_output><jupyter_text>Aula 9 CLUSTER 0: Pagam as vezesCLUSTER 1: Clientes com os maiores calores em gastos. Menor valores em compras. Piores pagadoresCLUSTER 2: Clientes com os menos valores em gastos.CLUSTER 3: Maior valor em compras com grande diferençaCLUSTER 4: Piores pagadores<jupyter_code>dataframe.groupby("cluster")["PRC_FULL_PAYMENT"].describe()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1364264015465341, 'fraction_numerical': 0.06517536592101629, 'mean_word_length': 8.009950248756219, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '302633', 'license': 'MIT', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1400, 'n_words': 243, 'path': 'Clustering/notebook/Cartao_credito.ipynb', 'repo_name': 'HenriqueCCdA/FormacaoAluraML', 'size': '3621'}