<jupyter_start><jupyter_text><jupyter_code># Importar librerías necesarias
from sklearn.datasets import load_breast_cancer
from sklearn.cluster import KMeans
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import scale
import pandas as pd
# Cargar datos del dataset
bc = load_breast_cancer()
print(bc)
# Cargar los datos y el label, además escalar los datos
X = scale(bc.data)
print(X)
y = bc.target
#Dividir en datos de entrenamiento y prueba
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.33)
#Declarar el modelo y entrenarlo
model = KMeans(n_clusters=2)
model.fit (X_train)
#Realizar predicciones
prediction = model.predict(X_test)
labels = model.labels_
print(labels)
# Imprimir la eficacia
print('Efectividad: ', accuracy_score(y_test, prediction))
print('Real: ', y_test)
print('Predicciones: ', prediction)
# Los labels están invertidos, se invierten los valores para calcular nuevamente la efectividad
fix_p = prediction.copy()
for i,j in enumerate(fix_p):
  #print(i)
  #print(j)
  if j == 0:
    fix_p[i] = 1
  else:
    fix_p[i] = 0
print(fix_p)
# Imprimir la eficacia
print('Efectividad: ', accuracy_score(y_test, fix_p))
print('Real: ', y_test)
print('Predicciones: ', fix_p)<jupyter_output>Efectividad:  0.9414893617021277
Real:  [1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0
 0 1 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 0 0 1
 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 0 1 1 1 1 1 0 0
 1 0 0 0 0 0 0 0 0 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1
 1 1 0 1 0 0 1 1 1 0 1 1 0 0 0 1 1 0 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 0 0
 1 1 1]
Predicciones:  [1 0 1 1 0 0 1 1 1 0 0 0 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0
 0 1 1 1 0 1 0 1 1 1 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 0 0 1
 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 0 1 0 0 1 1 1 0 1 1 0 1 0 1 1 1 1 1 1 1
 1 0 0 0 0 1 0 0 0 1 1 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 0 1 1 1 0 1 1 1 1 1 1
 1 1 1 1 0 0 1 1 1 0 1 1 0 0 0 1 1 0 0 0 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 0 1
 1 1 1]
{'fraction_non_alphanumeric': 0.06497175141242938, 'fraction_numerical': 0.18832391713747645, 'mean_word_length': 2.6956521739130435, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '144805', 'license': 'Apache-2.0', 'n_tokens_mistral': 1290, 'n_tokens_neox': 899, 'n_words': 510, 'path': 'KMeans.ipynb', 'repo_name': 'bryanlopezb/practicas-machine-learning', 'size': '2124'}