机器学习——k邻近算法的数据解析

时间:2023-02-03 21:25:04
def file2matrix(filename):
    #?????
    fr = open(filename)
    #??????????
    arrayOLines = fr.readlines()
    #??????????
    numberofLines = len(arrayOLines)
    #????numberofLines?????��??????
    returnMat = zeros((numberofLines,3))
    classLabelVector = []
    index = 0
    #for??????????????��?????
    for line in arrayOLines:
        #??line?????????
        line = line.strip()
        #?????????\t???
        listFormLine = line.split('\t')
        #??????? ?????��??????????
        returnMat [index,:] = listFormLine[0:3]
        #??listFormLine???????�՛�??classLabelVector?????  ???????int
        classLabelVector.append(int(listFormLine[-1]))
        index+=1
        #???????????????????????
    return returnMat,classLabelVector