Challenge

Unsupervised Learning - Kmeans Week 11 Challenge

When dealing with categorical data in K-means clustering, you need to use a different method to calculate similarity or distance between data points, as the Euclidean distance used in traditional K-means is not suitable for categorical variables. One common approach is to use the Hamming distance or Jaccard distance.

1. **Hamming Distance**: It measures the number of positions at which the corresponding symbols are different between two categorical vectors. For example, if you have two binary vectors, the Hamming distance is the number of bits where they differ.

2. **Jaccard Distance**: It calculates the dissimilarity between two sets by dividing the size of their intersection by the size of their union. It's often used for binary variables but can be adapted for categorical data.

```python

import pandas as pd

from sklearn.cluster import KMeans

from sklearn.preprocessing import LabelEncoder

# Step 1: Load the car dataset

# Replace 'car_data.csv' with the path to your dataset

car_data = pd.read_csv('car_data.csv')

# Step 2: Preprocess categorical variables

# Encode categorical variables into numerical values

label_encoder = LabelEncoder()

for column in car_data.columns:

if car_data[column].dtype == 'object':

car_data[column] = label_encoder.fit_transform(car_data[column])

# Step 3: Perform K-means clustering

# Specify the number of clusters

k = 3

kmeans = KMeans(n_clusters=k, random_state=42)

# Exclude any columns that shouldn't be considered in clustering, e.g., 'Car_Name'

X = car_data.drop(columns=['Car_Name'])

# Fit K-means to the data

kmeans.fit(X)

# Step 4: Interpret results and derive business insights

# Assign cluster labels to each data point

car_data['Cluster'] = kmeans.labels_

# Analyze cluster characteristics

cluster_means = car_data.groupby('Cluster').mean()

# You can also analyze cluster sizes, centroids, and other metrics to derive insights

# Print cluster characteristics

print(cluster_means)

```

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.