from numpy import *

import operator

from os import listdir

import matplotlib.pyplot as plt


"""程序清单2-1 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.items(), key=operator.itemgetter(1), reverse=True)

    return sortedClassCount[0][0]


"""程序清单2-2 将文本记录录到Numpy的解析程序"""

def file2matrix(filename):

    fr = open(filename)

    numberOfLines = len(fr.readlines())         #get the number of lines in the file

    returnMat = zeros((numberOfLines,3))        #prepare matrix to return

    classLabelVector = []                       #prepare labels return

    fr = open(filename)

    index = 0

    for line in fr.readlines():

        line = line.strip()

        listFromLine = line.split('\t')

        returnMat[index, :] = listFromLine[0:3]

        classLabelVector.append(int(listFromLine[-1]))

        index += 1

    return returnMat, classLabelVector


"""程序清单2-3 归一化特征值"""

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))   #element wise divide

    return normDataSet, ranges, minVals


"""程序清单2-4 分类器针对约会网站的测试代码"""

def datingClassTest():

    hoRatio = 0.50      #hold out 10%

    datingDataMat,datingLabels = file2matrix('datingTestSet2.txt')       #load data setfrom file

    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],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)


"""程序清单2-5 约会网站预测函数"""

def classifyPerson():

    resultList = ["not at all", "in small doses", "in large doses"]

    percentTats = float(input("percentage of time spent playing video games?"))

    ffMiles = float((input("frequent flier miles earned per year?")))

    icecream = float(input("liters of ice cream consumed per year?"))

    datingDataMat, datingLabels = file2matrix("datingTestSet2.txt")

    normMat, ranges, minVals = autoNorm(datingDataMat)

    inArr = array([ffMiles, percentTats, icecream])

    classifierResult = classify0((inArr - minVals) / ranges, normMat, datingLabels, 3)

    print("You will probaly like this: ", resultList[classifierResult - 1])


if __name__ == "__main__":

    datingDataMat, datingLabels = file2matrix("datingTestSet2.txt") #注意教材上是错的

    # print (datingDataMat)

    # print(datingLabels[0:20])


    """分析数据"""

    #plt.scatter(datingDataMat[:,1],datingDataMat[:,2])

    """图2-3"""

    #plt.show()


    # plt.scatter(datingDataMat[:,1],datingDataMat[:,2],15*array(datingLabels), 15*array(datingLabels))

    # """图2-4"""

    # plt.show()


    """特征值归一化"""

    normat, ranges, minVals = autoNorm(datingDataMat)

    # print(normat)

    # print(ranges)

    # print(minVals)

    """测试"""

    #datingClassTest()


    classifyPerson()