<jupyter_start><jupyter_text>Coding Exercise 0401  1. K-Means clustering with simulated data:<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1.1. Generate simulated data and visualize:<jupyter_code># Dataset #1.
X1, label1 = make_blobs(n_samples=100, n_features=2, centers=2, cluster_std = 5, random_state=123)
plt.scatter(X1[:,0],X1[:,1], c= label1, alpha=0.7 )
plt.title('Dataset #1 : Original')
plt.show()
# Dataset #2
X2, label2 = make_blobs(n_samples=100, n_features=2, centers=3, cluster_std = 1, random_state=321)
plt.scatter(X2[:,0],X2[:,1], c= label2, alpha=0.7 )
plt.title('Dataset #2 : Original')
plt.show()<jupyter_output><empty_output><jupyter_text>1.2. Apply k-means clustering and visualize:<jupyter_code># Dataset #1 and two clusters.
kmeans = KMeans(n_clusters=2,random_state=123)                 # kmeans object for 2 clusters. radom_state=123 means deterministic initialization.
kmeans.fit(X1)                                                 # Unsupervised learning => Only X1.    
myColors = {0:'red',1:'green', 2:'blue'}                       # Define a color palette: 0~2.
plt.scatter(X1[:,0],X1[:,1], c= pd.Series(kmeans.labels_).apply(lambda x: myColors[x]), alpha=0.7 )   
plt.title('Dataset #1 : K-Means')
plt.show()
# Dataset #1 and three clusters.
kmeans = KMeans(n_clusters=3,random_state=123)                 # kmeans object for 3 clusters. radom_state=123 means deterministic initialization.
kmeans.fit(X1)                                                 # Unsupervised learning => Only X1.    
plt.scatter(X1[:,0],X1[:,1], c= pd.Series(kmeans.labels_).apply(lambda x: myColors[x]), alpha=0.7 ) 
plt.title('Dataset #1 : K-Means')
plt.show()
# Dataset #2 and two clusters.
kmeans = KMeans(n_clusters=2,random_state=123)                 # kmeans object for 2 clusters. radom_state=123 means deterministic initialization.
kmeans.fit(X2)                                                 # Unsupervised learning => Only X2.    
plt.scatter(X2[:,0],X2[:,1], c= pd.Series(kmeans.labels_).apply(lambda x: myColors[x]), alpha=0.7 )  
plt.title('Dataset #2 : K-Means')
plt.show()
# Dataset #2 and three clusters.
kmeans = KMeans(n_clusters=3, random_state=123)                # kmeans object for 3 clusters. radom_state=123 means deterministic initialization.
kmeans.fit(X2)                                                 # Unsupervised learning => Only X2.    
plt.scatter(X2[:,0],X2[:,1], c= pd.Series(kmeans.labels_).apply(lambda x: myColors[x]), alpha=0.7 )  
plt.title('Dataset #2 : K-Means')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13671162960262487, 'fraction_numerical': 0.04593510754648195, 'mean_word_length': 3.404494382022472, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '510967', 'license': 'MIT', 'n_tokens_mistral': 1007, 'n_tokens_neox': 939, 'n_words': 211, 'path': 'SIC_AI_Coding_Exercises/SIC_AI_Chapter_05_Coding_Exercises/ex_0401.ipynb', 'repo_name': 'BattlePlot/SIC-Artificial-Intelligence', 'size': '2743'}