首先放两个好玩的网页,动态可视化展示K-means和DBSCAN
- K-mean
https://www.naftaliharris.com/blog/visualizing-k-means-clustering/ - DBSCAN
https://www.naftaliharris.com/blog/visualizing-dbscan-clustering/
import pandas as pd
beer=pd.read_csv('data.txt',sep=' ')
X = beer[['calories', 'sodium', 'alcohol', 'cost']]
from sklearn.cluster import KMeans
km = KMeans(n_clusters = 3).fit(X)
km2 =KMeans(n_clusters = 2).fit(X)
print(km.labels_)
print(km2.labels_)
beer['cluster'] = km.labels_
beer['cluster2'] =km2.labels_
beer.sort_values('cluster')
# 画图
from pandas.plotting import scatter_matrix
%matplotlib inline
cluster_centers = km.cluster_centers_
cluster2_centers=km2.cluster_centers_
centers = beer.groupby('cluster').mean().reset_index()
centers2=beer.groupby('cluster2').mean().reset_index()
import matplotlib.pyplot as plt
plt.rcParams['font.size'] =