如何在Python规范化 NumPy 中的数组?
在本文中,我们将讨论如何使用 NumPy 在Python规范化一维和二维数组。归一化是指将数组的值缩放到所需范围。
一维数组的归一化
假设,我们有一个数组 = [1,2,3] 并且在 [0,1] 范围内对其进行归一化意味着它将数组 [1,2,3] 转换为 [0, 0.5, 1] 为 1, 2和 3 是等距的。
Array [1,2,4] -> [0, 0.3, 1]
这也可以在范围内完成,即我们将使用 [3,7] 而不是 [0,1]。
现在,
Array [1,2,3] -> [3,5,7]
和
Array [1,2,4] -> [3,4.3,7]
让我们看代码示例
示例 1:
Python3
# import module
import numpy as np
# explicit function to normalize array
def normalize(arr, t_min, t_max):
norm_arr = []
diff = t_max - t_min
diff_arr = max(arr) - min(arr)
for i in arr:
temp = (((i - min(arr))*diff)/diff_arr) + t_min
norm_arr.append(temp)
return norm_arr
# gives range staring from 1 and ending at 3
array_1d = np.arange(1,4)
range_to_normalize = (0,1)
normalized_array_1d = normalize(array_1d,
range_to_normalize[0],
range_to_normalize[1])
# display original and normalized array
print("Original Array = ",array_1d)
print("Normalized Array = ",normalized_array_1d)
Python3
# import module
import numpy as np
# explicit function to normalize array
def normalize(arr, t_min, t_max):
norm_arr = []
diff = t_max - t_min
diff_arr = max(arr) - min(arr)
for i in arr:
temp = (((i - min(arr))*diff)/diff_arr) + t_min
norm_arr.append(temp)
return norm_arr
# assign array and range
array_1d = [1, 2, 4, 8, 10, 15]
range_to_normalize = (0, 1)
normalized_array_1d = normalize(
array_1d, range_to_normalize[0],
range_to_normalize[1])
# display original and normalized array
print("Original Array = ", array_1d)
print("Normalized Array = ", normalized_array_1d)
Python3
# import module
import numpy as np
# explicit function to normalize array
def normalize_2d(matrix):
norm = np.linalg.norm(matrix)
matrix = matrix/norm # normalized matrix
return matrix
# gives and array staring from -2
# and ending at 13
array = np.arange(16) - 2
# coverts 1d array to a matrix
matrix = array.reshape(4, 4)
print("Simple Matrix \n", matrix)
normalized_matrix = normalize_2d(matrix)
print("\nSimple Matrix \n", normalized_matrix)
Python3
# import module
import numpy as np
def normalize_2d(matrix):
# Only this is changed to use 2-norm put 2 instead of 1
norm = np.linalg.norm(matrix, 1)
# normalized matrix
matrix = matrix/norm
return matrix
# gives and array staring from -2 and ending at 13
array = np.arange(16) - 2
# coverts 1d array to a matrix
matrix = array.reshape(4, 4)
print("Simple Matrix \n", matrix)
normalized_matrix = normalize_2d(matrix)
print("\nSimple Matrix \n", normalized_matrix)
输出:
示例 2:
现在,让输入数组为 [1,2,4,8,10,15],范围再次为 [0,1]
蟒蛇3
# import module
import numpy as np
# explicit function to normalize array
def normalize(arr, t_min, t_max):
norm_arr = []
diff = t_max - t_min
diff_arr = max(arr) - min(arr)
for i in arr:
temp = (((i - min(arr))*diff)/diff_arr) + t_min
norm_arr.append(temp)
return norm_arr
# assign array and range
array_1d = [1, 2, 4, 8, 10, 15]
range_to_normalize = (0, 1)
normalized_array_1d = normalize(
array_1d, range_to_normalize[0],
range_to_normalize[1])
# display original and normalized array
print("Original Array = ", array_1d)
print("Normalized Array = ", normalized_array_1d)
输出:
二维数组的归一化
为了规范化二维数组或矩阵,我们需要 NumPy 库。对于矩阵,一般的归一化是使用欧几里得范数或弗罗贝尼乌斯范数。
简单归一化的公式是
这里,v 是矩阵,|v|是行列式或也称为欧几里得范数。 v-cap 是归一化矩阵。
以下是实现上述内容的一些示例:
示例 1:
蟒蛇3
# import module
import numpy as np
# explicit function to normalize array
def normalize_2d(matrix):
norm = np.linalg.norm(matrix)
matrix = matrix/norm # normalized matrix
return matrix
# gives and array staring from -2
# and ending at 13
array = np.arange(16) - 2
# coverts 1d array to a matrix
matrix = array.reshape(4, 4)
print("Simple Matrix \n", matrix)
normalized_matrix = normalize_2d(matrix)
print("\nSimple Matrix \n", normalized_matrix)
输出:
示例 2:
我们还可以使用其他规范,如 1-norm 或 2-norm
蟒蛇3
# import module
import numpy as np
def normalize_2d(matrix):
# Only this is changed to use 2-norm put 2 instead of 1
norm = np.linalg.norm(matrix, 1)
# normalized matrix
matrix = matrix/norm
return matrix
# gives and array staring from -2 and ending at 13
array = np.arange(16) - 2
# coverts 1d array to a matrix
matrix = array.reshape(4, 4)
print("Simple Matrix \n", matrix)
normalized_matrix = normalize_2d(matrix)
print("\nSimple Matrix \n", normalized_matrix)
输出:
这样,我们就可以在Python中用 NumPy 进行归一化。