K-Means Clustering Algorithm

  1. Read Data:

    Read the data points from the "places.txt" file.

  2. Set Number of Clusters (k):

    Define the number of clusters (k) to be used in the algorithm.

  3. Preprocess Data:

    Convert the data points into tuples of floating-point numbers.

  4. Initialize Centroids:

    Initialize empty dictionaries for centroids and clusters. Generate k random centroids by selecting random data points as initial centroids.

  5. Define Functions:

    Define functions to select random centroids and calculate Euclidean distances between points.

  6. Assign Data Points to Clusters:

    For each data point, calculate the Euclidean distance to each centroid. Assign the data point to the cluster associated with the nearest centroid. Write the index of each data point and its assigned cluster to the "clusters.txt" file.

Code