机器学习入门笔记聚类算法
sklearn # 聚类前 X = np . random . rand ( 100 , 2 ) plt . scatter ( X [ : , 0 ] , X [ : , 1 ] , marker = 'o' ) plt . show ( ) # 聚类后 kmeans = KMeans ( n_clusters = 2 ) . fit ( X ) label_pred = kmeans . labels_ plt . scatter ( X [ : , 0 ] , X [ : , 1 ] , c = label_pred ) plt . show ( ) k均值聚类 # -*- coding: UTF-8 -*- import matplotlib . pyplot as plt import numpy as np def distEclud ( vecA , vecB ) : ''' 欧氏距离计算函数 :param vecA: :param vecB: :return: float ''' dist = 0.0 # ========= show me your code ================== # here dist = np . sqrt ( np . sum ( vecA - vecB ) ** 2 ) # ========= show me your