ValueError: could not convert string to float: X

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kuheli1991
    New Member
    • Feb 2014
    • 1

    ValueError: could not convert string to float: X

    i have the following code:
    Code:
    from numpy import *
    import operator
    
    def createDataSet():
        groups = array([[1.0,1.1],[1.0,1.0],[0,0],[0,.1]])
        labels = ['A','A','B','B']
        return groups, labels
                      
    def classify0(inX,dataSet,labels,k):
        dataSetSize = dataSet.shape[0]
        print(dataSetSize)
        diffMat = tile(inX,(dataSetSize,1)) - dataSet
        print(diffMat)
        sqDiffMat = diffMat**2
        print(sqDiffMat)
        sqDistances = sqDiffMat.sum(axis=1)
        print(sqDistances)
        distances = sqDistances**0.5
        sortedDistIndicies = distances.argsort()
        print(sortedDistIndicies)
        classCount = {}
        for i in range(k) :
            print i
            voteIlabel = labels[sortedDistIndicies[i]]
            print(voteIlabel)
            classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
            print(classCount)
            sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),
                                      reverse = True)
            print(sortedClassCount)
        return sortedClassCount[0][0]
    
    def file2matrix(filename):
        fr = open(filename)
        arrayOLines = fr.readlines()
        numberOfLines = len(arrayOLines)            #get the number of lines in the file
        returnMat = (zeros((numberOfLines,3)))        #prepare matrix to return
        classLabelVector = []                       #prepare labels return
        print numberOfLines
        
        index = 0
        for line in arrayOLines:
            line = line.strip()
            listFromLine = line.split('\t')
            print index
    ##        print listFromLine
    ##        returnMat[index ,:] = array(listFromLine[0])
            returnMat[index,:] =['X','Y','Z']
    ##        returnMat[index ,2] = listFromLine[2]
    ##        returnMat[index ,3] = listFromLine[3]
            classLabelVector.append(listFromLine[0])
            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))   #element wise divide
        return normDataSet, ranges, minVals
    
    the other code is:
    import my_bayes as kNN
    datingDataMat,datingLabels = kNN.file2matrix('train.txt')
    ##print datingLabels
    ##print datingDataMat
    the error is comming:
    Traceback (most recent call last):
    File "D:\python_fold er\python_2002\ run_mybayes.py" , line 2, in <module>
    datingDataMat,d atingLabels = kNN.file2matrix ('train.txt')
    File "D:\python_fold er\python_2002\ my_bayes.py", line 48, in file2matrix
    returnMat[index,:] =['X','Y','Z']
    ValueError: could not convert string to float: X
    Last edited by bvdet; Feb 20 '14, 08:06 AM. Reason: Please use code tags when posting code [code]....[/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The array initialized by numpy.zeros expects float values. You get the same error if you did this:
    Code:
    >>> float('A')
    Traceback (most recent call last):
      File "<interactive input>", line 1, in <module>
    ValueError: could not convert string to float: A
    >>>

    Comment

    Working...