Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn.metrics import silhouette_samples, silhouette_score
- from sklearn.cluster import KMeans
- import matplotlib.cm as cm
- range_n_clusters = [2, 3, 4, 5, 6, 7, 8, 9, 10]
- score = pd.Series()
- features = [
- 'angle(X,gravityMean)',
- 'tGravityAccMag-std()'
- ]
- for n_cluster in range_n_clusters:
- # Create a subplot with 1 row and 2 columns
- fig, (ax1, ax2) = plt.subplots(1, 2)
- fig.set_size_inches(18, 7)
- # The 1st subplot is the silhouette plot
- # The silhouette coefficient can range from -1, 1 but in this example all
- # lie within [-0.1, 1]
- ax1.set_xlim([-0.2, 1])
- # The (n_clusters+1)*10 is for inserting blank space between silhouette
- # plots of individual clusters, to demarcate them clearly.
- ax1.set_ylim([0, len(dane) + (n_cluster + 1) * 10])
- kmeans = KMeans(n_clusters=n_cluster, random_state=0).fit(X_test)
- score = score.append(pd.Series(kmeans.inertia_))
- cluster_labels = kmeans.fit_predict(dane[features])
- silhouette_avg = silhouette_score(dane[features], cluster_labels)
- # Compute the silhouette scores for each sample
- sample_silhouette_values = silhouette_samples(dane[features], cluster_labels)
- y_lower = 10
- for i in range(n_cluster):
- # Zebranie wyników sylwetek do próbek należących do klastra i ich sortowanie
- ith_cluster_silhouette_values = sample_silhouette_values[cluster_labels == i]
- ith_cluster_silhouette_values.sort()
- size_cluster_i = ith_cluster_silhouette_values.shape[0]
- y_upper = y_lower + size_cluster_i
- color = cm.spectral(float(i) / n_cluster)
- ax1.fill_betweenx(np.arange(y_lower, y_upper),
- 0, ith_cluster_silhouette_values,
- facecolor=color, edgecolor=color, alpha=0.7)
- # Etykieta sylwetek z numerami klastrów w środku
- ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i))
- # Wyliczenie przesunięcia w pionie dla kolejnego wykresu
- y_lower = y_upper + 10 # 10 dla kolejnej próbki
- ax1.set_title("Wykres sylwetek dla poszczegolnych klastrow.")
- ax1.set_xlabel("Wartosc sylwetek")
- ax1.set_ylabel("Etykiety klastrow")
- # Wyrysowanie wartości średniej sylwetki
- ax1.axvline(x=silhouette_avg, color="red", linestyle="--")
- ax1.set_yticks([]) # Wyczyszczenie etykiety osi Y
- ax1.set_xticks([-0.1, 0, 0.2, 0.4, 0.6, 0.8, 1])
- # Drugi wykres będzie przedstawiał klastry
- colors = cm.spectral(cluster_labels.astype(float) / n_cluster)
- ax2.scatter(dane[features[0]], dane[features[1]], marker='.', s=30, lw=0, alpha=0.7,
- c=colors)
- # Etykietowanie klastrów
- centers = kmeans.cluster_centers_
- # Rysowanie białych kółek w cetroidach
- ax2.scatter(centers[:, 0], centers[:, 1],
- marker='o', c="white", alpha=1, s=200)
- # Numerowanie centroidów
- for i, c in enumerate(centers):
- ax2.scatter(c[0], c[1], marker='$%d$' % i, alpha=1, s=50)
- ax2.set_title("Wizualizacja grupowania danych.")
- ax2.set_xlabel("Wartosc pierwszej cechy")
- ax2.set_ylabel("Wartosc drugiej cechy")
- plt.suptitle(("Analiza sylwetki dla algorytmu K-Srednich "
- "dla %d klastrow" % n_cluster),
- fontsize=14, fontweight='bold')
- plt.figtext(0.14, 0,
- ("Dla n_klastrow = ", n_cluster,
- "Srednia wartosc sylwetki jest: ", silhouette_avg,
- "Suma odleglosci od centroidow: ", kmeans.inertia_), fontsize=14)
- plt.show()
- plt.plot(range_n_clusters, score,'bo-')
- plt.title("Wykres lokciowy", fontsize=14, fontweight='bold')
- plt.xlabel("Ilosc klastrow")
- plt.ylabel("Suma odleglosci od centroidow")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement