IndexError: tuple index out of range


錯誤代碼:

def loadDataSet(fileName):      #general function to parse tab -delimited floats
    dataMat = []                #assume last column is target value
    fr = open(fileName)
    for line in fr.readlines():
        curLine = line.strip().split('\t')
        fltLine = map(float,curLine) #map all elements to float()
        dataMat.append(fltLine)
    return dataMat

打印出dataset

[<map object at 0x0000022920841B70>, <map object at 0x0000022920841C50>, <map object at 0x0000022920841D68>, 。。。。。。

發現是一個個map對象

 shape(dataset)
(80,)
只有行數沒有列數

代碼修正:

def loadDataSet(fileName):      #general function to parse tab -delimited floats
    dataMat = []                #assume last column is target value
    fr = open(fileName)
    for line in fr.readlines():
        curLine = line.strip().split('\t')
        fltLine = list(map(float,curLine)) #map all elements to float()//將map對象轉為list列表
        dataMat.append(fltLine)
    return dataMat
shape(dataset)
(80, 2)





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM