"""
![image.png](attachment:97f32897-00af-4f5e-a1eb-b539a9050eab.png)
"""
"""
# I tried to find what factors have the influence to the WBL score, which shows the Gender Equality.
![image.png](attachment:f0453952-d872-4feb-853d-26bd0a9cea8c.png)
"""
"""
# First, I tried to categorize countries into four clusters. The average features of each clusters are
![image.png](attachment:729ce477-8489-4da0-b587-9d3d0aae6a30.png)
"""
"""
# * Countries in Cluster1 are seem to be better environmnet for gender equality in social, financial, information tech and industrial point of views.
# * Countries in Cluster3 are actually higher in WBL, but there seem to be some domestic gap in society, finance and IT.
# * Countries in Cluster2 are in developed countries, but there seem to be some barriers for WBL.
# * Countries in Cluster4 are still in progress. So the important thing is how they can improve social, financial, information tech and industrial environment for better WBL.
"""
"""
![image.png](attachment:5a9f2093-55ca-4dae-9948-e6d70fb8601f.png)
"""
"""
# Let's go into the details
"""
"""
# 1) Data Preprocessing
"""
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
import matplotlib.pyplot as plt
import seaborn as sns
df=pd.read_csv('/kaggle/input/worldbank-economicsdemographics-data/final_demographics_data.csv')
pd.set_option('display.max_columns', 70)
df.info()
df = df.rename(columns = {'Women Business and the Law Index Score (scale 1-100)': 'WBL'})
df.head()
"""
# 2) Visualization
"""
df['WBL'].plot.hist()
plt.figure(figsize = (15,10))
df_con=df.sort_values('WBL', ascending=False)
sns.barplot(y=df_con['country'],x=df_con['WBL'])
import pycountry
def findCountryAlpha3 (country_name):
    try:
        return pycountry.countries.get(name=country_name).alpha_3
    except:
        return ("USA")
df['country_code'] = df.apply(lambda row: findCountryAlpha3(row.country) , axis = 1)
df.head()
import matplotlib.pyplot as plt
import plotly_express as px

fig = px.choropleth(df,locations='country_code',color='WBL',scope='world',title='WBL by Countries')
fig.show()
plt.figure(figsize = (20,15))
sns.heatmap(df.corr(),annot=True, fmt="1.1f")
cols = df.columns.tolist()
cols = cols[-2:] + cols[:-2]
df = df[cols]
df.corr().head(1)
"""
# I chose the features which have strong positive or negative correlation (over 30% or under -30%) with WBL.
"""
df1=df.iloc[:,[0,2,7,8,11,12,19,21,22,23,24,25,27,33,39,41,44,47,49,50,54,58]]
df1.corr().head(1)
"""
# 3) Clustering by PyCaret
"""
pip install pycaret --ignore-installed llvmlite numba
from pycaret.clustering import *
cluster = setup(df1,silent=True,session_id=1)
kmeans = create_model('kmeans',num_clusters = 4)
plot_model(kmeans, plot = 'cluster')
kmeans_df = assign_model(kmeans)
kmeans_df.head()
kmeans_df.groupby('Cluster').mean().T
kmeans_df['country_code']=df['country_code']
sns.scatterplot(x='Age dependency ratio, old (% of working-age population)', y='WBL',data=kmeans_df,hue='Cluster')
sns.scatterplot(x='Computer, communications and other services (% of commercial service imports)', y='WBL',data=kmeans_df,hue='Cluster')
sns.scatterplot(x='Domestic credit to private sector (% of GDP)', y='WBL',data=kmeans_df,hue='Cluster')
sns.scatterplot(x='Fixed broadband subscriptions (per 100 people)', y='WBL',data=kmeans_df,hue='Cluster')
sns.scatterplot(x='GDP per capita (constant 2010 US$)', y='WBL',data=kmeans_df,hue='Cluster')
sns.scatterplot(x='Human capital index (HCI) (scale 0-1)', y='WBL',data=kmeans_df,hue='Cluster')
sns.scatterplot(x='Individuals using the Internet (% of population)', y='WBL',data=kmeans_df,hue='Cluster')
sns.scatterplot(x='Ratio of female to male labor force participation rate (%) (national estimate)', y='WBL',data=kmeans_df,hue='Cluster')
fig = px.choropleth(kmeans_df,locations='country_code',color='Cluster',scope='world',title='Countries by Clusters')
fig.show()