3-5-1K均值聚类

#3-5-1K均值聚类
import mglearn
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.datasets import load_breast_cancer
from sklearn.datasets import make_moons
from sklearn.datasets import make_blobs
from sklearn.datasets import make_circles
from sklearn.datasets import load_iris
from sklearn.datasets import fetch_lfw_people
from sklearn.datasets import load_digits
from sklearn.decomposition import NMF
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.svm import LinearSVC
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.manifold import TSNE
from sklearn.tree import DecisionTreeClassifier
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from numpy.core.umath_tests import inner1d
from mpl_toolkits.mplot3d import Axes3D,axes3d
mglearn.plots.plot_kmeans_algorithm()

在这里插入图片描述

mglearn.plots.plot_kmeans_boundaries()

在这里插入图片描述

x,y = make_blobs(random_state=1)
kmeans = KMeans(n_clusters=3)
kmeans.fit(x)
print("cluster memberships:<n{}".format(kmeans.labels_))
print(kmeans.predict(x))

cluster memberships:<n[1 2 2 2 0 0 0 2 1 1 2 2 0 1 0 0 0 1 2 2 0 2 0 1 2 0 0 1 1 0 1 1 0 1 2 0 2
2 2 0 0 2 1 2 2 0 1 1 1 1 2 0 0 0 1 0 2 2 1 1 2 0 0 2 2 0 1 0 1 2 2 2 0 1
1 2 0 0 1 2 1 2 2 0 1 1 1 1 2 1 0 1 1 2 2 0 0 1 0 1]
[1 2 2 2 0 0 0 2 1 1 2 2 0 1 0 0 0 1 2 2 0 2 0 1 2 0 0 1 1 0 1 1 0 1 2 0 2
2 2 0 0 2 1 2 2 0 1 1 1 1 2 0 0 0 1 0 2 2 1 1 2 0 0 2 2 0 1 0 1 2 2 2 0 1
1 2 0 0 1 2 1 2 2 0 1 1 1 1 2 1 0 1 1 2 2 0 0 1 0 1]

mglearn.discrete_scatter(x[:,0],x[:,1],kmeans.labels_,markers='o')
mglearn.discrete_scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],[0,1,2],markers='^',markeredgewidth=5)

在这里插入图片描述

fig,axes = plt.subplots(1,2,figsize=(10,5))
kmeans = KMeans(n_clusters=2)
kmeans.fit(x)
assignments = kmeans.labels_
mglearn.discrete_scatter(x[:,0],x[:,1],assignments,ax=axes[0])
kmeans = KMeans(n_clusters=5)
kmeans.fit(x)
assignments = kmeans.labels_
mglearn.discrete_scatter(x[:,0],x[:,1],assignments,ax=axes[1])

在这里插入图片描述

x_v,y_v = make_blobs(n_samples=200,cluster_std=[1.0,2.5,0.5],random_state=170)
y_p = KMeans(n_clusters=3,random_state=0).fit_predict(x_v)
mglearn.discrete_scatter(x_v[:,0],x_v[:,1],y_p)
plt.legend(['cluster 0','cluster 1','cluster 2'],loc='best')
plt.xlabel('feature 0')
plt.xlabel('feature 1')

在这里插入图片描述

x,y =make_blobs(random_state=170,n_samples=600)  #随机生成一些分组数据
rng = np.random.RandomState(74)
transformation = rng.normal(size=(2,2))  #变换数据使其拉长
x = np.dot(x,transformation)
kmeans = KMeans(n_clusters=3)
kmeans.fit(x)
y_p = kmeans.predict(x)
plt.scatter(x[:,0],x[:,1],c=y_p,cmap=mglearn.cm3)
plt.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],marker="^",c[0,1,2],s=100,linewidth=5,cmap=mglearn.cm3)
plt.xlabel('feature 0')
plt.xlabel('feature 1')

在这里插入图片描述

x,y = make_moons(n_samples=200,noise=0.05,random_state=0)
kmeans = KMeans(n_clusters=2)
kmeans.fit(x)
y_p = kmeans.predict(x)
plt.scatter(x[:,0],x[:,1],c=y_p,cmap=mglearn.cm2,s=60)
plt.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],marker="^",c=[mglearn.cm2(0),mglearn.cm2(1)],s=100,linewidth=5)
plt.xlabel('feature 0')
plt.xlabel('feature 1')

在这里插入图片描述

people = fetch_lfw_people(min_faces_per_person=20,resize=0.7) #灰度图像,按最小比例缩小以加快处理速度
image_shape = people.images[0].shape
counts = np.bincount(people.target)  #计算每个目标出现的次数
mask = np.zeros(people.target.shape,dtype=np.bool)
for target in np.unique(people.target):
    mask[np.where(people.target == target)[0][:50]] = 1  #每个人只取50张照片
x_people = people.data[mask]
y_people = people.target[mask]
x_people = x_people / 255  #将灰度值稳定在0~1之间,而不是0~255之间
x_train,x_test,y_train,y_test = train_test_split(x_people,y_people,stratify=y_people,random_state=0)
pca = PCA(n_components=2)
nmf = NMF(n_components=100,random_state=0)
nmf.fit(x_train)
kmeans = KMeans(n_clusters=100,random_state=0)
kmeans.fit(x_train)
x_reconstructed_pca = pca.inverse_transform(pca.transform(x_test))
x_reconstructed_kmeans = kmeans.cluster_centers_(kmeans.predict(x_test))
x_reconstructed_nmf = np.dot(nmf.transform(x_test),nmf_components_)
fig,axes = plt.subplots(3,5,figsize=(8,8),subplot_kw={'xticks':(),'yticks':()})
fig.suptitle("extracted components")
for ax,comp_keams,comp_pca,comp_nmf in zip(axes.T,kmeans.cluster_centers_,pca.components_,nmf.components_):
    ax[0].imshow(comp_kmeans.reshape(image_shape))
    ax[1].imshow(comp_pca.reshape(image_shape),cmap='viridis')
    ax[2].imshow(comp_nmf.reshape(image_shape))
axes[0,0].set_ylabel('kmeans')
axes[1,0].set_ylabel('pca')
axes[2,0].set_ylabel('nmf')
fig,axes = plt.subplots(4,5,figsize=(8,8),subplot_kw={'xticks':(),'yticks':()})
fig.suptitle("reconstructions")
for ax,orig,rec_kmeans,rec_pca,rec_nmf in zip(axes.T,x_test,x_reconstructed_kmeans,x_reconstructed_pca,x_reconstructed_nmf):
    ax[0].imshow(orig.reshape(image_shape))
    ax[1].imshow(rec_kmeans.reshape(image_shape))
    ax[2].imshow(rec_pca.reshape(image_shape))
    ax[3].imshow(rec_nmf.reshape(image_shape))
axes[0,0].set_ylabel('original')
axes[1,0].set_ylabel('kmeans')
axes[2,0].set_ylabel('pca')
axes[3,0].set_ylabel('nmf')
x,y = make_moons(n_samples=200,noise=0.05,random_state=0)
kmeans = KMeans(n_clusters=10,random_state=0)
kmeans.fit(x)
y_pred = kmeans.predict(x)
plt.scatter(x[:,0],x[:,1],c=y_pred,s=60,cmap='Paired')
plt.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],s=60,marker='^',c=range(kmeans.n_clusters),linewidth=2,cmap='Paired')
plt.xlabel('feature 0')
plt.ylabel('feature 1')
print("cluster memberships:{}".format(y_pred))

在这里插入图片描述

distance_features = kmeans.transform(x)
print("distance feature shape:{}".format(distance_features.shape))
print("distance feature:{}".format(distance_features))

distance feature shape:(200, 10)
distance feature:[[0.9220768 1.46553151 1.13956805 … 1.16559918 1.03852189 0.23340263]
[1.14159679 2.51721597 0.1199124 … 0.70700803 2.20414144 0.98271691]
[0.78786246 0.77354687 1.74914157 … 1.97061341 0.71561277 0.94399739]

[0.44639122 1.10631579 1.48991975 … 1.79125448 1.03195812 0.81205971]
[1.38951924 0.79790385 1.98056306 … 1.97788956 0.23892095 1.05774337]
[1.14920754 2.4536383 0.04506731 … 0.57163262 2.11331394 0.88166689]]

发布了65 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/heroybc/article/details/103531270