Unsupervised Learning with Simple Python
Pure Python Code: Unsupervised Learning (Simple K-Means)
import random # Step 1: Create fake 2D data points points = [(random.randint(0, 20), random.randint(0, 20)) for _ in range(10)] # Step 2: Pick 2 random points as initial cluster centers centroids = random.sample(points, 2) def distance(p1, p2): # Euclidean distance return ((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2) ** 0.5 def assign_clusters(points, centroids): clusters = {i: [] for i in range(len(centroids))} for point in points: distances = [distance(point, c) for c in centroids] closest = distances.index(min(distances)) clusters[closest].append(point) return clusters def update_centroids(clusters): new_centroids = [] for points in clusters.values(): x_coords = [p[0] for p in points] y_coords = [p[1] for p in points] avg_x = sum(x_coords) / len(points) avg_y = sum(y_coords) / len(points) new_centroids.append((avg_x, avg_y)) return new_centroids # Step 3: Run K-Means algorithm manually for i in range(5): # Repeat 5 times clusters = assign_clusters(points, centroids) centroids = update_centroids(clusters) # Step 4: Print the final groups for i, group in clusters.items(): print(f"\nGroup {i+1}:") for point in group: print(point)
What’s Happening Here:
1. Random points are created.
2. We choose 2 random centers (you can change to 3 or more).
3. The algorithm:
- Assigns each point to the closest center
- Updates the center to be the “average” of points near it
- Repeats this 5 times to “learn” better groupings
Output Example:
Group 1:
(3, 2)
(2, 1)
(4, 3)Group 2:
(15, 18)
(14, 17)
(16, 19)
Even though it’s very basic, this shows how unsupervised learning works without any help — the computer figures out groups on its own.
We’ll draw a simple grid, place the points on it, and show their cluster group using different symbols (like X, O, *, etc.).
Unsupervised Learning with ASCII Visualization
import random # Grid size (like a board of 20x20) GRID_SIZE = 20 # Step 1: Create random 2D points points = [(random.randint(0, GRID_SIZE-1), random.randint(0, GRID_SIZE-1)) for _ in range(10)] # Step 2: Choose 2 random points as cluster centers centroids = random.sample(points, 2) def distance(p1, p2): return ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2) ** 0.5 def assign_clusters(points, centroids): clusters = {i: [] for i in range(len(centroids))} for point in points: dists = [distance(point, c) for c in centroids] closest = dists.index(min(dists)) clusters[closest].append(point) return clusters def update_centroids(clusters): new_centroids = [] for pts in clusters.values(): avg_x = sum(p[0] for p in pts) / len(pts) avg_y = sum(p[1] for p in pts) / len(pts) new_centroids.append((avg_x, avg_y)) return new_centroids # Run clustering steps for _ in range(5): clusters = assign_clusters(points, centroids) centroids = update_centroids(clusters) # Cluster symbols for visualization symbols = ['X', 'O', '*', '#'] # Draw the grid with clusters print("\n Clustering Grid View:\n") for y in range(GRID_SIZE): row = "" for x in range(GRID_SIZE): point = (x, y) printed = False for idx, group in clusters.items(): if point in group: row += symbols[idx] # Use symbol for the cluster printed = True break if not printed: row += '.' # Empty space print(row) # Optional: print group members print("\n Cluster Groups:") for i, group in clusters.items(): print(f"Group {i+1} ({symbols[i]}): {group}")
What we’ll See:
A 20×20 grid, with:
- Points marked as X, O, etc. (based on which group they belong to)
- Empty cells as .
A list showing which points are in which group
Sample Output (snipped):
………………..
………………..
……….X………
………………..
…..O…………..
…..O…………..
………………..
………………..
………………..
……X………….
………………..
………………..
…….X…………
………………..
………………..
………………..
………………..
………………..
………………..
………………..Cluster Groups:
Group 1 (X): [(10, 2), (6, 9), (7, 12)]
Group 2 (O): [(5, 4), (5, 5)]
What It Teaches:
- The computer groups points by closeness, without knowing the groups in advance.
- The ASCII grid gives a visual feeling of clustering without using graphics.
- Kids (or beginners) can see learning in action — like coloring similar things together on paper.
Unsupervised Learning – Brainstorming Session