K均值聚类python代码
时间: 2023-10-31 22:40:26 浏览: 12
我可以提供一个K均值聚类的Python代码示例: from sklearn.cluster import KMeans # 导入KMeans模型 # 创建KMeans模型,并设置聚类中心数量为3
kmeans = KMeans(n_clusters=3)
# 将数据拟合到模型中
kmeans.fit(data)
# 计算聚类中心
centroids = kmeans.cluster_centers_
# 计算每个点的预测类别
labels = kmeans.labels_
相关问题
k均值聚类python代码
下面是一个简单的 k 均值聚类的 Python 代码示例:
```python
import numpy as np
from sklearn.cluster import KMeans
# 随机生成数据
X = np.random.rand(100, 2)
# 创建一个 KMeans 对象,指定聚类数量为 3
kmeans = KMeans(n_clusters=3)
# 训练模型
kmeans.fit(X)
# 获取聚类结果
labels = kmeans.labels_
# 获取聚类中心
centers = kmeans.cluster_centers_
```
这段代码使用了 `sklearn` 库中的 `KMeans` 类来实现 k 均值聚类算法。首先,我们随机生成了一个 100 行、2 列的数据矩阵 `X`。然后,我们创建了一个 `KMeans` 对象,并将聚类数量设置为 3。接下来,我们用 `fit` 方法训练模型,并用 `labels_` 属性获取聚类结果,用 `cluster_centers_` 属性获取聚类中心。
经典k均值聚类和模糊k均值聚类python代码
经典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均值聚类代码中采用了随机初始化质心的方式,因此每次运行的结果可能不同。
阅读全文