<jupyter_start><jupyter_text>Unscaled<jupyter_code>x = data.copy()
kmeans = KMeans(3)
kmeans.fit(x)
clusters = data.copy()
clusters['cluster_pred']=kmeans.fit_predict(x)
plt.scatter(clusters['sepal_length'], clusters['sepal_width'], c= clusters ['cluster_pred'], cmap = 'rainbow')
# A scatter plot with unscaled values<jupyter_output><empty_output><jupyter_text>Scaled<jupyter_code>xscaled = data.copy()
x_scaled = preprocessing.scale(xscaled)
x_scaled
# The Elbow Method
wcss = []
cl_num = 10
# 'cl_num' is an arbitrary number.
for i in range(1,cl_num):
# Range is 1 to 9
    kmeans= KMeans(i)
    kmeans.fit(x_scaled)
    wcss_iter = kmeans.inertia_
    wcss.append(wcss_iter)
wcss
number_clusters = range(1,cl_num)
plt.plot(number_clusters, wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('Within-cluster Sum of Squares')
kmeans_scaled = KMeans(3)
kmeans_scaled.fit(x_scaled)
clusters_scaled = data.copy()
clusters_scaled['cluster_pred'] = kmeans_scaled.fit_predict(x_scaled)
# After several tries, I settled with 3 Clusters, just like what I did with the clustered unscaled data of the sepal size.
plt.scatter(clusters_scaled['sepal_length'], clusters_scaled['sepal_width'], c=clusters_scaled ['cluster_pred'], cmap = 'rainbow')<jupyter_output><empty_output><jupyter_text>Checking my Solution<jupyter_code>real_data = pd.read_csv('iris-with-answers.csv')
real_data
# Here I loaded the dataset containing the species of the Iris flower to check whether I did a correct Clustering of the Iris Dataset. 
# I learned that Clustering cannot be trusted at all times even with the Elbow Method. The method doesn't show the optimal number of clusters to use - only the different distance between different clusters and within clusters.
real_data['species'].unique()
# To check the data in the feature species
data_mapped = real_data.copy()
data_mapped['species']=data_mapped['species'].map({'setosa':0, 'versicolor':1, 'virginica':2})
data_mapped
# Here I simply mapped the categorical data into numerical variables to plot them on a graph. 
x = data_mapped.iloc[:,4:]
x
# I used the .iloc indexer to select my desired position in the dataframe.
kmeans = KMeans(3)
kmeans.fit(x)
identified_clusters = kmeans.fit_predict(x)
data_with_clusters = data_mapped.copy()
data_with_clusters['Cluster'] = identified_clusters
data_with_clusters<jupyter_output><empty_output><jupyter_text>Sepal Size<jupyter_code>plt.scatter(data_with_clusters['sepal_length'], data_with_clusters['sepal_width'], c= data_with_clusters ['Cluster'], cmap = 'rainbow')<jupyter_output><empty_output><jupyter_text>Petal Size<jupyter_code>plt.scatter(data_with_clusters['petal_length'], data_with_clusters['petal_width'], c= data_with_clusters ['Cluster'], cmap = 'rainbow')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11297819091884162, 'fraction_numerical': 0.0050053628888094386, 'mean_word_length': 7.4787878787878785, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '577573', 'license': 'Apache-2.0', 'n_tokens_mistral': 1022, 'n_tokens_neox': 978, 'n_words': 237, 'path': 'Clustering the Iris Dataset.ipynb', 'repo_name': 'jovemmanuelre/Clustering-Iris-Dataset', 'size': '2797'}