经典k均值聚类和模糊k均值聚类python代码
时间: 2023-08-31 14:12:55 浏览: 103
经典k均值聚类的Python代码:
```
import numpy as np
class KMeans:
def __init__(self, k=2, max_iter=100):
self.k = k
self.max_iter = max_iter
def fit(self, X):
self.centroids = []
# Initialize centroids randomly
for i in range(self.k):
self.centroids.append(X[np.random.randint(0, len(X), size=1)[0]])
for i in range(self.max_iter):
# Assign points to nearest centroid
clusters = [[] for _ in range(self.k)]
for x in X:
distances = [np.linalg.norm(x - c) for c in self.centroids]
cluster = np.argmin(distances)
clusters[cluster].append(x)
# Update centroids
for j in range(self.k):
self.centroids[j] = np.mean(clusters[j], axis=0)
def predict(self, X):
predictions = []
for x in X:
distances = [np.linalg.norm(x - c) for c in self.centroids]
cluster = np.argmin(distances)
predictions.append(cluster)
return predictions
```
模糊k均值聚类的Python代码:
```
import numpy as np
class FuzzyKMeans:
def __init__(self, k=2, m=2, max_iter=100):
self.k = k
self.m = m
self.max_iter = max_iter
def fit(self, X):
# Initialize membership matrix randomly
self.U = np.random.rand(len(X), self.k)
self.U = self.U / np.sum(self.U, axis=1)[:, None]
for i in range(self.max_iter):
# Update centroids
centroids = []
for j in range(self.k):
centroid = np.sum((self.U[:,j]**self.m)[:,None] * X, axis=0) / np.sum(self.U[:,j]**self.m)
centroids.append(centroid)
# Update membership matrix
distances = np.zeros((len(X), self.k))
for j in range(self.k):
distances[:,j] = np.linalg.norm(X - centroids[j], axis=1)
self.U = distances ** (-2/(self.m-1))
self.U = self.U / np.sum(self.U, axis=1)[:, None]
self.centroids = centroids
def predict(self, X):
distances = np.zeros((len(X), self.k))
for j in range(self.k):
distances[:,j] = np.linalg.norm(X - self.centroids[j], axis=1)
predictions = np.argmin(distances, axis=1)
return predictions
```
注意,这里的经典k均值聚类代码中采用了随机初始化质心的方式,因此每次运行的结果可能不同。
阅读全文