# Unveiling K-Means Clustering in Python: A Powerful Unsupervised Learning Technique

## K-Means Clustering in Python

If you are in the field of data science, you must have heard of K-Means Clustering. Clustering is an unsupervised learning technique that helps to classify unstructured data into a structured form.

With K-Means Clustering, we divide data points into different clusters based on their similarity. Python is the most popular language used for data science, and it has dozens of libraries to help in different ways.

## 1. Creating a DataFrame for a Two-Dimensional Dataset

For demonstration purposes, let’s create some sample data. First, we will import pandas, which is a commonly used Python library for data manipulation and analysis.

``````import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

X = -2 * np.random.rand(100,2)
X1 = 1 + 2 * np.random.rand(50,2)
X[50:100, :] = X1
df = pd.DataFrame(X, columns=['X', 'Y'])
df.head(10)
``````

In this code, we are creating two random arrays; one of size 1002 (X) and another of size 502 (X1), both of which are populated with random values. We concatenate these two arrays to create a single dataset (X), which has 100 data points.

We then convert this into a DataFrame using the pandas library. The DataFrame has two columns representing the X and Y coordinates of each data point.

## 2. Finding the Centroids of 3 Clusters, and Then of 4 Clusters

To use K-Means Clustering, we must first determine the centroids of the clusters. The centroids are the mean values of all the points within a cluster.

In this example, we will create three clusters. Let’s use the KMeans function from the sklearn library to do this:

``````from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3)
kmeans.fit(df)
``````

Now that we have created three clusters, we can find their centroids:

``````centroids = kmeans.cluster_centers_

print(centroids)
``````

The output should show the centroids of each cluster as a numpy array with two values (X and Y coordinates). Now, let’s increase the number of clusters to four:

``````kmeans2 = KMeans(n_clusters=4)
kmeans2.fit(df)
centroids2 = kmeans2.cluster_centers_

print(centroids2)
``````

The output will show that there are now four centroids, with each corresponding to one of the four clusters.

## 3. Example of K-Means Clustering in Python

Let’s now visualize our clusters by plotting the data points, coloring the points by cluster and plotting the centroids:

``````plt.scatter(df['X'], df['Y'], c= kmeans.labels_.astype(float), s=50, alpha=0.5)
plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
plt.show()
``````

This code plots our data points, with each point colored according to its assigned cluster.

It also plots the centroids as red dots.

## 4. Unsupervised Learning

Unsupervised learning is a type of machine learning where the model is not provided with any labeled data. The model is trained to find patterns and structure in the data by itself.

## 5. Definition of K-Means Clustering

K-Means Clustering is a type of unsupervised learning where the algorithm partitions a set of data points into a fixed number of clusters, K (where K is a positive integer). The algorithm assigns each data point to a cluster based on the similarity between the data points, where similarity is measured as the distance between the data points.

The goal of the algorithm is to minimize the variance within each cluster while maximizing the variance between the clusters.

## 6. Application of K-Means Clustering in Finding Groups within Unlabeled Data

K-Means Clustering is applicable in a wide variety of contexts, including market segmentation, image segmentation, and anomaly detection. One example of its application is in customer segmentation in the retail industry.

By identifying patterns and similarities in customer behavior, retailers can tailor their marketing strategies to specific groups of customers.

## 7. Comparison with Supervised Learning

Supervised learning is a type of machine learning where the model is trained on labeled data. This means that the data is already classified, and the model is trained to predict the class of a new data point based on its features.

In contrast, unsupervised learning does not use labeled data. Unsupervised learning algorithms identify patterns and structure in the data and do not require a prelabeled dataset to classify data points.

K-Means Clustering is an unsupervised learning technique, making it particularly useful when working with unstructured data.

## 8. Conclusion

In this article, we looked at K-Means Clustering in Python, which is an unsupervised learning technique used to classify unstructured data into a structured form. We discussed how to create a DataFrame for a two-dimensional dataset, how to find the centroids of three clusters, and how to find the centroids of four clusters.

We also talked about unsupervised learning, the K-Means Clustering algorithm, its application in finding groups within unlabeled data, and compared it with supervised learning.

## 9. Creating a DataFrame for a Two-Dimensional Dataset

Data formatting is an essential step in machine learning that helps in building accurate models.

The format of data can have a significant impact on how well a model can perform. As a result, it is critical to have data in the correct format before performing machine learning.

One of the most commonly used formats for storing and manipulating data is the DataFrame. Pandas is a powerful and popular Python module used for data manipulation and analysis.

It is widely used in machine learning for creating, manipulating, and analyzing large, complex data sets. One of the primary data structures in Pandas is the DataFrame, used to organize data in a two-dimensional data structure in a tabular form.

### Here is an example of how to create a DataFrame using the Pandas module:

``````import pandas as pd

data = {'Country': ['India', 'China', 'Japan', 'USA'],
'Population': [1380, 1420, 126.5, 328],
'Area': [3287240, 9596961, 377944, 9833520]}
df = pd.DataFrame(data, columns=['Country', 'Population', 'Area'])

print(df)
``````

In this code, we first import the Pandas module, creating a dictionary named “data” that contains values for three columns: Country, Population, and Area. We then create a new DataFrame using the Pandas DataFrame function, passing our dictionary as the data parameter.

The columns of the DataFrame are defined using the column parameter, which is a list of column names. Finally, we use the print method to display our DataFrame.

DataFrame is an excellent way to organize, view, and analyze two-dimensional data. It is a versatile data structure used primarily in machine learning to preprocess and manipulate data.

## 10. Finding the Centroids of Three Clusters, and Then of Four Clusters

In unsupervised learning, clustering is a popular technique that groups similar data points together. K-means is a common clustering algorithm used to divide data into K clusters.

It does this by finding the centroid points per cluster. Centroids are the central point of cluster data, and the K-means algorithm iteratively calculates these centroid points until they converge and can no longer move.

To understand the process of finding centroids, let’s take an example of an unlabeled dataset of 100 two-dimensional data points. We can use K-means clustering to find three clusters by dividing the dataset into three groups of data points and finding the centroid of each cluster.

The number of clusters can be changed based on the characteristics of the dataset. In K-means clustering, we first randomly select K points, known as centroids.

We then assign each data point to its nearest centroid, forming K clusters. After that, we calculate the centroid of each cluster by taking the average of all data points within that cluster.

Then we reassign each data point to its nearest centroid and repeat until the centroids converge. The final centroids are indicative of the final cluster formation.

### Here is an example of how to find centroids of three clusters and display them:

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

X = -2 * np.random.rand(100,2)
X1 = 1 + 2 * np.random.rand(50,2)
X[50:100, :] = X1
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
centroids = kmeans.cluster_centers_
plt.scatter(X[:,0],X[:,1],s=50,c=kmeans.labels_.astype(float))
plt.scatter(centroids[:,0], centroids[:,1], marker='*', s=300, c='red')
plt.show()
``````

In this code, we create two arrays X and X1, each populated with random values. We concatenate these arrays to create a dataset with 100 data points.

We then use the KMeans function from the Sklearn library to create three clusters, passing our dataset as the parameter. We then find the centroids using the “`.cluster_centers_“` method and plot the data points, coloring the points based on their respective cluster assignments.

Finally, we plot the centroids using red stars. It is essential to specify the number of clusters beforehand because it affects the outcome significantly.

Defining too few clusters will make the model perform poorly, while defining too many clusters will result in the model overfitting on the data.

## 11. Conclusion

In this article, we looked at how to create a DataFrame for a two-dimensional dataset using the Pandas module. We also went through the concept of finding centroids and their importance in clustering-based algorithms.

Knowing how to properly format data and organize it into a two-dimensional data structure is essential in machine learning. We also saw how selecting the right cluster number is crucial; otherwise, the outcome can lead to insufficient or excessive data grouping.

By practicing and implementing these concepts, you can significantly enhance the accuracy and efficiency of your machine learning models.

## 12. Example of K-Means Clustering in Python

K-Means Clustering is a powerful unsupervised learning technique used to identify similarities and groupings in a dataset.

Python is one of the most widely used programming languages in the field of data science. Combining these two technology tools, we can harness and fully utilize the potential of K-Means Clustering.

### To the Example Dataset

For our example, we’ll use a famous dataset called the “Iris dataset.” The Iris dataset comprises four features of three different plants, each with 50 data points.

The first plant is Iris Setosa, the second is Iris Virginica, and the third is Iris Versicolor. The four features include petal length, petal width, sepal length, and sepal width.

The Iris dataset is widely used for clustering and classification problems and can be downloaded from many academic and scientific websites. For this example, we will use the data built into the sklearn library.

## 13. Importance of Matplotlib and Sklearn Modules in Data Visualization and Clustering

Matplotlib is a popular Python library for plotting and data visualization. It provides a wide range of methods and functions to create professional-quality plots and graphs.

Matplotlib can be used to create visualizations for datasets, to visualize model performance, and to plot the results of machine learning models, among other things. Sklearn is also widely used in the field of data science and machine learning.

It is a comprehensive library that provides support for various machine learning algorithms, including K-Means Clustering. Sklearn is known for its accuracy, flexibility, and ease of use.

With these two libraries, we can visualize data, clusters found by the model, and a lot more. Sklearn provides all necessary features required to run K-Means Clustering, while Matplotlib provides the visualization of the findings.

## 14. Example Code for Applying K-Means Clustering in Python

Here is an example code that applies K-Means clustering on an Iris dataset:

``````import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn import datasets

iris = datasets.load_iris()
X = iris.data[:, :2]
kmeans = KMeans(n_clusters=3)
kmeans.fit(X)
plt.scatter(X[:,0], X[:,1], cmap='viridis')
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=200,
alpha=0.5, label='Centroids')
``````

This code first imports libraries such as pandas, numpy, and matplotlib and the required libraries from the sklearn module such as datasets, KMeans, etc. After importing, the iris dataset is loaded and stored in “iris.” The X variable is then assigned values for the first two columns (or “features”) of the dataset.

Next, we create the KMeans model and specify the number of clusters as three. Then, we fit the data using the KMeans clustering algorithm.

The output from the model is a set of cluster labels at each point. Finally, we plot the data as scatter points with separation length versus separation width represented.

The `cmap = ‘viridis’`in scatter() function is used to represent the color of the dots on the graph. The centroids of the three clusters are plotted as red stars.

## 15. Conclusion

In this article, we looked at an example of applying K-Means Clustering in Python using the Iris dataset. We also highlighted the importance of Matplotlib and Sklearn modules in data visualization and clustering.

Python is a versatile and powerful language for data science, and K-Means Clustering is a popular, unsupervised learning technique for identifying patterns in data. By combining these two, we can create powerful and insightful models that can help us better understand our data.

In this article, we discussed K-Means Clustering, an unsupervised learning technique used for data classification. We began by creating a DataFrame for a two-dimensional dataset in Python using the Pandas module and went on to explain how to find the centroids of three and four clusters.

Next, we introduced an example of K-Means Clustering in Python using the Iris dataset and highlighted the importance of the Matplotlib and Sklearn modules in data visualization and clustering. In data science and machine learning, properly formatting and organizing data can significantly impact model performance.

Exploring and using various techniques such as clustering can lead to powerful insights and help us better understand our data.