<jupyter_start><jupyter_text>Metodo del cluster para averigura el numero optimo de clusters<jupyter_code>from sklearn.cluster import KMeans
#with in cluster square sum
wcss = []
for i in range(1,11):
    kmeans = KMeans(n_clusters = i, 
                    init = 'k-means++', 
                    max_iter = 300, 
                    n_init = 10,
                    random_state = 0)
    kmeans.fit(X)
    wcss.append(kmeans.inertia_)

plt.plot(range(1,11), wcss)
plt.title('Metodo del codo')
plt.xlabel('Numero de clusters')
plt.ylabel('WCSS(k)')
plt.show()<jupyter_output>C:\Users\HP\anaconda3\lib\site-packages\sklearn\cluster\_kmeans.py:881: UserWarning: KMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=1.
  warnings.warn(<jupyter_text>aplicar el metodo de k-means para segmentar le dataset<jupyter_code>kmeans = KMeans(n_clusters=5,
                init = 'k-means++', 
                max_iter = 300, 
                n_init = 10,
                random_state = 0)
#agrupamiento del dataset en clusters y se realiza la prediccion
y_kmeans = kmeans.fit_predict(X)
y_kmeans
X
plt.scatter(X[y_kmeans == 0, 0], X[y_kmeans == 0, 1], s = 100, c = 'blue', label = 'Cautos')
plt.scatter(X[y_kmeans == 1, 0], X[y_kmeans == 1, 1], s = 100, c = 'red', label = 'Estandar')
plt.scatter(X[y_kmeans == 2, 0], X[y_kmeans == 2, 1], s = 100, c = 'green', label = 'Objetivo')
plt.scatter(X[y_kmeans == 3, 0], X[y_kmeans == 3, 1], s = 100, c = 'cyan', label = 'Descuidados')
plt.scatter(X[y_kmeans == 4, 0], X[y_kmeans == 4, 1], s = 100, c = 'magenta', label = 'Conservadores')
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s = 300, c = 'yellow', label = 'Baricentros')
plt.title('Cluster de clientes')
plt.xlabel('Ingresos anuales (en miles $)')
plt.ylabel('Puntuación de Gasto (1-100)')
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1436354631005448, 'fraction_numerical': 0.033680039623576026, 'mean_word_length': 3.307036247334755, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '223462', 'license': 'MIT', 'n_tokens_mistral': 857, 'n_tokens_neox': 767, 'n_words': 190, 'path': 'Clases/Part 4 - Clustering/24__K_Means_clustering.ipynb', 'repo_name': 'joelbarranteswin/Curso_machine_learning_a_z', 'size': '2019'}