尝试执行以下代码,学习参数max_iter和tol
(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)max_iter&tol让迭代停止
max_iter
cluster_01 = KMeans(n_clusters =8,init='k-means++',max_iter=10,random_state=10).fit(X)
cluster_01.n_iter_
silhouette_score(X,cluster_01.labels_)
inertia_ = cluster_01.inertia_
inertia_
输出为:
8
0.32232579372186637
583.0524684068532
tol
cluster_01 = KMeans(n_clusters =8,init='k-means++',tol=1e-4,random_state=10).fit(X)
cluster_01.n_iter_
silhouette_score(X,cluster_01.labels_)
inertia_ = cluster_01.inertia_
inertia_
输出为:
22
0.3314853899648912
577.4713083206148
问题: 尝试绘制inertia_随max_iter变化的学习曲线