Project 2
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 data from the CSV file
file_path = 'path_to_your_directory/country_data.csv'
df = pd.read_csv(file_path)
# Select relevant columns for analysis
columns_for_analysis = ['child_mort', 'exports', 'health', 'imports', 'income', 'inflation', 'life_expec', 'total_fer', 'gdpp']
data_for_analysis = df[columns_for_analysis]
# 1. Data Preprocessing: Standardize the data
scaler = StandardScaler()
scaled_data = scaler.fit_transform(data_for_analysis)
# 2. Principal Component Analysis (PCA):
n_components = 2
pca = PCA(n_components=n_components)
pca_result = pca.fit_transform(scaled_data)
# Plot the explained variance
explained_variance_ratio = pca.explained_variance_ratio_
cumulative_explained_variance = np.cumsum(explained_variance_ratio)
plt.plot(range(1, n_components + 1), cumulative_explained_variance, marker='o', linestyle='--')
plt.title('Cumulative Explained Variance')
plt.xlabel('Number of Principal Components')
plt.ylabel('Cumulative Explained Variance')
plt.show()
# 3. K-Means Clustering:
n_clusters = 3
kmeans = KMeans(n_clusters=n_clusters, random_state=42)
cluster_labels = kmeans.fit_predict(pca_result)
# Visualize K-Means Clustering results
df['cluster_kmeans'] = cluster_labels
plt.scatter(pca_result[:, 0], pca_result[:, 1], c=cluster_labels, cmap='viridis', alpha=0.8)
plt.title('K-Means Clustering Results')
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
# 4. Hierarchical Clustering:
linked = linkage(scaled_data, 'ward')
# Plot the dendrogram
dendrogram(linked, orientation='top', distance_sort='descending', show_leaf_counts=True)
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('Countries')
plt.ylabel('Distance')
plt.show()
# 5. Interpretation and Recommendation:
# Analyze the clusters and dendrogram to suggest countries in need of aid based on the analysis.
# You can check the cluster centroids for K-Means or the dendrogram for hierarchical clustering.
# Example of getting cluster centroids for K-Means
cluster_centroids = pd.DataFrame(kmeans.cluster_centers_, columns=['PC1', 'PC2'])
print(cluster_centroids)
# Example of getting countries in each cluster
clustered_data = pd.concat([df['country'], df['cluster_kmeans']], axis=1)
# Assuming 'cluster_kmeans' is the cluster column created during K-Means
countries_in_cluster_0 = clustered_data[clustered_data['cluster_kmeans'] == 0]['country'].tolist()
countries_in_cluster_1 = clustered_data[clustered_data['cluster_kmeans'] == 1]['country'].tolist()
countries_in_cluster_2 = clustered_data[clustered_data['cluster_kmeans'] == 2]['country'].tolist()
print("Countries in Cluster 0:", countries_in_cluster_0)
print("Countries in Cluster 1:", countries_in_cluster_1)
print("Countries in Cluster 2:", countries_in_cluster_2)