Docsity

Hierarchical Clustering Experiment, Lab Reports of Machine Learning

Toggle sidebar
bg1
In [44]:
In [45]:
In [46]:
In [65]:
Out[46]:
Variable 1 Variable 2
02.00 2.0
15.50 4.0
25.00 5.0
31.50 2.5
41.00 1.0
57.00 5.0
65.75 6.5
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy
data = pd.read_excel \
("C:/Users/kriti/OneDrive/Desktop/machine Learning/experiments/hierarchical_clustering.xlsx")
data
fig = plt.figure(figsize = (5,5))
x = data["Variable 1"]
y = data["Variable 2"]
n = range(0,7)
plt.grid()
plt.scatter(x, y, marker = 'o', c = 'red' )
plt.xlabel('Variable_1')
plt.ylabel('Variable_2')
for i, txt in enumerate(n):
plt.annotate(txt, (x[i], y[i]))
1
2
3
4
1
2
1
1
2
3
4
5
6
7
8
9
10
pf3
pf4
pf5

Partial preview of the text

Download Hierarchical Clustering Experiment and more Lab Reports Machine Learning in PDF only on Docsity!

In [45]: In [46]: In [65]: Out[46]: Variable 1 Variable 2 0 2.00 2. 1 5.50 4. 2 5.00 5. 3 1.50 2. 4 1.00 1. 5 7.00 5. 6 5.75 6. import numpy as np import pandas as pd import matplotlib.pyplot as plt import scipy data = pd.read_excel
("C:/Users/kriti/OneDrive/Desktop/machine Learning/experiments/hierarchical_clustering.xlsx") data fig = plt.figure(figsize = ( 5 , 5 )) x = data["Variable 1"] y = data["Variable 2"] n = range( 0 , 7 ) plt.grid() plt.scatter(x, y, marker = 'o', c = 'red' ) plt.xlabel('Variable_1') plt.ylabel('Variable_2') for i, txt in enumerate(n): plt.annotate(txt, (x[i], y[i]))

In [12]: In [12]: Out[4]: 0 2. 1 5. 2 5. 3 1. 4 1. 5 7. 6 5. Name: Variable 1, dtype: float Out[12]:

x = data["Variable 1"] x x[ 1 ] # how many clusters, draw a dendrogram #linkage-single from scipy.cluster.hierarchy import dendrogram, linkage

In [40]: In [111]: In [41]: Out[18]: array([1, 0, 0, 1, 1, 0, 0], dtype=int64)

Out[111]: 0. Out[41]: array([1, 0, 0, 1, 1, 0, 0], dtype=int64) result fig **=** plt.figure(figsize **=** ( 5 , 5 )) x **=** data["Variable 1"] y **=** data["Variable 2"] n **=** range( 0 , 7 ) fig, ax **=** plt.subplots() ax.scatter(x, y, c **=** Hclustering.labels_, cmap **=** 'rainbow') plt.grid() plt.xlabel('Variable 1') plt.ylabel('Variable 2') **for** i, txt **in** enumerate(n): plt.annotate(txt, (x[i], y[i])) Silhouette score is used to evaluate the quality of clusters created using clustering algorithms: The best value is 1 and the worst value is -1. Values near 0 indicate overlapping clusters. Negative values generally indicate that a sample has been assigned to the wrong cluster, as a different cluster is more similar **from** sklearn.metrics **import** silhouette_score silhouette_score(data, result) Hclustering.labels_