Kmeans
October 31, 2025
[1]: import pandas as pd
from [Link] import KMeans
import [Link] as plt
# Load your dataset using the full path
df = pd.read_csv("G:\\Edureka_machine_learning\\Assignemt\\module7\\driver-data.
↪csv")
# Select relevant columns (assuming the 2nd and 3rd columns are mean_dist_day␣
↪and mean_over_speed_perc)
new_data = [Link][:, 1:3].copy() # make a safe copy
print(new_data.head())
# Fit KMeans model
kmeans = KMeans(n_clusters=5, random_state=42)
[Link](new_data)
# Assign cluster labels and get centroids
new_data['cluster'] = kmeans.labels_
centroids = kmeans.cluster_centers_
# Plot clusters using Matplotlib
[Link](figsize=(10, 7))
# Scatter plot for each cluster
for cluster_id in range(5):
cluster_points = new_data[new_data['cluster'] == cluster_id]
[Link](
cluster_points.iloc[:, 0], # X-axis: mean_dist_day
cluster_points.iloc[:, 1], # Y-axis: mean_over_speed_perc
label=f'Cluster {cluster_id}'
)
# Plot centroids
[Link](
centroids[:, 0],
centroids[:, 1],
1
s=250, # centroid size
c='black',
marker='X',
label='Centroids'
)
# Add titles, labels, and legend
[Link]('K-Means Clustering of Drivers', fontsize=14)
[Link]('Mean Distance per Day', fontsize=12)
[Link]('Mean Overspeed Percentage', fontsize=12)
[Link]()
[Link](True)
[Link]()
mean_dist_day mean_over_speed_perc
0 71.24 28
1 52.53 25
2 64.54 27
3 55.69 22
4 54.58 25
2
[ ]: