机器学习之KNN算法

k-邻算法

优点:精度高,对一场之不敏感、无数据输入假定。

缺点:计算复杂度高,空间复杂度高。

适用数据范围:数值和标称型

原理:  输入一个新的没有标签的数据后,将新数据的每个特征值与训练样本集中数据的对应的特征进行比较,选择训练样本数据集中前K个最相似的数据,最后,选择K个最相似数据中出现次数最多的分类,作为新数据的分类。

#!/usr/bin/python
#-*-coding:utf-8-*-
import sys  
reload(sys)  
sys.setdefaultencoding('utf8') 

from numpy import *
import operator
from os import listdir


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

"""
首先取得数据集的行数,用当前数据构造一个相同的矩阵,对每一行平方,求平方和在开方。
排序,返回排序之后最小的k个数的下标,计算出现标签出现的次数,返回出现频率最高的一项。
"""
def classify0(inX, dataSet, labels, k):
	dataSetSize = dataSet.shape[0]
	diffMat = tile(inX, (dataSetSize,1))-dataSet
	sqDiffMat = diffMat**2
	sqDistances = sqDiffMat.sum(axis=1)
	distances = sqDistances**0.5
	sortedDistIndicies = distances.argsort()
	classCount={}
	for i in range(k):
		voteIlabel = labels[sortedDistIndicies[i]]
		classCount[voteIlabel] = classCount.get(voteIlabel,0)+1
	sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),reverse=True)
	return sortedClassCount[0][0]

"""
打开文件,读取行数,创建 行*3 矩阵,解析文件数据到列表,返回特征矩阵和类标集合
"""
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):
	minVals = dataSet.min(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

"""
1.读取文件到列表,归一化处理,调用分类函数,求得分类准确率
"""
def datingClassTest():
	hoRatio = 0.10
	datingDataMat,datingLabels = file2matrix('datingTestSet.txt')
	normMat,ranges,minVals = autoNorm(datingDataMat)
	m = normMat.shape[0]
	numTestVecs = int(m*hoRatio)
	errorCount = 0.0
	for i in range(numTestVecs):
		classifierResult = classify0(normMat[i,:],normMat[numTestVecs:m,:],datingLabels[numTestVecs:m],60)
		print ("the classifier came back with:%s,the real answer is:%s"%(classifierResult,datingLabels[i]))
		if(classifierResult != datingLabels[i]): errorCount+=1.0
	print ("the total error rate is:%f"%(errorCount/float(numTestVecs)))
	print (errorCount)


def classifyPerson():
	resultList = ['not at all','in small does','in large does']
	percentTat = float(raw_input("the time you playing game per years?"))
	ffMiles = float(raw_input("the mail you go?"))
	iceCream = float(raw_input("the iceCream you eat once year?"))
	datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')
	normMat,ranges,minVals = autoNorm(datingDataMat)
	inArr = array([percentTat,ffMiles,iceCream])
	classifierResult = classify0((inArr-minVals)/ranges,normMat,datingLabels,3)
	print ("you will like this person",resultList[classifierResult])


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值