of kNN (parsed data from a text file)

# Prepare data: parse the data from a text file 
# Create a function named file2matrix kNN.py, handling input format issue
# input file name of the function is a string, the output is the training sample matrix and vector-based label
# text record of the resolver conversion Numpy
. 1  DEF file2matrix (filename):
 2      fr = Open (filename)
 . 3      arrayOLines = fr.readlines ()
 . 4      numberOfLines = len (arrayOLines)     # give the file number of rows 
. 5      returnMat = zeros ((numberOfLines,. 3))     # Create returned Numpy matrix 
. 6      classLabelVector = []
 . 7      index = 0
 . 8      for Line in arrayOLines:     # parse data list file 
. 9          Line = line.strip ()      # use line.strip () taken off all of the carriage return character 
10          listFromLine line.split = (' \ T ' )      # use the tab character \ t the entire row of data obtained in the previous step is divided into a list of elements 
. 11          returnMat [,: index] = listFromLine [0:. 3]       # Select the first three elements stored in the feature matrix 
12 is          classLabelVector.append (int (listFromLine [-1]))       # -1 represents the last one in the list elements stored in a vector classLabelVector 
13 is          index =. 1 +
 14          return returnMat, classLabelVector
1 出现的错误
2 >>>reload(kNN)
3 Traceback (most recent call last):
4   File "<input>", line 1, in <module>
5 NameError: name 'reload' is not defined
1 If your python version 2 is the Python .X:
 2  Import SYS
 . 3  reload (SYS)
 . 4 sys.setdefaultencoding ( " UTF-. 8 " )
 . 5  
. 6 If your python version 3.3 python :
 . 7  Import IMP
 . 8  imp.reload ( SYS)
 . 9  
10  Note:
 . 11 1.Python. 3 and Python 2 are very different, Python 3 wherein the system default is UTF- . 8 encoding.
12 2. So, for the case of using the Python 3, there is no need sys.setdefaultencoding ( " UTF-. 8 " ) code.
13 3 The most important thing is, Python sys library 3 which has no setdefaultencoding () function of.
14 
15 If your python version 3 python .4 and 3.4 above:
 16  Import importlib
 17  Import SYS
 18 importlib.reload (SYS))

 

Guess you like

Origin www.cnblogs.com/fd-682012/p/11564795.html