×
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
Break your NetLogo model into simple procedures using functions for better readability. Use the ask command efficiently to control agents and optimize performance by minimizing unnecessary computations in the go procedure. Always test your model with small agent populations before scaling up.
News
LFortran Advances: LFortran, an open-source Fortran compiler, achieved compatibility with the PRIMA library in March 2025, enhancing support for numerical computing.

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.