Preview:
 import pandas as pd
 from sklearn.datasets import load_iris
 import matplotlib.pyplot as plt
 from scipy.cluster.hierarchy import dendrogram,linkage,fcluster
 from sklearn.preprocessing import StandardScaler
 iris=load_iris()
 df=pd.DataFrame(iris.data,columns=iris.feature_names)
 print(df.head())
 scaler=StandardScaler()
 scaled_data=scaler.fit_transform(df)
 z=linkage(scaled_data,method='ward')
 plt.figure(figsize=(7,5))
 dendrogram(z,labels=iris.target)
 plt.show()
 s=3
 cluster=fcluster(z,t=s,criterion='maxclust')
 df['cluster']=cluster
 df['Species']=iris.target
 print(df.groupby(['cluster','Species']).size())
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter