数字差值最大的属性对计算结果的影响最大,每年获取的飞行常客里程数对于计算结果的影响远大于其他两项,但这三项的重要性是一致的,为了使三项数值在结果上是一致的,所以采用的方法是数值归一化,将取值范围处理为0到1或者-1到1之间。
公式:
newValue = (oldValue-min)/(max-min)
最小的值为0,最大的值为1,中间的数按大小占不同的比值
新函数autoNorm()
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))
return normDataSet,ranges,minVals
问题
>>> import kNN
>>> normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
AttributeError: module 'kNN' has no attribute 'autoNorm'
>>> import kNN
>>> from imp import reload
>>> reload(kNN)
<module 'kNN' from 'E:\\Python\\kNN.py'>
>>> normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
Traceback (most recent call last):
File "<pyshell#43>", line 1, in <module>
normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
AttributeError: module 'kNN' has no attribute 'autoNorm'
AttributeError: module ‘kNN’ has no attribute ‘autoNorm’
看到有的博客说是重启idle 就重新run shell
>>> import kNN
>>> normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
AttributeError: module 'kNN' has no attribute 'autoNorm'
>>> import kNN
>>> from imp import reload
>>> reload(kNN)
<module 'kNN' from 'E:\\Python\\kNN.py'>
>>> normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
AttributeError: module 'kNN' has no attribute 'autoNorm'
>>>
========================= RESTART: E:\Python\kNN.py =========================
>>> import kNN
>>> from imp import reload
>>> reload(kNN)
<module 'kNN' from 'E:\\Python\\kNN.py'>
>>> normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
NameError: name 'datingDataMat' is not defined
>>>
还是不行,就重新run Module,然后报错就变为NameError: name ‘datingDataMat’ is not defined
然后
>>> import kNN
>>> from imp import reload
>>> reload(kNN)
<module 'kNN' from 'E:\\Python\\kNN.py'>
>>> datingDataMat,datingLabels = kNN.file2matrix('datingTestSet.txt')
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
datingDataMat,datingLabels = kNN.file2matrix('datingTestSet.txt')
AttributeError: module 'kNN' has no attribute 'file2matrix'
>>>
以为没有调用文件的问题,再继续找问题
>>> import kNN
>>> from imp import reload
>>> reload(kNN)
<module 'kNN' from 'E:\\Python\\kNN.py'>
>>> datingDataMat,datingLabels = kNN.file2matrix('datingTestSet.txt')
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
datingDataMat,datingLabels = kNN.file2matrix('datingTestSet.txt')
File "E:\Python\kNN.py", line 25, in file2matrix
fr = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'datingTestSet.txt'
>>> datingDataMat,datingLabels = kNN.file2matrix('datingTestSet2.txt')
>>>
KeyboardInterrupt
>>> normMat,ranges,minVals = kNN.autoNorm(datingDataMat)
>>> normMat
array([[0.44832535, 0.39805139, 0.56233353],
[0.15873259, 0.34195467, 0.98724416],
[0.28542943, 0.06892523, 0.47449629],
...,
[0.29115949, 0.50910294, 0.51079493],
[0.52711097, 0.43665451, 0.4290048 ],
[0.47940793, 0.3768091 , 0.78571804]])
>>> ranges
array([9.1273000e+04, 2.0919349e+01, 1.6943610e+00])
>>> minVals
array([0. , 0. , 0.001156])
>>>
把原来的 file2matrix给注释了,所以没找到,而且在调用的时候还把2忘记了,太粗心了,这次总算是实现了