HeirarchicalClustering
Thu Nov 07 2024 00:36:36 GMT+0000 (Coordinated Universal Time)
Saved by
@sagar123
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())
content_copyCOPY
Comments