RecSysDevCode
RecSysDevCode copied to clipboard
55页:小数定标标准化,j值计算错误
def DecimalScaling(self):
arr_ = list()
j = self.x_max // 10 if self.x_max % 10 == 0 else self.x_max // 10 + 1
for x in self.arr:
arr_.append(round(x / math.pow(10, j), 4))
print("经过Decimal Scaling标准化后的数据为:\n{}".format(arr_))
书中的代码只针对init函数中给出的数组有效,但不具有通用性,小数定标标准化修改之后的代码如下:
def DecimalScaling(self):
arr_ = list()
j = 1
x_max = max([abs(one) for one in self.arr])
while x_max / 10 >= 1.0:
j += 1
x_max = x_max / 10
for x in self.arr:
arr_.append(round(x / math.pow(10, j), 4))
print("经过Decimal Scaling标准化后的数据为:\n{}".format(arr_))
附:这里的代码不具有工程属性,因此不可能考虑到所有的异常,比如log函数的输入不能是负数,e^x中x如果太大的话,会造成int越界等。大家在使用时选择适合自己的标准化方式即可!