<jupyter_start><jupyter_text>Kmeans--- from https://jakevdp.github.io/PythonDataScienceHandbook/05.11-k-means.html  imports<jupyter_code>%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns,set()

import pandas as pd
import numpy as np

#Metrics
from sklearn.metrics import silhouette_score

# clusterings 
from sklearn.cluster import KMeans
from sklearn.cluster import SpectralClustering

# datasets
from sklearn.datasets import make_moons
# Create data set

from sklearn.datasets.samples_generator import make_blobs
X, y_true = make_blobs(n_samples=300, centers=4,
                       cluster_std=0.60, random_state=0)
plt.scatter(X[:, 0], X[:, 1], s=50);
# fit and Preduct

kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
y_predict = kmeans.predict(X)
y_kmeans
# creates center points of Kmeans

centers = kmeans.cluster_centers_
# plots clusters
plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')

# plots center points
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5);<jupyter_output><empty_output><jupyter_text>$How it works$    Expectation–maximization (E–M):        under typical circumstances, each repetition of the E-step and M-step will always result in a better estimate of the cluster characteristics.   * Guess some cluster centers   * Repeat until converged       * E-Step: assign points to the nearest cluster center       * M-Step: set the cluster centers to the mean        under typical circumstances, each repetition of the E-step and M-step will always result in a better estimate of the cluster characteristics.  Attributes<jupyter_code># cluster_centers

kmeans.cluster_centers_<jupyter_output><empty_output><jupyter_text>cluster_centers_ : ndarray of shape (n_clusters, n_features)    Coordinates of cluster centers. If the algorithm stops before fully    converging (see ``tol`` and ``max_iter``), these will not be    consistent with ``labels_``.<jupyter_code># Labels

kmeans.labels_<jupyter_output><empty_output><jupyter_text>Labels of each point<jupyter_code>#Intertia 

kmeans.inertia_<jupyter_output><empty_output><jupyter_text>Sum of squared distances of samples to their closest cluster center.<jupyter_code># number of interations run

kmeans.n_iter_
# silhouette score 

silhouette_score(X, kmeans.labels_)<jupyter_output><empty_output><jupyter_text>$ intuitive$ $iterative$ $approach $  Metrics   silhouette analysishttps://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_silhouette_analysis.html  Wrtting simple K-means algorithm<jupyter_code>from sklearn.metrics import pairwise_distances_argmin

def find_clusters(X, n_clusters, rseed=2):
    # 1. Randomly choose clusters
    rng = np.random.RandomState(rseed)
    i = rng.permutation(X.shape[0])[:n_clusters]
    centers = X[i]
    
    while True:
        # 2a. Assign labels based on closest center
        labels = pairwise_distances_argmin(X, centers)
        
        # 2b. Find new centers from means of points
        new_centers = np.array([X[labels == i].mean(0)
                                for i in range(n_clusters)])
        
        # 2c. Check for convergence
        if np.all(centers == new_centers):
            break
        centers = new_centers
    
    return centers, labels

centers, labels = find_clusters(X, 4)
plt.scatter(X[:, 0], X[:, 1], c=labels,
            s=50, cmap='viridis');<jupyter_output><empty_output><jupyter_text>Caveats of expectations * the globally optimal result may not be acheived* the number of clusters must be selected beforehand* K-means is limited to linear cluster boundaries* K-means can be slow for large number of samples<jupyter_code>#<jupyter_output><empty_output><jupyter_text>Sklearn.cluster* DBscan * mean-shift* affinity propagation* SpectralClustering<jupyter_code># new data

X2, y2 = make_moons(200, noise=.05, random_state=0)
# plot kmeans with moons and it fails 

labels = KMeans(2, random_state=0).fit_predict(X2)
plt.scatter(X2[:, 0], X2[:, 1], c=labels,
            s=50, cmap='viridis');
model2 = SpectralClustering(n_clusters=2, affinity='nearest_neighbors',
                           assign_labels='kmeans')
labels = model2.fit_predict(X2)
plt.scatter(X2[:, 0], X2[:, 1], c=labels,
            s=50, cmap='viridis');<jupyter_output>/Users/metamorphosis/opt/anaconda3/lib/python3.8/site-packages/sklearn/manifold/_spectral_embedding.py:236: UserWarning: Graph is not fully connected, spectral embedding may not work as expected.
  warnings.warn("Graph is not fully connected, spectral embedding"
{'fraction_non_alphanumeric': 0.1003528892809881, 'fraction_numerical': 0.016100573445081607, 'mean_word_length': 4.463855421686747, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '302016', 'license': 'MIT', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1477, 'n_words': 392, 'path': 'clustering/.ipynb_checkpoints/Kmeans-checkpoint.ipynb', 'repo_name': 'encodingintuition/MachineLearningWorkbook', 'size': '4534'}