Page 353 - AI_Ver_3.0_class_11
P. 353

Without advanced knowledge of what a cluster includes, how can a computer know where a group begins or ends? The
                 answer is simple. Clustering is driven by the principle that objects within a group should be very similar to each other,
                 but very different from the objects outside. The similarity function can vary across different applications, but the basic
                 idea is always the same—group the data so that the related elements are placed together.
                 For Advanced Learners

                  Program 3: Write a program to represent K Means Clustering using Python.

                 import numpy as np
                 import matplotlib.pyplot as plt

                 from sklearn.datasets import make_blobs
                 from sklearn.cluster import KMeans


                 # Generate synthetic data

                 X, _ = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)


                 # Apply K-means clustering
                 kmeans = KMeans(n_clusters=4)
                 kmeans.fit(X)

                 y_kmeans = kmeans.predict(X)


                 # Plot the data points and centroids
                 plt.scatter(X[:, 0], X[:, 1], c=y_kmeans, s=50, cmap='viridis')

                 centers = kmeans.cluster_centers_
                 plt.scatter(centers[:, 0], centers[:, 1], c='red', s=200, alpha=0.75)
                 plt.title('K-means Clustering')
                 plt.xlabel('Feature 1')
                 plt.ylabel('Feature 2')

                 plt.show()




























                                                                                  Machine Learning Algorithms   351
   348   349   350   351   352   353   354   355   356   357   358