1、分类器实现
import numpy as np
import operator
def KNN(inx, dataset, labels, k):
'''inx:待测元素;dataset:已知数据集(Numpy格式);labels:已知数据集元素对应的类别;
k:与待测点距离最近的点的个数'''
'''距离计算(欧几里德距离)'''
datasetsize = dataset.shape(0)
diffmat = np.tile(inx, (datasetsize, 1)) - dataset
sqdiffmat = diffmat**2
sqdistances = sqdiffmat.sum(axis=1)
distances = sqdistances**0.5
'''选择距离最小的k个点'''
sortlist = distances.argsort()
classcount = {}
for i in range(k):
votelabel = labels[sortlist[i]]
classcount[votelabel] = classcount.get(votelabel,0) + 1 #生成各个标签的直方图
'''降序排序'''
sortclasscount = sorted(classcount.iteritems(),
key=operator.itemgetter(1), reverse=True)
return sortclasscount[0][0]
2、文档处理,将tidy data(数据清洗部分本文不做说明):
def file2matrix(filename):
fr = open(filename)
alines = fr.readlines()
lines_num = len(alines)
returnmat = np.zeros((lines_num, 3))
labelsvector = []
index = 0
for line in alines:
line_list = line.strip().split('\t')
returnmat[index] = line_list[0:3]
labelsvector.append(int(line_list[-1]))
index += 1
return returnmat, labelsvector
3、数据归一化,将不同取值范围的特征值转化为0到1的范围,也可以根据不同权重进行分配,以下为统一转化为0到1范围的代码:
def newvalue(dataset):
minval = dataset.min(0)
maxval = dataset.max(0)
ranges = maxval - minval
newdataset = np.zeros(np.shape(dataset))
m = dataset.shape[0]
newdataset = dataset - np.tile(minval, (m,1))
newdataset = newdataset/np.tile(ranges, (m,1))
return newdataset, ranges, minval
newdataset, ranges, minval = newvalue(returnmat)
4、分类器检测,kNN算法是机器学习中最简单的,对训练样本的预处理要求较高,错误率也通常较高,需要进行检测后才能使用分类器中的训练样本:
def datingClassTest():
hoRatio = 0.10 #hold out 10%
datingDataMat,datingLabels = file2matrix('datingTestSet2.txt') #load data setfrom file
newdataset, ranges, minval = newvalue(datingDataMat)
m = newdataset.shape[0]
numTestVecs = int(m*hoRatio)
errorCount = 0.0
for i in range(numTestVecs):
classifierResult = KNN(newdataset[i,:],newdataset[numTestVecs:m,:],datingLabels[numTestVecs:m],3)
print "the classifier came back with: %d, the real answer is: %d" % (classifierResult, datingLabels[i])
if (classifierResult != datingLabels[i]): errorCount += 1.0
print "the total error rate is: %f" % (errorCount/float(numTestVecs))
print errorCount