【阿旭机器学习实战】系列文章主要介绍机器学习的各种算法模型及其实战案例,欢迎点赞,关注共同学习交流。
本文对机器学习中的KMeans聚类中常见的几个问题进行说明介绍。
samples,targets = datasets.make_blobs(n_samples=150,n_features=2,centers=3,random_state=1)
plt.scatter(samples[:,0],samples[:,1],c=targets)
km = KMeans(n_clusters=2)
km.fit(samples)
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
n_clusters=2, n_init=10, n_jobs=1, precompute_distances='auto',
random_state=None, tol=0.0001, verbose=0)
y_ = km.predict(samples)
metrics.adjusted_rand_score(targets,y_)
0.5681159420289855
metrics.silhouette_score(samples,km.labels_)
0.7802809392385796
一般综合考虑ARI指标和轮廓系数来决定划分几个聚类。
更多优质内容可关注公众号:
“阿旭算法与机器学习”
,共同学习交流
samples,target = datasets.make_blobs(n_features=2,n_samples=150,centers=3,random_state=5)
plt.scatter(samples[:,0],samples[:,1],c=target)
km = KMeans(n_clusters=2)
y_ = km.fit_predict(samples)
metrics.adjusted_rand_score(y_,target)
0.5681159420289855
metrics.silhouette_score(samples,km.labels_)
0.7744057193895231
对于偏差比较大的数据,可以引入一个修正矩阵来对特征进行修正
trans = np.array([[0.6,-0.6],[-0.4,0.8]])
sam1 = np.dot(samples,trans)
plt.scatter(sam1[:,0],sam1[:,1],c=target)
km = KMeans(n_clusters=2)
y_ = km.fit_predict(sam1)
metrics.adjusted_rand_score(y_,target)
0.5681159420289855
metrics.silhouette_score(sam1,km.labels_)
0.8527525114558019
samples,target= datasets.make_blobs(n_features=2,n_samples=150,centers=3,
cluster_std=[0.5,2,10])
plt.scatter(samples[:,0],samples[:,1],c=target)
km = KMeans(n_clusters=3)
y_ = km.fit_predict(samples)
plt.scatter(samples[:,0],samples[:,1],c=y_)
metrics.adjusted_rand_score(y_,target)
0.530328438685834
metrics.silhouette_score(samples,km.labels_)
0.5932400629187524
samples,target = datasets.make_blobs(n_samples=1500,n_features=2,centers=3,random_state=0)
train1 = samples[target==0][:10]
train2 = samples[target==1][:100]
train3 = samples[target==2][:500]
train = np.concatenate([train1,train2,train3])
# 用train作为训练数据
train.shape
(610, 2)
# 生成标签
target = [0]*10 + [1]*100 + [2]*500
plt.scatter(train[:,0],train[:,1],c=target)
km = KMeans(n_clusters=3)
y_ = km.fit_predict(train)
plt.scatter(train[:,0],train[:,1],c=y_)
metrics.adjusted_rand_score(y_,target)
0.3479149392627809
metrics.silhouette_score(train,km.labels_)
0.35994070955867
如果内容对你有帮助,感谢记得点赞+关注哦!
更多干货内容持续更新中…