机器学习实战 4knn(书)终章

1.记录

机器学习实战 4knn(书)终章

机器学习实战 4knn(书)终章

 

机器学习实战 4knn(书)终章

from numpy import *
import operator


def classify0(inX, dataSet, labels, k):
    dataSetSize = dataSet.shape[0]
    diffMat = tile(inX, (dataSetSize,1)) - dataSet
    sqDiffMat = diffMat**2
    sqDisttances = sqDiffMat.sum(axis=1)
    distances = sqDisttances**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]
        
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

def datingClassTest():
     hoRatio = 0.10 
     datingDataMat,datingLabels = file2matrix('datingTestSet2.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],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)))   

def classifyPerson():
    resultList = ['not at all','in small doses','in large doses']
    percentTats = float(input(\
                 "percentage of time game?"))
    ffMiles = float(input("freguent 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 probably like this person:",\
                     resultList[classifierResult - 1])

这是shi2.py文件的

runfile('C:/Users/zy/.spyder-py3/site_packages/shi2.py', wdir='C:/Users/zy/.spyder-py3/site_packages')

import shi2

datingDataMat,datingLabels = kNN.file2matrix('datingTestSet2.txt')
Traceback (most recent call last):

  File "<ipython-input-3-e20e636c98d6>", line 1, in <module>
    datingDataMat,datingLabels = kNN.file2matrix('datingTestSet2.txt')

NameError: name 'kNN' is not defined




datingDataMat,datingLabels = shi2.file2matrix('datingTestSet2.txt')

normMat, ranges, minVals = shi2.autoNorm(datingDataMat)

normMat
Out[6]: 
array([[0.44832535, 0.39805139, 0.56233353],
       [0.15873259, 0.34195467, 0.98724416],
       [0.28542943, 0.06892523, 0.47449629],
       ...,
       [0.29115949, 0.50910294, 0.51079493],
       [0.52711097, 0.43665451, 0.4290048 ],
       [0.47940793, 0.3768091 , 0.78571804]])

ranges
Out[7]: array([9.1273000e+04, 2.0919349e+01, 1.6943610e+00])

minVals
Out[8]: array([0.      , 0.      , 0.001156])

shi2.datingClassTest()
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:3,the real answer is:3
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:3,the real answer is:3
the classifier came back with:3,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:3
the classifier came back with:1,the real answer is:1
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:3
the classifier came back with:3,the real answer is:3
the classifier came back with:2,the real answer is:2
the classifier came back with:1,the real answer is:1
the classifier came back with:3,the real answer is:1
the total error rate is:0.050000

shi2.classifyPerson()


percentage of time game?3000

freguent flier miles earned per year8000

liters of ice cream consumed per year?10
You will probably like this person: not at all

2.problem

机器学习实战 4knn(书)终章

解决:上一行的print括号不完整,右括号补上就好

 

上一篇:Halcon学习笔记-深度学习 preprocess_dl_classifier_images描述


下一篇:数据不均衡问题