<jupyter_start><jupyter_text>K Means Clustering with Python Method UsedK Means Clustering is an unsupervised learning algorithm that tries to cluster data based on their similarity. Unsupervised learning means that there is no outcome to be predicted, and the algorithm just tries to find patterns in the data. In k means clustering, we have the specify the number of clusters we want the data to be grouped into. **The algorithm randomly assigns each observation to a cluster, and finds the centroid of each cluster. Then, the algorithm iterates through two steps:*** `Reassign data points to the cluster whose centroid is closest. `* `Calculate new centroid of each cluster.` These two steps are repeated till the within cluster variation cannot be reduced any further. The within cluster variation is calculated as the sum of the euclidean distance between the data points and their respective cluster centroids.  Import Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Create some artifical Data<jupyter_code>from sklearn.datasets import make_blobs
data= make_blobs(n_samples=200, n_features=2, centers=4, cluster_std=1.8, random_state=101)<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>data<jupyter_output><empty_output><jupyter_text>data is a tuple. Inside has 2 numpy arrays.* first: the data points* second: cluster number that each data point belong to.<jupyter_code>data[0].shape
plt.scatter(data[0][:, 0], data[0][:, 1]);
plt.scatter(data[0][:, 0], data[0][:, 1], c=data[1], cmap='rainbow');<jupyter_output><empty_output><jupyter_text>check the second item of the data<jupyter_code># check the second item of the data
data[1]
data[1].shape<jupyter_output><empty_output><jupyter_text>K-Means Modelling<jupyter_code>from sklearn.cluster import KMeans
km_model = KMeans(n_clusters=4)
km_model.fit(data[0])<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code># check the cluster center
km_model.cluster_centers_
# prediction of the data points and their predicted clusters which model think they are belonged to
km_model.labels_<jupyter_output><empty_output><jupyter_text>Visually Compare the actual clusters and predicted one<jupyter_code>fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10, 6)) # sharey= share the axis

ax1.set_title('K Means')
ax1.scatter(data[0][:, 0], data[0][:, 1], c=km_model.labels_) # color by predicted clusters

ax2.set_title('Original')
ax2.scatter(data[0][:, 0], data[0][:, 1], c=data[1]) # color by original clusters<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09335863377609108, 'fraction_numerical': 0.017077798861480076, 'mean_word_length': 7.1609907120743035, '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': '287050', 'license': 'MIT', 'n_tokens_mistral': 862, 'n_tokens_neox': 826, 'n_words': 285, 'path': 'Data Science and Machine Learning Bootcamp - JP/18. K Means Clustering/01-K Means Clustering with Python.ipynb', 'repo_name': 'ptyadana/probability-and-statistics-for-business-and-data-science', 'size': '2635'}