Challenge

Project 2

### **Hierarchial (Agglomerative) Clustring**
### Loading required modules

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
from sklearn.preprocessing import StandardScaler
from scipy.cluster.hierarchy import dendrogram, linkage, fcluster

# Let us first explore the given data before using the clustering algorithms

data = pd.read_csv('Country_data.csv', index_col=False, na_values=["?"])
df1 = pd.read_csv('Country_data.csv', index_col=False, na_values=["?"])
df = df1.copy()

# Collecting features
features = list(df.columns)
print(features)

X1 = df[features]

# Data Preprocessing
X = pd.get_dummies(X1)
X = StandardScaler().fit_transform(X)

dendrogram = dendrogram(linkage(X,method='ward'))
clf = AgglomerativeClustering(n_clusters=2, affinity='euclidean', linkage='ward')
clf.fit(X)
labels = clf.labels_

data['anomaly'] = labels
outliers = data.loc[data['anomaly']==1]
outliers_index = list(outliers.index)
print(outliers_index)

# Find the number of anomalies and normal points here points calssified -1 are
#  anomalous

print(data['anomaly'].value_counts())

#import matplitlib.pyplot as plt

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from mpl_toolkits.mplot3d import Axes3D
pca = PCA(n_components=3)  # Reduce to K=3 dimensions
scaler = StandardScaler()

# Normalize the matrix

X = scaler.fit_transform(X)
X_reduce = pca.fit_transform(X)

fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')

ax.scatter(X_reduce[:,0], X_reduce[:,1], zs = X_reduce[:,2], s=4, lw=1, 
           label="inlines", c="green")
ax.scatter(X_reduce[outliers_index,0], X_reduce[outliers_index,1],X_reduce[outliers_index,2],
           lw=2, s=60, marker="x", c="red", label="outliers")
ax.legend()
plt.show()
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.