kmeans python 难度系数代码
时间: 2023-07-22 21:03:40 浏览: 83
import numpy as np
def kmeans_difficulty(X, K, max_iters=100):
# Initialize centroids randomly
centroids = X[np.random.choice(range(len(X)), K, replace=False)]
for _ in range(max_iters):
# Assign each data point to the nearest centroid
distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=-1)
labels = np.argmin(distances, axis=-1)
# Update centroids
new_centroids = np.array([X[labels == k].mean(axis=0) for k in range(K)])
# Calculate the sum of squared distances within each cluster
cluster_sums = np.array([np.sum(np.square(X[labels == k] - new_centroids[k]))
for k in range(K)])
# Calculate the total sum of squared distances
total_sum = np.sum(cluster_sums)
# Calculate the difficulty coefficient
difficulty_coef = total_sum / len(X)
# Check for convergence
if np.allclose(centroids, new_centroids):
break
centroids = new_centroids
return difficulty_coef
# Example usage
X = np.array([[1, 2], [1.5, 1.8], [5, 8], [8, 8], [1, 0.6], [9, 11]])
K = 2
difficulty_coef = kmeans_difficulty(X, K)
print("Difficulty Coefficient:", difficulty_coef)
阅读全文