如何按列和按行将我的数组标准化为 0 和 1
How to normalize my array between 0 and 1 by column and by line
我有一个数组,需要以结果为 0 到 1 之间的数字的方式对其进行规范化。我已经对整个数组进行了规范化,如下所示:
C = A / A.max(axis=0)
print(C)
____________________________________________________________________
[[0. 0.05263158 0.1 0.14285714 0.18181818 0.2173913 ]
[0.33333333 0.36842105 0.4 0.42857143 0.45454545 0.47826087]
[0.66666667 0.68421053 0.7 0.71428571 0.72727273 0.73913043]
[1. 1. 1. 1. 1. 1. ]]
但现在我必须按列和按行进行标准化。我怎样才能通过轴减少来做到这一点?如果有比我做的更好的方法,建议我修改。
我的预期结果是两个数组,其值已标准化。一个考虑列,另一个考虑行
这是我的数据
A = [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
My expected result is two arrays with the values normalized. One considering the columns and the other by the lines
a = np.array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
如果
c = a / a.max(axis=0)
然后给你你想要的列
d = a / a.max(axis=1)[:,None]
行就够了。
>>> d.round(4)
array([[0. , 0.2 , 0.4 , 0.6 , 0.8 , 1. ],
[0.5455, 0.6364, 0.7273, 0.8182, 0.9091, 1. ],
[0.7059, 0.7647, 0.8235, 0.8824, 0.9412, 1. ],
[0.7826, 0.8261, 0.8696, 0.913 , 0.9565, 1. ]])
你跳过了最小部分。通常 0-1 归一化要求从分母和分子中减去最小值。
https://stats.stackexchange.com/questions/70801/how-to-normalize-data-to-0-1-range
import numpy as np
A = np.matrix([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
(A-A.min(axis=1))/(A.max(axis=1)-A.min(axis=1))
(A-A.min(axis=0))/(A.max(axis=0)-A.min(axis=0))
我有一个数组,需要以结果为 0 到 1 之间的数字的方式对其进行规范化。我已经对整个数组进行了规范化,如下所示:
C = A / A.max(axis=0)
print(C)
____________________________________________________________________
[[0. 0.05263158 0.1 0.14285714 0.18181818 0.2173913 ]
[0.33333333 0.36842105 0.4 0.42857143 0.45454545 0.47826087]
[0.66666667 0.68421053 0.7 0.71428571 0.72727273 0.73913043]
[1. 1. 1. 1. 1. 1. ]]
但现在我必须按列和按行进行标准化。我怎样才能通过轴减少来做到这一点?如果有比我做的更好的方法,建议我修改。
我的预期结果是两个数组,其值已标准化。一个考虑列,另一个考虑行
这是我的数据
A = [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
My expected result is two arrays with the values normalized. One considering the columns and the other by the lines
a = np.array([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
如果
c = a / a.max(axis=0)
然后给你你想要的列
d = a / a.max(axis=1)[:,None]
行就够了。
>>> d.round(4)
array([[0. , 0.2 , 0.4 , 0.6 , 0.8 , 1. ],
[0.5455, 0.6364, 0.7273, 0.8182, 0.9091, 1. ],
[0.7059, 0.7647, 0.8235, 0.8824, 0.9412, 1. ],
[0.7826, 0.8261, 0.8696, 0.913 , 0.9565, 1. ]])
你跳过了最小部分。通常 0-1 归一化要求从分母和分子中减去最小值。 https://stats.stackexchange.com/questions/70801/how-to-normalize-data-to-0-1-range
import numpy as np
A = np.matrix([[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]])
(A-A.min(axis=1))/(A.max(axis=1)-A.min(axis=1))
(A-A.min(axis=0))/(A.max(axis=0)-A.min(axis=0))