Challenge

Project 2

Steps to Follow:

  1. Preprocess the data: Clean the data, encode categorical variables, and scale the features.
  2. Apply Principal Component Analysis (PCA): To reduce dimensionality and identify the most important features contributing to country classifications.
  3. K-Means Clustering: To group countries into clusters based on the features provided.
  4. Hierarchical Clustering: To further explore the relationships between countries.
  5. Visualization: Visualize the clusters and PCA results to suggest the countries needing aid.

Approach:

  1. PCA:

    • Perform PCA to reduce the number of dimensions and identify which features have the most significant contribution.
    • Visualize the explained variance by each principal component.
  2. Clustering:

    • K-Means: Apply K-Means clustering to classify countries into groups based on socio-economic and health parameters.
    • Hierarchical Clustering: Use hierarchical clustering to explore how countries relate to each other based on the features.

Let's proceed with the steps in Python, starting with data preprocessing and PCA application.

Code to Perform PCA, K-Means, and Hierarchical Clustering:

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

from sklearn.preprocessing import StandardScaler

from sklearn.decomposition import PCA

from sklearn.cluster import KMeans

from scipy.cluster.hierarchy import dendrogram, linkage

# Load the dataset (assuming the dataset is in CSV format)

file_path = '/mnt/data/heart_P1.csv' # Update this with the correct file path if needed

data = pd.read_csv(file_path)

# Step 1: Preprocessing

# Drop the output column from the features

X = data.drop('output', axis=1)

# Handle categorical variables (encode them using label encoding if necessary)

X['sex'] = X['sex'].map({1: 'male', 0: 'female'})

X['cp'] = X['cp'].map({0: 'typical angina', 1: 'atypical angina', 2: 'non-anginal pain', 3: 'asymptomatic'})

# Continue encoding for other categorical features similarly

# Standardizing the features

scaler = StandardScaler()

X_scaled = scaler.fit_transform(X)

# Step 2: Apply PCA

pca = PCA(n_components=2) # Reduce to 2 principal components for visualization

X_pca = pca.fit_transform(X_scaled)

# Plot the explained variance by each principal component

explained_variance = pca.explained_variance_ratio_

print("Explained Variance by PCA Components:", explained_variance)

plt.figure(figsize=(8,6))

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=data['output'], cmap='viridis')

plt.title("PCA of Socio-Economic and Health Data")

plt.xlabel("First Principal Component")

plt.ylabel("Second Principal Component")

plt.colorbar()

plt.show()

# Step 3: Apply K-Means Clustering

kmeans = KMeans(n_clusters=3) # Choosing 3 clusters for example

kmeans_labels = kmeans.fit_predict(X_scaled)

# Plot the K-Means clustering results

plt.figure(figsize=(8,6))

plt.scatter(X_pca[:, 0], X_pca[:, 1], c=kmeans_labels, cmap='rainbow')

plt.title("K-Means Clustering")

plt.xlabel("First Principal Component")

plt.ylabel("Second Principal Component")

plt.colorbar()

plt.show()

# Step 4: Apply Hierarchical Clustering

Z = linkage(X_scaled, 'ward')

plt.figure(figsize=(10, 7))

dendrogram(Z)

plt.title("Hierarchical Clustering Dendrogram")

plt.xlabel("Countries")

plt.ylabel("Distance")

plt.show()

Explanation of the Code:

  1. Preprocessing:
    • We load the data and apply necessary transformations such as label encoding for categorical features.
    • StandardScaler is used to standardize the features for clustering and PCA.
  2. PCA:
    • We apply PCA to reduce the number of features and visualize the variance captured by the principal components. We use 2 components to visualize it easily.
  3. K-Means Clustering:
    • We apply K-Means clustering with 3 clusters to categorize countries based on the provided features.
    • We visualize the clustering results using a scatter plot of the first two principal components.
  4. Hierarchical Clustering:
    • We use linkage from scipy to perform hierarchical clustering, which creates a dendrogram to explore relationships among countries.

Output:

  • PCA Plot: Displays how the countries are distributed across the first two principal components, helping us identify which countries need the most aid based on the clustering.
  • K-Means Plot: Visualizes the countries grouped into 3 clusters based on their socio-economic and health data.
  • Dendrogram: Provides a hierarchical view of how countries are grouped.

Next Steps:

  • After running the code, you can identify which countries fall into clusters with high need based on their socio-economic and health conditions.
  • You may want to further refine the number of clusters in K-Means or explore more components in PCA.
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.