尝试执行以下代码,学习参数n_init和random_state
(1) 导入需要的模块、库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
plt.style.use('ggplot')
(2)自建数据集
X, y = make_blobs(n_samples=500,
n_features=2,centers=4,random_state=1)
(3)n_init&random_state确定质心位置
n_init
cluster_01 = KMeans(n_clusters = 8,n_init=500).fit(X)
cluster_01.n_iter_
silhouette_score(X,cluster_01.labels_)
random_state
cluster_02 = KMeans(n_clusters = 8,n_init=500,random_state=10).fit(X)
cluster_02.n_iter_
silhouette_score(X,cluster_02.labels_)