<jupyter_start><jupyter_text>Hierarchy  Dendogram of the Customer Data<jupyter_code>import scipy.cluster.hierarchy as sch
sch.linkage(x,method="ward")#ward minimizes the wcss
dendogram = sch.dendrogram(sch.linkage(x,method="ward"))
plt.title("Dendogram")
plt.xlabel("Annual income & spending score")
plt.ylabel("Distane Measure")
plt.show()
from sklearn.cluster import AgglomerativeClustering
model_h = AgglomerativeClustering(n_clusters=5,linkage="ward")
model_h
labels = model_h.fit_predict(x) #Both steps toghether
labels
plt.scatter(x[labels==0,0],x[labels==0,1],s=75, color='r',label="Cluster1")
plt.scatter(x[labels==1,0],x[labels==1,1],s=75, color='g',label="Cluster2")
plt.scatter(x[labels==2,0],x[labels==2,1],s=75, color='b',label="Cluster3")
plt.scatter(x[labels==3,0],x[labels==3,1],s=75, color='y',label="Cluster4")
plt.scatter(x[labels==4,0],x[labels==4,1],s=75, color='m',label="Cluster5")

#plt.scatter(model.cluster_centers_[:,0],model.cluster_centers_[:,1],s=250,c="black",marker='D')
plt.legend()
plt.xlabel("Annual income")
plt.ylabel("Spending Score")
plt.title("Cluster of Customer Vs Spending Scale")<jupyter_output><empty_output><jupyter_text>Wine Data<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt # this is for visualization
import seaborn as sns # for visualization
%matplotlib inline
from sklearn.cluster import KMeans
#Import the Data Set
df = pd.read_csv("wineQualityReds.csv",index_col=0)
df.head()
df.dtypes
#Selecting the data for cluster
x = df.iloc[:,[0,1,2,3,4,5,6,7,8,9,10,11]].values
#using the elbow method we are able to this...
ks = range(1,11)
WCSS = []
for i in ks:
    #Model the kmeans
    model=KMeans(n_clusters=i,random_state=42)
    #Fit the Model
    model.fit(x) #x = df.iloc[:,[2,3]].values
    #Appending the inertia value in WCSS
    WCSS.append(model.inertia_)
    
#Plot the results to find the 
plt.plot(ks,WCSS,"-o")
plt.xlabel("No. of Clusters")
plt.ylabel("WCSS")
plt.xticks(ks)
plt.show()
#Model the kmeans with ncluster
model = KMeans(n_clusters=4,random_state=42)
model.fit(df)
labels = model.predict(df)
centroids=model.cluster_centers_
centroid_x=centroids[:,0]
centroid_y =centroids[:,1]
xs=df.iloc[:,2]
ys=df.iloc[:,10]

x=np.array(df)
fig,axes =plt.subplots(figsize=(16,10))
plt.scatter(x[labels==0,2],x[labels==0,10],s=75, color='r',label="Cluster1")
plt.scatter(x[labels==1,2],x[labels==1,10],s=75, color='g',label="Cluster2")
plt.scatter(x[labels==2,2],x[labels==2,10],s=75, color='b',label="Cluster3")
plt.scatter(x[labels==3,2],x[labels==3,10],s=75, color='y',label="Cluster4")
plt.scatter(model.cluster_centers_[:,2],model.cluster_centers_[:,10],s=250,c="black",label='Centroid',marker='D')
plt.legend()
plt.xlabel("Citric Acid")
plt.ylabel("Alchohal")
plt.title("Citric Acid vs Alchohal")
labels = model.predict(df)
centroids=model.cluster_centers_
centroid_x=centroids[:,0]
centroid_y =centroids[:,1]
xs=df.iloc[:,6]
ys=df.iloc[:,10]

x=np.array(df)
fig,axes =plt.subplots(figsize=(16,10))
plt.scatter(x[labels==0,6],x[labels==0,10],s=75, color='r',label="Cluster1")
plt.scatter(x[labels==1,6],x[labels==1,10],s=75, color='g',label="Cluster2")
plt.scatter(x[labels==2,6],x[labels==2,10],s=75, color='b',label="Cluster3")
plt.scatter(x[labels==3,6],x[labels==3,10],s=75, color='y',label="Cluster4")
plt.scatter(model.cluster_centers_[:,6],model.cluster_centers_[:,10],s=250,c="black",label='Centroid',marker='D')
plt.legend()
plt.xlabel("free Sulphur")
plt.ylabel("Alchohal")
plt.title("Free Sulphur vs Alchohal")
import scipy.cluster.hierarchy as sch
dendogram = sch.dendrogram(sch.linkage(x,method="ward"))
plt.title("Dendogram")
plt.xlabel("All")
plt.ylabel("Distance Measure")
plt.show()<jupyter_output><empty_output><jupyter_text>Diabetic DataSet<jupyter_code>diabetes = pd.read_csv("diabetes.csv")
from scipy.cluster.hierarchy import dendrogram
z = sch.linkage(diabetes.drop("Outcome",axis=1),method="single")
plt.figure(figsize=(13,10))
dendogram = sch.dendrogram(z,truncate_mode="lastp",p=20,leaf_rotation=45)
plt.title("Dendrogram")
plt.xlabel("Cluster Size")
plt.ylabel("Distance Measure")
plt.show()
z = sch.linkage(diabetes.drop("Outcome",axis=1),method="complete")
plt.figure(figsize=(13,10))
endogram = sch.dendrogram(z,truncate_mode="lastp",p=20,leaf_rotation=45)
plt.title("Dendrogram")
plt.xlabel("Cluster Size")
plt.ylabel("Distance Measure")
plt.show()
z = sch.linkage(diabetes.drop("Outcome",axis=1),method="ward") #Best Value, WCSS value will be less
plt.figure(figsize=(13,10))
endogram = sch.dendrogram(z,truncate_mode="lastp",p=20,leaf_rotation=45)
plt.title("Dendrogram")
plt.xlabel("Cluster Size")
plt.ylabel("Distance Measure")
plt.show()
z = sch.linkage(diabetes.drop("Outcome",axis=1),method="average")
plt.figure(figsize=(13,10))
endogram = sch.dendrogram(z,truncate_mode="lastp",p=20,leaf_rotation=45)
plt.title("Dendrogram")
plt.xlabel("Cluster Size")
plt.ylabel("Distance Measure")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1960547504025765, 'fraction_numerical': 0.039452495974235106, 'mean_word_length': 9.732181425485962, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '652964', 'license': 'MIT', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2007, 'n_words': 279, 'path': 'Unsupervised_Clustering/Practice_Notebooks/Day2_Clustering Kmeans & Hierarchical.ipynb', 'repo_name': 'Aujasvi-Moudgil/ML', 'size': '4968'}