image_output_1.png
from sklearn.cluster import AgglomerativeClustering

import matplotlib.pyplot as plt

import pandas as pd

import scipy.cluster.hierarchy as sch

dataset = pd.read_csv('/kaggle/input/student-marks-dataset/Student_Marks.csv')
dataset

dataset.isnull().sum()

dataset.nunique()

X = dataset.iloc[:, 1:3].values.round(2)

import scipy.cluster.hierarchy as sch
den = sch.dendrogram(sch.linkage(X, method='ward'))

from sklearn.cluster import AgglomerativeClustering
hc = AgglomerativeClustering(n_clusters=5, affinity='euclidean', linkage='ward')
y_hc = hc.fit_predict(X)

plt.figure(figsize=(10, 7))
plt.scatter(X[y_hc == 0, 0], X[y_hc == 0, 1], s=100, c='red', label='Cluster 1')
plt.scatter(X[y_hc == 1, 0], X[y_hc == 1, 1], s=100, c='blue', label='Cluster 2')
plt.scatter(X[y_hc == 2, 0], X[y_hc == 2, 1], s=100, c='green', label='Cluster 3')
plt.scatter(X[y_hc == 3, 0], X[y_hc == 3, 1], s=100, c='cyan', label='Cluster 4')
plt.scatter(X[y_hc == 4, 0], X[y_hc == 4, 1], s=100, c='magenta', label='Cluster 5')
plt.title('Clusters of students')
plt.xlabel('Number of courses')
plt.ylabel('Time spent to study')
plt.legend()
plt.show()