k- nearest neighbor (kNN) preparation of data: normalized value

1  # preparing the data: normalized value of 
2  DEF autoNorm (dataSet A):       # autoNorm () function can be automatically converted to a digital value wherein the interval from 0 to 1 
. 3      minVals = dataSet.min (0)
 . 4      maxVals dataSet.max = ( 0)     # ddataSet.max (parameter 00) is such that the minimum value of the function can be selected from a column 
. 5      Ranges = maxVals - minVals
 . 6      normDataSet = zeros (Shape (dataSet a))
 . 7      m = dataSet.shape [0]
 . 8      # newValue = (oldValue-min) / ( max-min), characterized in that the formula can be any value in the range is converted to a value in the interval 0 to. 1 
. 9      # the tile () function to copy the contents of a variable input matrix into a matrix of the same size (specific features divided value) 
10      #In numpy library, the matrix division function required linalg.solve (Mata, matB) 
. 11      normDataSet = dataSet A - the tile (minVals, (m,. 1 ))
 12 is      normDataSet = normDataSet / the tile (Ranges, (m,. 1 ))
 13 is      return normDataSet, ranges, minVals

operation result:

 1 >>>normMat, ranges, minVals = kNN.autoNorm(datingDataMat)
 2 >>>normMat
 3 array([[1., 1., 1.],
 4        [0., 0., 0.],
 5        [0., 0., 0.],
 6        ...,
 7        [0., 0., 0.],
 8        [0., 0., 0.],
 9        [0., 0., 0.]])
10 >>>ranges
11 array([4.092000e+04, 8.326976e+00, 9.539520e-01])
12 >>>minVals
13 array([0., 0., 0.])

Errors:

1 >>>normMat, ranges, minVals = kNN.autoNorm(datingDataMat)
2 Traceback (most recent call last):
3   File "<input>", line 1, in <module>
4 NameError: name 'kNN' is not defined
5 
6 >>>normMat, ranges, minVals = kNN.autoNorm(datingDataMat)
7 Traceback (most recent call last):
8   File "<input>", line 1, in <module>
9 AttributeError: module ', 'knn has no attribute 'autoNorm'

Solution:

  Personal Solution: Restart PyCharm, run kNN.py, re-enter the complete command to run, the problem will be solved

 1 >>>from numpy import *
 2 >>>random.rand(4,4)
 3 >>>randMat = mat(random.rand(4,4))
 4 >>>randMat.I
 5 >>>invRandMat = randMat.I
 6 >>>myEye = randMat*invRandMat
 7 >>>myEye - eye(4)
 8 >>>group,labels = kNN.createDataSet()
 9 >>>group
10 >>>labels
11 >>>kNN.classify0([0,0], group, labels, 3)
12 >>>datingDataMat,datingLabels = kNN.file2matrix('datingTestSet.txt')
13 >>>datingDataMat
14 >>>datingLabels[0:16]
15 >>>import matplotlib
16 >>>import matplotlib.pyplot as plt
17 >>>fig = plt.figure()
18 >>>ax = fig.add_subplot(111)
19 >>>ax.scatter(datingDataMat[:,1], datingDataMat[:,2])
20 >>>plt.show()
21 >>>normMat, ranges, minVals = kNN.autoNorm(datingDataMat)
22 >>>normMat
23 array([[1., 1., 1.],
24        [0., 0., 0.],
25        [0., 0., 0.],
26        ...,
27        [0., 0., 0.],
28        [0., 0., 0.],
29        [0., 0., 0.]])
30 >>>ranges
31 array([4.092000e+04, 8.326976e+00, 9.539520e-01])
32 >>>minVals
33 array([0., 0., 0.])

 

Guess you like

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