File:The effect of z-score normalization on k-means clustering.svg

From testwiki
Jump to navigation Jump to search
    Original file (SVG file, nominally 960 × 480 pixels, file size: 500 KB)

    This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

    Summary

    Description
    English: ```python

    import matplotlib.pyplot as plt import numpy as np from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler

    1. Parameters

    n_samples = 300 cluster_std = 0.15

    1. Generate data

    np.random.seed(0) cluster_centers_scales = (([-1, -1], 0.2), ([1, -1], 0.5), ([1.5, 1], 0.2), ([-0.5, 1], 0.5)) data = np.concatenate([

       np.random.normal(loc=center, scale=scale, size=(n_samples, 2))
       for center, scale in cluster_centers_scales
    

    ])

    1. KMeans clustering

    kmeans = KMeans(n_clusters=4, random_state=0)

    1. Stretch along y-axis

    data_stretched = data * np.array([1, 0.02])

    1. KMeans on stretched data

    labels_stretched = kmeans.fit_predict(data_stretched)

    1. Z-score normalize data

    scaler = StandardScaler() data_normalized = scaler.fit_transform(data_stretched)

    1. KMeans on Z-score normalized data

    labels_normalized = kmeans.fit_predict(data_normalized)

    1. Plot both results as subplots within the same figure

    plt.figure(figsize=(10, 5))

    plt.subplot(1, 2, 1) plt.scatter(data_stretched[:, 0], data_stretched[:, 1], c=labels_stretched, marker='+') plt.title('Stretched Data')

    plt.subplot(1, 2, 2) plt.scatter(data_normalized[:, 0], data_normalized[:, 1], c=labels_normalized, marker='+') plt.title('Z-score Normalized Data')

    plt.savefig('z_score_normalization.svg') plt.show()

    ```
    Date
    Source Own work
    Author Cosmia Nebula

    Licensing

    I, the copyright holder of this work, hereby publish it under the following license:
    w:en:Creative Commons
    attribution share alike
    This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
    You are free:
    • to share – to copy, distribute and transmit the work
    • to remix – to adapt the work
    Under the following conditions:
    • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
    • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

    Captions

    Add a one-line explanation of what this file represents

    Items portrayed in this file

    depicts

    6 August 2024

    image/svg+xml

    512,382 byte

    be4d873841c7e16806618e5733b137a4cb1e7733

    File history

    Click on a date/time to view the file as it appeared at that time.

    Date/TimeThumbnailDimensionsUserComment
    current07:20, 7 August 2024Thumbnail for version as of 07:20, 7 August 2024960 × 480 (500 KB)wikimediacommons>Cosmia NebulaUploaded while editing "Normalization (machine learning)" on en.wikipedia.org

    There are no pages that use this file.