<jupyter_start><jupyter_text>Clustering Categorical Data  You are given much more country data. Using the same methodology as the one in the lecture, group all the countries in 2 clusters.  Already done that? Okay!     There are other features: name and continent. Encode the continent one and use it in the clustering solution. Think about the difference with the previous exercise.  Import the relevant libraries<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
from sklearn.cluster import KMeans<jupyter_output><empty_output><jupyter_text>Load the data Load data from the csv file:  'Categorical.csv'.<jupyter_code>df = pd.read_csv('Categorical.csv')
df.head()<jupyter_output><empty_output><jupyter_text>Remove the duplicate index column from the dataset.<jupyter_code>df.set_index('name',inplace=True)<jupyter_output><empty_output><jupyter_text>Map the data Use the 'continent' category for this analysis.<jupyter_code>df['continent'] = df['continent'].map({'North America':0,'Europe':1,'Asia':2,'Africa':3,'South America':4, 'Oceania':5,'Seven seas (open ocean)':6, 'Antarctica':7})
df.head()<jupyter_output><empty_output><jupyter_text>Select the features<jupyter_code>x = df[['continent']]<jupyter_output><empty_output><jupyter_text>Clustering Use 4 clusters initially.<jupyter_code>kmeans = KMeans(8)
kmeans.fit(x)<jupyter_output><empty_output><jupyter_text>Clustering results<jupyter_code>ident = kmeans.fit_predict(x)
ident
dfc = df
dfc['Clusters'] = ident
dfc.head()<jupyter_output><empty_output><jupyter_text>Plot the data<jupyter_code>plt.scatter(dfc['Longitude'], dfc['Latitude'], c=dfc['Clusters'], cmap = 'rainbow')
plt.xlim(-180,180)
plt.ylim(-90, 90)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12281690140845071, 'fraction_numerical': 0.011830985915492958, 'mean_word_length': 8.446808510638299, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '95914', 'license': 'MIT', 'n_tokens_mistral': 643, 'n_tokens_neox': 610, 'n_words': 144, 'path': 'Resources/Data-Science/Machine-Learning/K-Means/Clustering Categorical Data .ipynb', 'repo_name': 'nate4bs/DataRepo', 'size': '1775'}