Python(jupyter notebook)--K-means聚类实例

Step1:导入所需的模块 

import csv
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import preprocessing
from mpl_toolkits.mplot3d import axes3d

Step2:导入所需数据

nodedata=pd.read_csv('node.data.txt')
newnodedata=nodedata.loc[0:9][["population","Total industrial output value","Consumption of material assets"]]
newnodedata#读取指定的行和列,多个列同时读取要加[]

Python(jupyter notebook)--K-means聚类实例_第1张图片

Step3:数据预处理

dataset_array=np.genfromtxt('node.data.txt',delimiter=',',skip_header=1,usecols=(1,2,3))#跳过第一行表头,读取有效的三列数据,以数组形式存储,不可删除
print(dataset_array)

Python(jupyter notebook)--K-means聚类实例_第2张图片 Step4:搭建K_Means模型求解(暂取簇数为3,即n_clusters=3)

from sklearn.cluster import KMeans
KMeans_model = KMeans(n_clusters=3,random_state = 9)
KMeans_model.fit(newnodedata)
#质心
print(KMeans_model.cluster_centers_)
#每个样本的标签
print(KMeans_model.labels_)
#SSE
print(KMeans_model.inertia_)

 Python(jupyter notebook)--K-means聚类实例_第3张图片

Step5:可视化

from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
labels = KMeans_model.labels_
fig = plt.figure()
#ax = Axes3D(fig)
ax = plt.subplot(projection = '3d')  # 创建一个三维的绘图工程
ax.set_title('Classification results-3D_image_show')  # 设置本图名称
color = ["dodgerblue", "seagreen", "lightcoral"]
for i in range(3):#range(i)=0,1,...,i-1
    d = newnodedata[labels == i]
    ax.scatter(d["population"], d["Total industrial output value"], d["Consumption of material assets"], color=color[i], label=f"type{i}")
    city_list=nodedata["city"]
    print(city_list[d.index])
#坐标轴标记 
ax.set_xlabel("population")
ax.set_ylabel("Total industrial output value")
ax.set_zlabel("Consumption of material assets")
# 使用plt.legend()函数展示图例
plt.legend()
# 展示图像
plt.show()

 Python(jupyter notebook)--K-means聚类实例_第4张图片

Step6: 利用肘部法则,确定合适的簇数,即确定n_clusters的值

from sklearn.cluster import KMeans
SSE_list=[]
for i in range(1,10):
    KMeans_model = KMeans(n_clusters=i,random_state = 1)
    KMeans_model.fit(dataset_array)
    SSE_list.append(KMeans_model.inertia_)
clusters_list=[1,2,3,4,5,6,7,8,9]
plt.plot(clusters_list, SSE_list,c='dodgerblue', linewidth=1.0)
plt.title('Elbow diagram')
plt.xlabel('n_clusters')
plt.ylabel('SSE')
plt.show()

Python(jupyter notebook)--K-means聚类实例_第5张图片

OVER!!!!!! 

你可能感兴趣的:(聚类,python,jupyter)