k-means聚类及可视化

本文主要是通过sklearn库实现k-means聚类并可视化

1.导入库

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.cluster import DBSCAN

2.读取数据并可视化

ori_data = pd.read_csv('D:/DL/机器学习/data.csv',header = None)

k-means聚类及可视化_第1张图片

可视化

x = ori_data.iloc[:,0]
y = ori_data.iloc[:,1]
plt.scatter(x, y,s=4)
#二维坐标可视化
plt.show()

 k-means聚类及可视化_第2张图片

3.实现聚类并可视化

all_points = []
colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'b']
i = 0 
while i < len(x):
    point = [x[i],y[i]]
    all_points.append(point)
    i+=1
# 调用KMeans方法, 聚类数为4个,fit()之后开始聚类
kmeans = KMeans(n_clusters=4).fit(all_points)
plt.title('kmeans')
print('all_points_len: ',len(all_points))
print('k_label_len:',len(kmeans.labels_))
print(all_points[0][0],all_points[0][1])
for id, label in enumerate(kmeans.labels_):
    plt.scatter(all_points[id][0], all_points[id][1], color = colors[label],marker='o',s=4)
plt.show() 

k-means聚类及可视化_第3张图片

你可能感兴趣的:(机器学习)