Skip to content

Instantly share code, notes, and snippets.

@sebadima
Last active February 8, 2022 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebadima/bddb150e111ff2cebf2a0626021729c7 to your computer and use it in GitHub Desktop.
Save sebadima/bddb150e111ff2cebf2a0626021729c7 to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
from sklearn.datasets import make_blobs
from matplotlib import pyplot as plt
from sklearn.cluster import AffinityPropagation
from sklearn import metrics
# generazione dei dati casuali
X, y = make_blobs(n_samples=40, centers=4, cluster_std=0.80)
plt.scatter(X[:, 0], X[:, 1], cmap='Accent')
afprop = AffinityPropagation(preference=-50)
afprop.fit(X)
labels = afprop.predict(X)
plt.scatter(X[:, 0], X[:, 1], c=labels, s=40, cmap='Accent')
centro_cluster = afprop.cluster_centers_indices_
numero_cluster = len(centro_cluster)
print('Numero di cluster: %d' % numero_cluster)
afprop.cluster_centers_
print("Omogeneità: %0.4f" % metrics.homogeneity_score(y, labels))
print("Completezza: %0.4f" % metrics.completeness_score(y, labels))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment