NumPy学习笔记(4)--数据归一化

# 归一化,将矩阵规格化到0-1之间

import numpy as np
a = 10*np.random.random((5, 5)) # 新建5*5矩阵做演示
print(a)
print('---')
amin, amax = a.min(), a.max() # 求最大最小值
a = (a-amin)/(amax-amin) # (矩阵元素-最小值)/(最大值-最小值)
print(a)
# 输出 [[ 2.36293248  2.28110919  6.16223037  2.82108853  3.82572016]
 [ 8.38507584  3.97018851  8.68038671  4.55166216  7.41336474]
 [ 8.641908    6.63312921  2.58191811  2.79359388  4.41776143]
 [ 2.14213006  8.24702055  4.69514129  3.44458659  2.33541662]
 [ 2.60246377  0.52364881  8.36609653  2.0713082   1.73721125]]
---
[[ 0.22549255  0.21546118  0.69127899  0.28166158  0.40482744]
 [ 0.96379547  0.42253898  1.          0.4938265   0.8446656 ]
 [ 0.99528259  0.74901026  0.25233976  0.27829079  0.47741054]
 [ 0.19842261  0.94687016  0.51141676  0.35810122  0.22211916]
 [ 0.25485862  0.          0.96146864  0.18973999  0.14878036]]

你可能感兴趣的:(Python)