主要代码:KNN.py
'''
优点:精度高、对异常值不敏感、无数据输入假定
缺点:计算复杂度高、空间复杂度高
适用数据范围:数值型和标称型
'''
from numpy import *
import operator #运算符模块
def createDataSet():
group = array([[1.0, 1.1], [1.0, 1.0], [0, 0], [0, 0.1]])
labels = ['A', 'A', 'B', 'B']
return group, labels
def classify0(inX, dataSet, labels, k):
'''
:param inX: 输入要分类的向量
:param dataSet: 已有的数据分类
:param labels: 分类标签
:param k: 选择就近的k个分类
:return:
'''
dataSetSize = dataSet.shape[0] #求行数
diffmat = tile(inX, (dataSetSize, 1)) - dataSet #复制成样本数据的个数,然后减去
sqDiffmat = diffmat**2 #每个位置平方
sqDistances = sqDiffmat.sum(axis = 1) #行求和
distances = sqDistances ** 0.5 #开根号
sortedDistIndicise = distances.argsort() #排序,返回的坐标值
classCount = {} #创建字典
for i in range(k):
voteIlabel = labels[sortedDistIndicise[i]] #获取标签
classCount[voteIlabel] = classCount.get(voteIlabel, 0) + 1 #标签计数
sortedClassCount = sorted(classCount.items(), key = operator.itemgetter(1),\
reverse = True)
classCount.items()
#将classCount字典分解为元组列表,operator.itemgetter(1)
#按照第二个元素的次序对元组进行排序,reverse = True是逆序,即按照从大到小的顺序排列
return sortedClassCount[0][0]
def file2matrix(filename):
fr = open(filename)
arrayOLines = fr.readlines()
numberOfLines = len(arrayOLines)
returnMat = zeros((numberOfLines, 3))
classLabelVector = []
index = 0
for line in arrayOLines:
line = line.strip() #除去所有回车符
listFromLine = line.split('\t') #将整行数据分割成一个元素列表
returnMat[index, :] = listFromLine[0 : 3] #取前三个数据
classLabelVector.append(int(listFromLine[-1])) #取标签
index += 1
return returnMat, classLabelVector
def autoNorm(dataSet):
'''
标准化公式:newValue = (oldValue - min) / (max - min)
'''
minVals = dataSet.min(0) #0表示每一列的最小值
maxVals = dataSet.max(0)
ranges = maxVals - minVals #
normDataSet = zeros(shape(dataSet))
m = dataSet.shape[0]
normDataSet = dataSet - tile(minVals, (m, 1))
normDataSet = normDataSet / tile(ranges, (m, 1))
return normDataSet, ranges, minVals
#测试准确率
def datingClassTest(filename):
hoRatio = 0.10 #测试百分之十的数据
datingDataMat, datingLabels = file2matrix(filename) #读取数据
normMat, ranges, minVals = autoNorm(datingDataMat) #标准化
m = normMat.shape[0]
numTestVecs = int(m * hoRatio)
errorCount = 0.0
for i in range(numTestVecs):
classfierResult = classify0(normMat[i, :], normMat[numTestVecs: m, :], \
datingLabels[numTestVecs : m], 4)
print('the classifier came back with: %d, the real answer is: %d'\
%(classfierResult, datingLabels[i]))
if(classfierResult != datingLabels[i]):
errorCount += 1.0
print('the total error rate is:%d' % (errorCount / float(numTestVecs)))
测试导入数据:利用KNN.createDataSet()测试导入数据,以及KNN.file2matrix()导入分类数据。
data.txt为任意的分类数据
import KNN
group, labels = KNN.createDataSet()
print(KNN.classify0([0,0], group, labels, 3))
dataingDataMat, datingLabels = KNN.file2matrix('data.txt')
print(dataingDataMat, datingLabels)
在分类前,可以对数据进行分析,例如画图分析
# 创建散点图
fig = plt.figure()
ax = fig.add_subplot(111)
# 设置标题
ax.set_title('Scatter plot')
# 设置x轴标签
ax.set_xlabel("X")
# 设置y轴标签
ax.set_ylabel("Y")
ax.scatter(dataingDataMat[:, 0], dataingDataMat[:, 2], 15.0*array(datingLabels), \
15.0 * array(datingLabels))
plt.show()
对每个数据标签
fig = plt.figure()
ax = fig.add_subplot(111)
datingLabels = array(datingLabels)
idx_1 = where(datingLabels == 1)[0][:]#去坐标
ax.scatter(dataingDataMat[idx_1,0], dataingDataMat[idx_1,2], c='r', label='1',s=20,marker='*')
idx_2 = where(datingLabels == 2)[0][:]
ax.scatter(dataingDataMat[idx_2,0], dataingDataMat[idx_2,2], c='b', label='2',s=10,marker='o')
idx_3 = where(datingLabels == 3)[0][:]
ax.scatter(dataingDataMat[idx_3,0], dataingDataMat[idx_3,2], c='g', label='3',s=30,marker='+')
# 设置图标
plt.legend(loc='upper right')
plt.show()
最后对数据进行标准化,以及计算准确率
# 数据标准化
# normMat, ranges, minVals = KNN.autoNorm(dataingDataMat)
#计算正确率
#KNN.datingClassTest('data.txt')
如果对于画图部分的代码有问题可以参考以下几个大佬的博客: