The Fundamentals of K-Means and Fuzzy-C Means Clustering and their usage for Image Segmentation

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import numpy as np
from matplotlib import pyplot as plt
import cv2
""" Plot the data """data = np.array([[1, 2], [1, 4], [1, 0],[10, 2], [10, 4], [10, 0]])
data = np.array([[1, 5], [3, 1], [10, 3], [10, 2], [10, 1], [1, 0], [2, 15], [0.5, 4.9], [5, 3], [7, 13], [18, 18], [1.9, 0.5]])
data = np.random.randint(100, size=(2,2))
centers = [[1, 1], [-1, -1], [1, -1]]
data, _ = make_blobs(n_samples=10000, centers=centers, cluster_std=0.6)
# Plot the data
plt.scatter(data[:,0],data[:,1])
plt.xlabel('x'),plt.ylabel('y')
plt.show()
""" Visualize K means for each iteration """""" create an empty list for each cluster, k is the cluster number """k = 2
clusters = [[[0 for _ in range(2)] for _ in range(1)] for _ in range(k)]
for i in range(k):
clusters[i].pop() #if we dont do...

Continue reading: https://towardsdatascience.com/image-segmentation-with-clustering-b4bbc98f2ee6?source=rss—-7f60cf5620c9—4

Source: towardsdatascience.com