<jupyter_start><jupyter_text>Unit 3 Project    Second section : Unsupervised task In this notebook you will be building and training an unsupervised learning model to cluster your data.  For this task we will be using another clustering method called "Agglomerative Clustering" For this task : It's a hierarchical clustering method. The main idea is to merge examples until the desired number of clusters is achieved.   Steps for this task:  1. Load the already clean dataset  2. Take into consideration that in this task we will not be using the target variable "Category" as we are trying to cluster the data.   3. Cluster numbers should be equal to the number of categories we have  4. We will add those clusters numbers as a feature (column in our dataset)  5. Build your model using the SKlearn AgglomerativeClustering class  6. Cluster your data and that as a column to your dataset 5. Save the new dataset with the additional column as "clustered_HepatitisC.csv"   1. Loading the data 1. Load the clean data 2. Keep only the needed **Columns**<jupyter_code>import pandas as pd 
from sklearn.cluster import AgglomerativeClustering
# Load the dataset 
df = pd.read_csv("HepatitisCdata_1.csv") 
#df.rename(columns={'Unnamed: 0': "ID"}, inplace=True)
df.head()
#Just to save the "Category" column:
df.head()
Category = df["Category"]
Category
#Just to save the "ID" column:
df.head()
Id = df["ID"]
Id
# To-Do : We will select only the columns to work with = all columns except the id and the Category 
df=df.loc[:,["Age","Sex","ALB","ALP","ALT","AST","BIL","CHE","CHOL","CREA","GGT","PROT"]]

df.head()<jupyter_output><empty_output><jupyter_text>2. Build the clusting model and cluster the data 1. Build the model 2. Fit the model to the data 3. Cluster all data points and add that as a column<jupyter_code># To-Do: set the number of clusters to be equal to the number of classes ( categories)
n_clusters =  5
# define the model
model = AgglomerativeClustering(n_clusters=n_clusters)
#To-Do : fit model and predict clusters
yhat = model.fit_predict(df)
# To-Do :Add the cluster as a column to the data frame 
df['cluster'] = yhat
df<jupyter_output><empty_output><jupyter_text>3. Save the new dataset with the additional column<jupyter_code># To-Do save the file , Do not forget to drop the index 
df.insert(1,"Category",Category)
df.insert(0,"ID",Id)

df.head()
df.to_csv('HepatitisCdata_2.csv',index=False )
df.head()<jupyter_output><empty_output>