Code icon

The App is Under a Quick Maintenance

We apologize for the inconvenience. Please come back later

Menu iconMenu iconMachine Learning with Python
Machine Learning with Python

Chapter 5: Unsupervised Learning

5.4 Practical Exercises of Chapter 5: Unsupervised Learning

Exercise 1: K-Means Clustering

Using the Iris dataset available in Scikit-learn, perform K-Means clustering with a number of clusters set to 3. After performing the clustering, visualize the clusters in a scatter plot.

Example:

from sklearn.cluster import KMeans
from sklearn import datasets
import matplotlib.pyplot as plt

# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data

# Perform K-Means clustering
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')  # Adjust the cmap for better visualization
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.title('K-Means Clustering of Iris Dataset')
plt.show()

Exercise 2: Hierarchical Clustering

Using the same Iris dataset, perform Hierarchical clustering. Visualize the clusters using a dendrogram.

Example:

import numpy as np
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram
import matplotlib.pyplot as plt

# Perform Hierarchical clustering
agg_clustering = AgglomerativeClustering(n_clusters=3).fit(X)

# Plot the dendrogram
children = agg_clustering.children_
distance = np.arange(children.shape[0])
no_of_observations = np.arange(2, children.shape[0] + 2)
linkage_matrix = np.column_stack([children, distance, no_of_observations]).astype(float)
dendrogram(linkage_matrix, p=3, truncate_mode='level')
plt.show()

Exercise 3: DBSCAN

Again, using the Iris dataset, perform DBSCAN clustering. Experiment with different values of eps and min_samples to see how they affect the clusters.

Example:

from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt

# Perform DBSCAN clustering
dbscan = DBSCAN(eps=0.5, min_samples=5).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=dbscan.labels_, cmap='viridis')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('DBSCAN Clustering')
plt.colorbar(label='Cluster Label')
plt.show()

Exercise 4: PCA

Perform PCA on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How much variance is captured by the first two principal components?

Example:

from sklearn.decomposition import PCA

# Perform PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

# Visualize the reduced data
plt.scatter(X_pca[:, 0], X_pca[:, 1])
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Visualization')
plt.show()

Exercise 5: t-SNE

Perform t-SNE on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How does the visualization compare to the one from PCA?

Exercise 6: Evaluation Metrics

Compute the silhouette score and Davies-Bouldin index for the clusters obtained from K-Means, Hierarchical clustering, and DBSCAN. Which clustering algorithm performed the best according to these metrics?

Remember, the goal of these exercises is not just to get the correct answers, but to understand the process and learn from it. Don't be afraid to experiment and try different things. Happy learning!

Chapter 5 Conclusion

In this chapter, we delved into the fascinating world of unsupervised learning, focusing on clustering techniques and dimensionality reduction methods. We started by exploring different clustering techniques, including K-Means, Hierarchical Clustering, and DBSCAN. Each of these techniques offers a unique approach to grouping data based on similarities, and understanding their strengths and weaknesses is crucial for choosing the right method for a given dataset.

We then moved on to dimensionality reduction, where we discussed Principal Component Analysis (PCA) and t-Distributed Stochastic Neighbor Embedding (t-SNE). These techniques are incredibly powerful for dealing with high-dimensional data, helping to simplify models, improve performance, and make the data easier to visualize and interpret.

We also discussed the importance of evaluation metrics in unsupervised learning. Unlike supervised learning, where we have a clear ground truth to compare our predictions against, unsupervised learning requires different methods for assessing the quality of our models. We explored several metrics, including the silhouette score, Davies-Bouldin index, and the explained variance ratio for PCA.

Finally, we concluded the chapter with practical exercises that allowed you to apply what you've learned. These exercises provided hands-on experience with implementing the techniques discussed in this chapter and interpreting the results.

As we wrap up this chapter, it's important to remember that unsupervised learning is a vast field with many more techniques and concepts to explore. The techniques we discussed in this chapter represent just the tip of the iceberg, but they are fundamental to understanding and working with unsupervised learning.

In the next chapter, we will dive into the world of neural networks and deep learning, where we will explore how these powerful models can learn from data in ways that go beyond what we've seen so far. We'll see how deep learning allows us to tackle more complex problems, and how it's driving many of the most exciting advancements in AI today. Stay tuned!

5.4 Practical Exercises of Chapter 5: Unsupervised Learning

Exercise 1: K-Means Clustering

Using the Iris dataset available in Scikit-learn, perform K-Means clustering with a number of clusters set to 3. After performing the clustering, visualize the clusters in a scatter plot.

Example:

from sklearn.cluster import KMeans
from sklearn import datasets
import matplotlib.pyplot as plt

# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data

# Perform K-Means clustering
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')  # Adjust the cmap for better visualization
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.title('K-Means Clustering of Iris Dataset')
plt.show()

Exercise 2: Hierarchical Clustering

Using the same Iris dataset, perform Hierarchical clustering. Visualize the clusters using a dendrogram.

Example:

import numpy as np
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram
import matplotlib.pyplot as plt

# Perform Hierarchical clustering
agg_clustering = AgglomerativeClustering(n_clusters=3).fit(X)

# Plot the dendrogram
children = agg_clustering.children_
distance = np.arange(children.shape[0])
no_of_observations = np.arange(2, children.shape[0] + 2)
linkage_matrix = np.column_stack([children, distance, no_of_observations]).astype(float)
dendrogram(linkage_matrix, p=3, truncate_mode='level')
plt.show()

Exercise 3: DBSCAN

Again, using the Iris dataset, perform DBSCAN clustering. Experiment with different values of eps and min_samples to see how they affect the clusters.

Example:

from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt

# Perform DBSCAN clustering
dbscan = DBSCAN(eps=0.5, min_samples=5).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=dbscan.labels_, cmap='viridis')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('DBSCAN Clustering')
plt.colorbar(label='Cluster Label')
plt.show()

Exercise 4: PCA

Perform PCA on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How much variance is captured by the first two principal components?

Example:

from sklearn.decomposition import PCA

# Perform PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

# Visualize the reduced data
plt.scatter(X_pca[:, 0], X_pca[:, 1])
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Visualization')
plt.show()

Exercise 5: t-SNE

Perform t-SNE on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How does the visualization compare to the one from PCA?

Exercise 6: Evaluation Metrics

Compute the silhouette score and Davies-Bouldin index for the clusters obtained from K-Means, Hierarchical clustering, and DBSCAN. Which clustering algorithm performed the best according to these metrics?

Remember, the goal of these exercises is not just to get the correct answers, but to understand the process and learn from it. Don't be afraid to experiment and try different things. Happy learning!

Chapter 5 Conclusion

In this chapter, we delved into the fascinating world of unsupervised learning, focusing on clustering techniques and dimensionality reduction methods. We started by exploring different clustering techniques, including K-Means, Hierarchical Clustering, and DBSCAN. Each of these techniques offers a unique approach to grouping data based on similarities, and understanding their strengths and weaknesses is crucial for choosing the right method for a given dataset.

We then moved on to dimensionality reduction, where we discussed Principal Component Analysis (PCA) and t-Distributed Stochastic Neighbor Embedding (t-SNE). These techniques are incredibly powerful for dealing with high-dimensional data, helping to simplify models, improve performance, and make the data easier to visualize and interpret.

We also discussed the importance of evaluation metrics in unsupervised learning. Unlike supervised learning, where we have a clear ground truth to compare our predictions against, unsupervised learning requires different methods for assessing the quality of our models. We explored several metrics, including the silhouette score, Davies-Bouldin index, and the explained variance ratio for PCA.

Finally, we concluded the chapter with practical exercises that allowed you to apply what you've learned. These exercises provided hands-on experience with implementing the techniques discussed in this chapter and interpreting the results.

As we wrap up this chapter, it's important to remember that unsupervised learning is a vast field with many more techniques and concepts to explore. The techniques we discussed in this chapter represent just the tip of the iceberg, but they are fundamental to understanding and working with unsupervised learning.

In the next chapter, we will dive into the world of neural networks and deep learning, where we will explore how these powerful models can learn from data in ways that go beyond what we've seen so far. We'll see how deep learning allows us to tackle more complex problems, and how it's driving many of the most exciting advancements in AI today. Stay tuned!

5.4 Practical Exercises of Chapter 5: Unsupervised Learning

Exercise 1: K-Means Clustering

Using the Iris dataset available in Scikit-learn, perform K-Means clustering with a number of clusters set to 3. After performing the clustering, visualize the clusters in a scatter plot.

Example:

from sklearn.cluster import KMeans
from sklearn import datasets
import matplotlib.pyplot as plt

# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data

# Perform K-Means clustering
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')  # Adjust the cmap for better visualization
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.title('K-Means Clustering of Iris Dataset')
plt.show()

Exercise 2: Hierarchical Clustering

Using the same Iris dataset, perform Hierarchical clustering. Visualize the clusters using a dendrogram.

Example:

import numpy as np
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram
import matplotlib.pyplot as plt

# Perform Hierarchical clustering
agg_clustering = AgglomerativeClustering(n_clusters=3).fit(X)

# Plot the dendrogram
children = agg_clustering.children_
distance = np.arange(children.shape[0])
no_of_observations = np.arange(2, children.shape[0] + 2)
linkage_matrix = np.column_stack([children, distance, no_of_observations]).astype(float)
dendrogram(linkage_matrix, p=3, truncate_mode='level')
plt.show()

Exercise 3: DBSCAN

Again, using the Iris dataset, perform DBSCAN clustering. Experiment with different values of eps and min_samples to see how they affect the clusters.

Example:

from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt

# Perform DBSCAN clustering
dbscan = DBSCAN(eps=0.5, min_samples=5).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=dbscan.labels_, cmap='viridis')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('DBSCAN Clustering')
plt.colorbar(label='Cluster Label')
plt.show()

Exercise 4: PCA

Perform PCA on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How much variance is captured by the first two principal components?

Example:

from sklearn.decomposition import PCA

# Perform PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

# Visualize the reduced data
plt.scatter(X_pca[:, 0], X_pca[:, 1])
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Visualization')
plt.show()

Exercise 5: t-SNE

Perform t-SNE on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How does the visualization compare to the one from PCA?

Exercise 6: Evaluation Metrics

Compute the silhouette score and Davies-Bouldin index for the clusters obtained from K-Means, Hierarchical clustering, and DBSCAN. Which clustering algorithm performed the best according to these metrics?

Remember, the goal of these exercises is not just to get the correct answers, but to understand the process and learn from it. Don't be afraid to experiment and try different things. Happy learning!

Chapter 5 Conclusion

In this chapter, we delved into the fascinating world of unsupervised learning, focusing on clustering techniques and dimensionality reduction methods. We started by exploring different clustering techniques, including K-Means, Hierarchical Clustering, and DBSCAN. Each of these techniques offers a unique approach to grouping data based on similarities, and understanding their strengths and weaknesses is crucial for choosing the right method for a given dataset.

We then moved on to dimensionality reduction, where we discussed Principal Component Analysis (PCA) and t-Distributed Stochastic Neighbor Embedding (t-SNE). These techniques are incredibly powerful for dealing with high-dimensional data, helping to simplify models, improve performance, and make the data easier to visualize and interpret.

We also discussed the importance of evaluation metrics in unsupervised learning. Unlike supervised learning, where we have a clear ground truth to compare our predictions against, unsupervised learning requires different methods for assessing the quality of our models. We explored several metrics, including the silhouette score, Davies-Bouldin index, and the explained variance ratio for PCA.

Finally, we concluded the chapter with practical exercises that allowed you to apply what you've learned. These exercises provided hands-on experience with implementing the techniques discussed in this chapter and interpreting the results.

As we wrap up this chapter, it's important to remember that unsupervised learning is a vast field with many more techniques and concepts to explore. The techniques we discussed in this chapter represent just the tip of the iceberg, but they are fundamental to understanding and working with unsupervised learning.

In the next chapter, we will dive into the world of neural networks and deep learning, where we will explore how these powerful models can learn from data in ways that go beyond what we've seen so far. We'll see how deep learning allows us to tackle more complex problems, and how it's driving many of the most exciting advancements in AI today. Stay tuned!

5.4 Practical Exercises of Chapter 5: Unsupervised Learning

Exercise 1: K-Means Clustering

Using the Iris dataset available in Scikit-learn, perform K-Means clustering with a number of clusters set to 3. After performing the clustering, visualize the clusters in a scatter plot.

Example:

from sklearn.cluster import KMeans
from sklearn import datasets
import matplotlib.pyplot as plt

# Load the Iris dataset
iris = datasets.load_iris()
X = iris.data

# Perform K-Means clustering
kmeans = KMeans(n_clusters=3, random_state=0).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')  # Adjust the cmap for better visualization
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.title('K-Means Clustering of Iris Dataset')
plt.show()

Exercise 2: Hierarchical Clustering

Using the same Iris dataset, perform Hierarchical clustering. Visualize the clusters using a dendrogram.

Example:

import numpy as np
from sklearn.cluster import AgglomerativeClustering
from scipy.cluster.hierarchy import dendrogram
import matplotlib.pyplot as plt

# Perform Hierarchical clustering
agg_clustering = AgglomerativeClustering(n_clusters=3).fit(X)

# Plot the dendrogram
children = agg_clustering.children_
distance = np.arange(children.shape[0])
no_of_observations = np.arange(2, children.shape[0] + 2)
linkage_matrix = np.column_stack([children, distance, no_of_observations]).astype(float)
dendrogram(linkage_matrix, p=3, truncate_mode='level')
plt.show()

Exercise 3: DBSCAN

Again, using the Iris dataset, perform DBSCAN clustering. Experiment with different values of eps and min_samples to see how they affect the clusters.

Example:

from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt

# Perform DBSCAN clustering
dbscan = DBSCAN(eps=0.5, min_samples=5).fit(X)

# Visualize the clusters
plt.scatter(X[:, 0], X[:, 1], c=dbscan.labels_, cmap='viridis')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('DBSCAN Clustering')
plt.colorbar(label='Cluster Label')
plt.show()

Exercise 4: PCA

Perform PCA on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How much variance is captured by the first two principal components?

Example:

from sklearn.decomposition import PCA

# Perform PCA
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)

# Visualize the reduced data
plt.scatter(X_pca[:, 0], X_pca[:, 1])
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.title('PCA Visualization')
plt.show()

Exercise 5: t-SNE

Perform t-SNE on the Iris dataset and reduce it to two dimensions. Then, visualize the reduced data in a scatter plot. How does the visualization compare to the one from PCA?

Exercise 6: Evaluation Metrics

Compute the silhouette score and Davies-Bouldin index for the clusters obtained from K-Means, Hierarchical clustering, and DBSCAN. Which clustering algorithm performed the best according to these metrics?

Remember, the goal of these exercises is not just to get the correct answers, but to understand the process and learn from it. Don't be afraid to experiment and try different things. Happy learning!

Chapter 5 Conclusion

In this chapter, we delved into the fascinating world of unsupervised learning, focusing on clustering techniques and dimensionality reduction methods. We started by exploring different clustering techniques, including K-Means, Hierarchical Clustering, and DBSCAN. Each of these techniques offers a unique approach to grouping data based on similarities, and understanding their strengths and weaknesses is crucial for choosing the right method for a given dataset.

We then moved on to dimensionality reduction, where we discussed Principal Component Analysis (PCA) and t-Distributed Stochastic Neighbor Embedding (t-SNE). These techniques are incredibly powerful for dealing with high-dimensional data, helping to simplify models, improve performance, and make the data easier to visualize and interpret.

We also discussed the importance of evaluation metrics in unsupervised learning. Unlike supervised learning, where we have a clear ground truth to compare our predictions against, unsupervised learning requires different methods for assessing the quality of our models. We explored several metrics, including the silhouette score, Davies-Bouldin index, and the explained variance ratio for PCA.

Finally, we concluded the chapter with practical exercises that allowed you to apply what you've learned. These exercises provided hands-on experience with implementing the techniques discussed in this chapter and interpreting the results.

As we wrap up this chapter, it's important to remember that unsupervised learning is a vast field with many more techniques and concepts to explore. The techniques we discussed in this chapter represent just the tip of the iceberg, but they are fundamental to understanding and working with unsupervised learning.

In the next chapter, we will dive into the world of neural networks and deep learning, where we will explore how these powerful models can learn from data in ways that go beyond what we've seen so far. We'll see how deep learning allows us to tackle more complex problems, and how it's driving many of the most exciting advancements in AI today. Stay tuned!