Unsupervised Learning - Kmeans Week 11 Challenge
1.How does similarity is calculated if data is categorical in nature
Calculating similarity for categorical data in machine learning often involves using various distance metrics or similarity measures that are specifically designed for non-numeric data.
Jaccard Similarity: Jaccard similarity measures the similarity between two sets by comparing their intersection with their union.
J(A, B) = |A ∩ B| / |A ∪ B|
Here, A and B represent two sets or lists of categorical values
Hamming Distance: Hamming distance is used to measure the difference between two binary strings of equal length. It counts the number of positions at which the corresponding elements are different.
Dice Coefficient: Similar to Jaccard similarity, the Dice coefficient is used to measure the similarity between two sets. It's often used for categorical data and is defined as:
Dice(A, B) = (2 * |A ∩ B|) / (|A| + |B|)
Levenshtein Distance: Levenshtein distance (edit distance) is commonly used for measuring the similarity between strings or sequences of categorical data.
Tversky Index: Tversky index is a generalized similarity measure that allows you to control the weight given to false positives and false negatives in binary data.
2.Implement K-means for “Car dataset” and come up with the business insights
# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# Load your car dataset (replace 'your_dataset.csv' with the actual file path)
data = pd.read_csv('Cars_mileage.csv')
# Select the features you want to use for clustering
# For example, we can select 'Horsepower' and 'Weight'
features = data[['HP', 'WT']]
# Choose the number of clusters (K)
K = 3
# Initialize and fit the K-means model
kmeans = KMeans(n_clusters=K)
kmeans.fit(features)
# Get the cluster labels for each data point
cluster_labels = kmeans.labels_
# Add the cluster labels to the original dataset
data['Cluster'] = cluster_labels
# Print the cluster centers
cluster_centers = kmeans.cluster_centers_
print("Cluster Centers:")
print(cluster_centers)
# Visualize the clusters
plt.scatter(features['HP'], features['WT'], c=cluster_labels, cmap='rainbow')
plt.scatter(cluster_centers[:, 0], cluster_centers[:, 1], marker='x', s=200, c='black')
plt.xlabel('Horsepower')
plt.ylabel('Weight')
plt.title('K-means Clustering')
plt.show()