最大值最小值归一化

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Fri Sep  7 16:28:20 2018
 4 
 5 @author: zhen
 6 """
 7 # 最大值最小值归一化:(X-Xmin)/(Xmax-Xmin)
 8 import numpy as np
 9 import matplotlib.pyplot as plt
10 
11 x = np.random.rand(100)
12 x = x.reshape(-1, 1)
13 
14 rand = np.random.rand(100) * np.random.randint(10)
15 rand = rand.reshape(-1, 1)
16 # 获取Xmax,Xmin
17 x_max = np.max(rand)
18 x_min = np.min(rand)
19 
20 result = []
21 # 查找数组的索引:np.where(rand == i)
22 for i in rand:
23     result.append((float(i[0]) - x_min)/(x_max - x_min))
24     
25 result = np.array(result, dtype = float)
26 result = result.reshape(-1, 1)
27 # 可视化
28 plt.plot(x, rand, "r.", label="native")
29 plt.plot(x, result, "b.", linewidth=2, label="normalized")
30 
31 plt.legend(loc="upper left")
32 plt.grid()
33 plt.show()

结果:

最大值最小值归一化_第1张图片

分析:可知,数据的离散性大大降低,数据之间的内聚性增加,数据更加密集!

你可能感兴趣的:(最大值最小值归一化)