聚类------KNN

Posted 131415-520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了聚类------KNN相关的知识,希望对你有一定的参考价值。

import numpy as np
from math import sqrt
import operator as opt

def normData(dataSet):
maxVals = dataSet.max(axis=0)
minVals = dataSet.min(axis=0)
ranges = maxVals - minVals
retData = (dataSet - minVals) / ranges
return retData, ranges, minVals

def kNN(dataSet, labels, testData, k):
distSquareMat = (dataSet - testData) ** 2 # 计算差值的平方
distSquareSums = distSquareMat.sum(axis=1) # 求每一行的差值平方和
distances = distSquareSums ** 0.5 # 开根号,得出每个样本到测试点的距离
sortedIndices = distances.argsort() # 排序,得到排序后的下标
indices = sortedIndices[:k] # 取最小的k个
labelCount = {} # 存储每个label的出现次数
for i in indices:
label = labels[i]
labelCount[label] = labelCount.get(label, 0) + 1 # 次数加一
sortedCount = sorted(labelCount.items(), key=opt.itemgetter(1), reverse=True) # 对label出现的次数从大到小进行排序
return sortedCount[0][0] # 返回出现次数最大的label

if name == "main":
dataSet = np.array([[2, 3], [6, 8],[1,1],[3,4],[5,6]])
normDataSet, ranges, minVals = normData(dataSet)
labels = [‘a‘, ‘b‘,‘c‘,‘a‘,‘b‘]
testData = np.array([3.9, 5.5])
normTestData = (testData - minVals) / ranges
result = kNN(normDataSet, labels, normTestData, 2)
print(result)

调用库

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs

X为样本特征,Y为样本簇类别, 共1000个样本,每个样本2个特征,共4个簇,簇中心在[-1,-1], [0,0],[1,1], [2,2], 簇方差分别为[0.4, 0.2, 0.2]

X, y = make_blobs(n_samples=1000, n_features=2, centers=[[-1,-1], [0,0], [1,1], [2,2]], cluster_std=[0.4, 0.2, 0.2, 0.2],
random_state =9)
plt.scatter(X[:, 0], X[:, 1], marker=‘o‘)
plt.show()
from sklearn.cluster import KMeans
y_pred = KMeans(n_clusters=3, random_state=9).fit_predict(X)
plt.scatter(X[:, 0], X[:, 1], c=y_pred)
plt.show()
from sklearn import metrics
metrics.calinski_harabaz_score(X, y_pred)
from sklearn import metrics
metrics.calinski_harabaz_score(X, y_pred)

以上是关于聚类------KNN的主要内容,如果未能解决你的问题,请参考以下文章

聚类------KNN

KmeansKmeans++和KNN算法比較

在 Scikit 的谱聚类中使用预先计算的亲和矩阵时的 KNN?

如何使用 KNN /K-means 在数据框中对时间序列进行聚类

Python实验--手写KNN+PCA实现药品聚类和手写字识别

scikit-learn估计器-KNN均值聚类