By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

ValueError: could not convert string to float: X

P: 1
i have the following code:
Expand|Select|Wrap|Line Numbers
  1. from numpy import *
  2. import operator
  3.  
  4. def createDataSet():
  5.     groups = array([[1.0,1.1],[1.0,1.0],[0,0],[0,.1]])
  6.     labels = ['A','A','B','B']
  7.     return groups, labels
  8.  
  9. def classify0(inX,dataSet,labels,k):
  10.     dataSetSize = dataSet.shape[0]
  11.     print(dataSetSize)
  12.     diffMat = tile(inX,(dataSetSize,1)) - dataSet
  13.     print(diffMat)
  14.     sqDiffMat = diffMat**2
  15.     print(sqDiffMat)
  16.     sqDistances = sqDiffMat.sum(axis=1)
  17.     print(sqDistances)
  18.     distances = sqDistances**0.5
  19.     sortedDistIndicies = distances.argsort()
  20.     print(sortedDistIndicies)
  21.     classCount = {}
  22.     for i in range(k) :
  23.         print i
  24.         voteIlabel = labels[sortedDistIndicies[i]]
  25.         print(voteIlabel)
  26.         classCount[voteIlabel] = classCount.get(voteIlabel,0) + 1
  27.         print(classCount)
  28.         sortedClassCount = sorted(classCount.iteritems(),key=operator.itemgetter(1),
  29.                                   reverse = True)
  30.         print(sortedClassCount)
  31.     return sortedClassCount[0][0]
  32.  
  33. def file2matrix(filename):
  34.     fr = open(filename)
  35.     arrayOLines = fr.readlines()
  36.     numberOfLines = len(arrayOLines)            #get the number of lines in the file
  37.     returnMat = (zeros((numberOfLines,3)))        #prepare matrix to return
  38.     classLabelVector = []                       #prepare labels return
  39.     print numberOfLines
  40.  
  41.     index = 0
  42.     for line in arrayOLines:
  43.         line = line.strip()
  44.         listFromLine = line.split('\t')
  45.         print index
  46. ##        print listFromLine
  47. ##        returnMat[index ,:] = array(listFromLine[0])
  48.         returnMat[index,:] =['X','Y','Z']
  49. ##        returnMat[index ,2] = listFromLine[2]
  50. ##        returnMat[index ,3] = listFromLine[3]
  51.         classLabelVector.append(listFromLine[0])
  52.         index += 1
  53.     return returnMat,classLabelVector
  54.  
  55. def autoNorm(dataSet):
  56.     minVals = dataSet.min(0)
  57.     maxVals = dataSet.max(0)
  58.     ranges = maxVals - minVals
  59.     normDataSet = zeros(shape(dataSet))
  60.     m = dataSet.shape[0]
  61.     normDataSet = dataSet - tile(minVals, (m,1))
  62.     normDataSet = normDataSet/tile(ranges, (m,1))   #element wise divide
  63.     return normDataSet, ranges, minVals
  64.  
  65. the other code is:
  66. import my_bayes as kNN
  67. datingDataMat,datingLabels = kNN.file2matrix('train.txt')
  68. ##print datingLabels
  69. ##print datingDataMat
the error is comming:
Traceback (most recent call last):
File "D:\python_folder\python_2002\run_mybayes.py", line 2, in <module>
datingDataMat,datingLabels = kNN.file2matrix('train.txt')
File "D:\python_folder\python_2002\my_bayes.py", line 48, in file2matrix
returnMat[index,:] =['X','Y','Z']
ValueError: could not convert string to float: X
Feb 20 '14 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
The array initialized by numpy.zeros expects float values. You get the same error if you did this:
Expand|Select|Wrap|Line Numbers
  1. >>> float('A')
  2. Traceback (most recent call last):
  3.   File "<interactive input>", line 1, in <module>
  4. ValueError: could not convert string to float: A
  5. >>> 
Feb 20 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.