지방이의 Data Science Lab

[python] kmeans, agglomerative clustering 본문

Data Analysis/Python

[python] kmeans, agglomerative clustering

[지현] 2019. 9. 3. 01:46

from sklearn.cluster import AgglomerativeClustering
%time
cluster = AgglomerativeClustering(n_clusters=7, affinity='euclidean', linkage='average')
cluster.fit_predict(temp_data)

pd.value_counts(pd.Series(cluster.labels_))

불러오는 중입니다...

 

 

 

from sklearn.cluster import KMeans

km = KMeans(n_clusters=7)
x_names = [x for x in total_activity.columns if x not in ['acc_id']]
km.fit(total_activity[x_names])
pd.value_counts(pd.Series(km.labels_))

불러오는 중입니다...

 


from scipy.cluster.hierarchy import dendrogram, linkage
import scipy.cluster.hierarchy as spc



from scipy.cluster.hierarchy import cophenet
from scipy.spatial.distance import pdist
import pylab

corr = temp_data.corr()#.values()
Z = linkage(corr, 'average')
c, coph_dists = cophenet(Z, pdist(corr))
c

# pdist = spc.distance.pdist(corr)
# linkage = spc.linkage(pdist, method='average')
# idx = spc.fcluster(linkage, 0.5 * pdist.max(), 'distance')


# assignments = fcluster(linkage(temp_data, method='complete'),4,'distance')
# cluster_output = pandas.DataFrame({'team':df.teamID.tolist() , 'cluster':assignments})

import matplotlib.pyplot as plt  
%matplotlib inline
plt.title('Dendrogram')
plt.xlabel('Index Numbers')
plt.ylabel('Distance')
dendrogram(
    Z,
    leaf_rotation=90.,  
    leaf_font_size=8.,
)
plt.show()

불러오는 중입니다...

 

Comments