运行 ❯
获取你
自己的 Python
服务器
×
更改方向
更改主题, 深色/亮色
进入 Spaces
#Three lines to make our compiler able to draw: import sys import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from sklearn.cluster import KMeans x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12] y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21] data = list(zip(x, y)) inertias = [] for i in range(1,11): kmeans = KMeans(n_clusters=i) kmeans.fit(data) inertias.append(kmeans.inertia_) plt.plot(range(1,11), inertias, marker='o') plt.title('Elbow method') plt.xlabel('Number of clusters') plt.ylabel('Inertia') plt.show() #Two lines to make our compiler able to draw: plt.savefig(sys.stdout.buffer) sys.stdout.flush()