<jupyter_start><jupyter_text><jupyter_code>#importing necessary modules
import numpy as np #array processing
import pandas as pd #dataframes
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
db1 = pd.read_html('https://www.numbeo.com/crime/rankings_by_country.jsp') #importing crime index statistics
db1
db1 = db1[1:][:] #slicing to remove the first row
db1
db1 = np.array(db1) #converting Python list to numpy array
db1.shape
db1 = pd.DataFrame(db1[0]) #converting numpy array to a dataframe
db1
db1.drop([0,3], axis =1, inplace = True) #dropping columns with 0 and 3 indices
db1
db2 = pd.read_html('https://worldpopulationreview.com/country-rankings/happiest-countries-in-the-world') #importing world happiness index report
db2 = np.array(db2) #converting python list to numpy array
db2.shape
db2 = pd.DataFrame(db2[0]) #converting numpy array to dataframe
db2
db2.drop([0,3,4], axis = 1, inplace = True) #dropping columns with indices 0,3 & 4
db2
db = pd.DataFrame.merge(db1 , db2 , on = 1 , how = 'inner') #merging two dataframes through an inner join
db
db.info() #check for null values
db.rename(columns={1: 'Country', '2_x': 'crime index','2_y':'happiness index'}, inplace=True) #renaming the column headers of the dataframe
db
db3 = pd.read_html('https://worldpopulationreview.com/country-rankings/most-corrupt-countries') #importing corruption perception indices

db3 = np.array(db3) #converting python list to a numpy array
db3 = pd.DataFrame(db3[0]) #converting numpy array to a dataframe
db3
db3.drop(2, axis =1, inplace = True) #dropping column with index 2 from the dataframe
db3.rename(columns={0: 'Country', 1: 'corruption perception index'}, inplace=True) #renaming column headers
db = pd.DataFrame.merge(db , db3 , on = 'Country' , how = 'inner') #merging two dataframes through an inner join
db
db4 = pd.read_excel('https://github.com/Asif1310/3005/blob/main/FEMALE%20LITERACY%20FROM%20UNICEF.xlsx?raw=true') #importing the Excel file of youth literacy data from GitHub
db4.drop(0,axis = 0, inplace = True) #dropping the column with index 0 from dataframe
db4.dropna(inplace = True) #dropping all null values from dataframe
db4.drop('Time period', axis = 1, inplace = True) #dropping the Time period column from dataframe
db4.rename(columns={'Geographic area': 'Country', 'Unnamed: 2': 'Female literacy rate among the youth'}, inplace=True) #renaming the column headers of dataframe
db4
db = pd.DataFrame.merge(db , db4 , on = 'Country' , how = 'inner') #merging two dataframes through an inner join
db
data = db.iloc[:,1:] #separating the numeric columns of the dataframe from the country names
data
data.info()
data = data.astype('float64') #converting the data in all the columns of dataframe to float64 datatype
data.info()
#feature scaling 
sc = StandardScaler() 
data = sc.fit_transform(data)
data
metrics = [] #initializing a blank list
test_no_clusters = 6 
for num in range(1, test_no_clusters+1):
    kmeans = KMeans(n_clusters = num, init = 'k-means++', random_state = 32)
    kmeans.fit(data)
    metrics.append(kmeans.inertia_)
metrics
#elbow method to find the optimal number of clusters
plt.plot(range(1, test_no_clusters+1), metrics)
plt.xlabel('number of clusters')
plt.ylabel('WCSS metric')
kmeans = KMeans(n_clusters = 4, init = 'k-means++', random_state = 42) #dividing the dataset into 4 clusters
clusters = kmeans.fit_predict(data) #storing cluster numbers predicted
clusters = pd.Series(clusters)
db['Clusters'] = clusters
db
db[db['Clusters'] == 0] #cluster 1
db[db['Clusters'] == 1] #cluster 2
db[db['Clusters'] == 2] #cluster 3
db[db['Clusters'] == 3] #cluster 4<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11855252497974615, 'fraction_numerical': 0.029165541452876045, 'mean_word_length': 5.637992831541219, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '666860', 'license': 'MIT', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1231, 'n_words': 401, 'path': 'Clustering_the_countries.ipynb', 'repo_name': 'Asif1310/3005', 'size': '3703'}