Challenge

Unsupervised Learning - Kmeans Week 11 Challenge

1.

There are a number of ways to calculate similarity if data is categorical in nature. Some of the most common methods include:

  • Jaccard similarity: The Jaccard similarity coefficient is a measure of similarity between two sets. It is calculated by dividing the number of elements that are common to both sets by the sum of the number of elements in each set.
  • Tanimoto coefficient: The Tanimoto coefficient is a measure of similarity between two sets that takes into account the number of elements that are unique to each set. It is calculated by dividing the number of elements that are common to both sets by the sum of the number of elements in each set minus the number of elements that are unique to each set.
  • Overlap coefficient: The overlap coefficient is a measure of similarity between two sets that is similar to the Jaccard similarity coefficient, but it takes into account the order of the elements in the sets. It is calculated by dividing the number of elements that are common to both sets by the minimum number of elements in each set.
  • Hamming distance: The Hamming distance is a measure of the difference between two sets. It is calculated by counting the number of elements that are different in the two sets.

The choice of similarity measure depends on the specific problem that is being solved. For example, the Jaccard similarity coefficient is often used for text analysis tasks, while the Hamming distance is often used for DNA sequence analysis tasks

2.

import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score, davies_bouldin_index
import davies_bouldin_index from sklearn.metrics

# Load the dataset
data = pd.read_csv(r"C:\Users\venka\Downloads\__MACOSX-20230715T140623Z-001.zip.csv",encoding="unicode_escape")

# Choose a set of features to use for the clustering task
features = ["Price", "Mileage", "Engine Size"]

# Implement K-means on the dataset
kmeans = KMeans(n_clusters=3)
kmeans.fit(data[features])

# Evaluate the performance of the K-means model
silhouette_score = silhouette_score(data[features], kmeans.labels_)
davies_bouldin_index = davies_bouldin_index(data[features], kmeans.labels_)

# Interpret the results of the K-means model
print("Silhouette coefficient:", silhouette_score)
print("Davies-Bouldin index:", davies_bouldin_index)

Conclusion:

The output of the code will be the silhouette coefficient and the Davies-Bouldin index of the K-means model. The results will vary depending on the hyperparameters that are used.

In general, K-means is a good choice for clustering tasks. However, it is important to note that the performance of the K-means model can be sensitive to the choice of features and the hyperparameters.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.