×
Samples Blogs Make Payment About Us 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
Use Python libraries effectively by importing only what you need. For example, if you're working with data, using libraries like pandas and numpy can save time and simplify complex tasks like data manipulation and analysis.
News
In 2024, the Biden-Harris Administration has expanded high-dosage tutoring and extended learning programs to boost academic achievement, helping programming students and others recover from pandemic-related setbacks. These initiatives are funded by federal resources aimed at improving math and literacy skills​

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.