在计算欧氏距离的过程中,数值较大的属性对结果的贡献大,如果认为不同属性权重应该相同的话,就需要将数值归一化处理。
from numpy import * def autoNorm(dataset): minVals=dataset.min(0) #取每列的最小值,返回数组 print(minVals) maxVals=dataset.max(0) print(maxVals) ranges=maxVals-minVals #ranges是每列的最大值与最小值之差组成的数组 print(ranges) normDataset=zeros(shape(dataset)) print(normDataset) m=dataset.shape[0] #取dataset的行数 normDataset=dataset-tile(minVals,(m,1)) print(normDataset) normDataset=normDataset/tile(ranges,(m,1)) print(ranges) return normDataset
minVals=dataset.min(0) #取数据集每列的最小值,返回数组
from numpy import * dataset=array(([6,5], [3,1000], [10,300])) print(dataset.min()) #返回所有元素中的最小值 print(dataset.min(0)) #返回每列的最小值组成的数组 print(dataset.min(1)) #返回每行的最小值组成的数组 输出: 3 [3 5] [ 5 3 10]
m=dataset.shape[0] #取dataset的行数
from numpy import * dataset=array(([6,5], [3,1000], [10,300])) print(dataset.shape) #返回数据集的“形状”,行数和列数 print(dataset.shape[0]) #返回行数 print(dataset.shape[1]) #返回列数 输出: (3, 2) 3 2