CHATGPT AI
moons


§ Output

> ['<matplotlib.collections.PathCollection at 0x1a1f5f7278>']
>


§ Markdown

### K-Means Clustering Algorithm
K-means clustering is an unsupervised learning algorithm that groups data points into a specified number of clusters. The algorithm works iteratively to assign each data point to one of k clusters based on the feature similarity. Data points are clustered based on feature similarity. The results of the K-means clustering algorithm are:
• The centroids of the K clusters, which can be used to label new data points • Labels for the training data (each data point is assigned to a single cluster)
The K-means algorithm aims to choose centroids that minimise the inertia, or within-cluster sum-of-squares criterion:

Inertia = Sum of squared distances of samples to their closest cluster center

The goal is to find centroids that minimize this criterion. This can be achieved by running the following steps:

1. Initialize cluster centers randomly
2. Assign each sample to its closest cluster center according to some distance metric (e.g., Euclidean distance)
3. Compute and place new cluster centers as barycenters (mean) of the samples in the new clusters
4. Repeat steps 2 and 3 until some convergence criteria is met (e.g., no samples changed clusters in an iteration).

Once convergence is reached, we have a partitioning of the original dataset into k clusters with associated centroids, which can be used for labeling new data points or for further analysis and visualization.

In this example, we will use Scikit Learn's implementation of K-means clustering to group our moon dataset into two clusters and visualize them using matplotlib's pyplot library. We will also calculate the inertia value for our model and use it as a measure of how well our model has clustered our data points:


§ Code

#import required libraries for kmeans clustering and plotting results
from sklearn import datasets,cluster,metrics #importing datasets from sklearn library
from sklearn.cluster import KMeans #importing kmeans from sklearn library
import matplotlib.pyplot as plt #importing pyplot from matplotlib library

#creating kmeans object with 2 clusters
k_means = KMeans(n_clusters=2)

#fitting our input data i.e moons dataset into kmeans object
k_means = k_means.fit(moons)

#getting predicted values i.e labels/categories assigned by kmeas object into labels variable
labels = k_means .labels_

#calculating inertia value for our model using inertia_ attribute of kmeas object
inertia = k_means .inertia_

#printing calculated inertia value print("Inertia: ",inertia)

#visualizing output by plotting original moons dataset with predicted labels/categories assigned by kmean object plt .scatter(moons[:,0],moons[:,1], c=labels , s=50 , cmap='viridis') plt .show()

Inertia: 0.3720307737952545
§ Output > ['<matplotlib.collections.PathCollection at 0x1a1f5f7278>'] > § Markdown ### K-Means Clustering Algorithm K-means clustering is an unsupervised learning algorithm that groups data points into a specified number of clusters. The algorithm works iteratively to assign each data point to one of k clusters based on the feature similarity. Data points are clustered based on feature similarity. The results of the K-means clustering algorithm are: • The centroids of the K clusters, which can be used to label new data points • Labels for the training data (each data point is assigned to a single cluster) The K-means algorithm aims to choose centroids that minimise the inertia, or within-cluster sum-of-squares criterion: Inertia = Sum of squared distances of samples to their closest cluster center The goal is to find centroids that minimize this criterion. This can be achieved by running the following steps: 1. Initialize cluster centers randomly 2. Assign each sample to its closest cluster center according to some distance metric (e.g., Euclidean distance) 3. Compute and place new cluster centers as barycenters (mean) of the samples in the new clusters 4. Repeat steps 2 and 3 until some convergence criteria is met (e.g., no samples changed clusters in an iteration). Once convergence is reached, we have a partitioning of the original dataset into k clusters with associated centroids, which can be used for labeling new data points or for further analysis and visualization. In this example, we will use Scikit Learn's implementation of K-means clustering to group our moon dataset into two clusters and visualize them using matplotlib's pyplot library. We will also calculate the inertia value for our model and use it as a measure of how well our model has clustered our data points: § Code #import required libraries for kmeans clustering and plotting results from sklearn import datasets,cluster,metrics #importing datasets from sklearn library from sklearn.cluster import KMeans #importing kmeans from sklearn library import matplotlib.pyplot as plt #importing pyplot from matplotlib library #creating kmeans object with 2 clusters k_means = KMeans(n_clusters=2) #fitting our input data i.e moons dataset into kmeans object k_means = k_means.fit(moons) #getting predicted values i.e labels/categories assigned by kmeas object into labels variable labels = k_means .labels_ #calculating inertia value for our model using inertia_ attribute of kmeas object inertia = k_means .inertia_ #printing calculated inertia value print("Inertia: ",inertia) #visualizing output by plotting original moons dataset with predicted labels/categories assigned by kmean object plt .scatter(moons[:,0],moons[:,1], c=labels , s=50 , cmap='viridis') plt .show() Inertia: 0.3720307737952545
0 Comments & Tags 0 hisse senetleri 1 Views

Password Copied!

Please Wait....