×
Reviews 4.9/5 Order Now

Create a Program to Implement K Means Clustering in Python Assignment Solution

July 13, 2024
Dr. Melissa
Dr. Melissa
🇺🇸 United States
Python
Dr. Melissa, with over 5 years of experience, earned her doctorate from the prestigious University of California, Berkeley. She has successfully completed 300+ Python assignments, demonstrating her deep understanding of programming concepts and her ability to deliver top-notch solutions. Driven by a passion for teaching and problem-solving, Dr. Melissa is dedicated to helping students excel in their Python endeavors.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
When doing NetLogo assignments, start by clearly defining agent behaviors and interactions. Use comments in your code to track logic, test models with smaller populations first, and gradually scale up for accuracy and efficiency.
News
JetBrains released IntelliJ IDEA 2025.1, introducing full Java 24 support, Kotlin notebooks, default K2 mode, Dockerfile enhancements, and tighter integration with streaming & database tools—great for students working on enterprise and full-stack assignments.

Instructions

Objective

Write a python assignment program to implement K means clustering.

Requirements and Specifications

program to implement K means clustering in python
program to implement K means clustering in python 1

Source Code

import numpy as np from scipy.spatial.distance import cdist import matplotlib.pyplot as plt data = np.loadtxt('data_points.txt', delimiter=',') data.shape ### Define number of clusters, tolerance and maximum number of iterations K = 4 tol = 1E-6 max_iters = 15 ### Pick centroids centroids = data[np.random.choice(data.shape[0], K),:] print(centroids) ### Begin with K-Means Clustering Algorithm # Define a numpy array to label each point labels = np.zeros((data.shape[0],1)) # Define initial error err = 1E10 iters = 0 while err > tol and iters < max_iters: # Calculate distances to centroids distances = cdist(data, centroids) # Pick the minimum distance index idx = np.argmin(distances, axis = 1) # Now, update centroids old_centroids = centroids.copy() for k in range(K): # Calculate the new value of centroid k idxs_ = np.where(idx == k)[0] centroid = np.mean(data[idxs_], axis = 0) centroids[k] = centroid labels[idxs_] = k # Now, compute error err = np.max(np.abs(old_centroids - centroids)) iters = iters + 1 # Print print("Iteration {0}, error = {1:.8f}".format(iters, err)) ### Plot centroids and points plt.figure() for k in range(K): idxs_ = np.where(labels == k)[0] p = data[idxs_,:] plt.scatter(p[:,0], p[:,1], label = f"K = {k}", marker="+") # Plot centroids plt.scatter(centroids[:,0], centroids[:,1], color='black', marker = "x")

Related Samples

Explore our free Python assignment samples for clarity and better understanding. These samples offer detailed solutions and practical examples, making Python concepts easier to grasp and apply.