如何使numpy数组列的总和为1 [英] How to make numpy array column sum up to 1
问题描述
我正在构建用于实现PageRank算法的转换矩阵.我该如何使用numpy来确保这些列加起来.
I am working on building a transition matrix for implementing the PageRank algorithm. How could I use numpy to make sure that the columns add up to one.
例如:
1 1 1
1 1 1
1 1 1
应标准化为
.33 .33 .33
.33 .33 .33
.33 .33 .33
推荐答案
按列总和来划分各列的元素-
Divide the elements of each column by their column-summations -
a/a.sum(axis=0,keepdims=1) # or simply : a/a.sum(0)
为使行总和统一,请更改轴输入-
For making the row-summations unity, change the axis input -
a/a.sum(axis=1,keepdims=1)
样品运行-
In [78]: a = np.random.rand(4,5)
In [79]: a
Out[79]:
array([[ 0.37, 0.74, 0.36, 0.41, 0.44],
[ 0.51, 0.86, 0.91, 0.03, 0.76],
[ 0.56, 0.46, 0.01, 0.86, 0.38],
[ 0.72, 0.66, 0.56, 0.84, 0.69]])
In [80]: b = a/a.sum(axis=0,keepdims=1)
In [81]: b.sum(0) # Verify
Out[81]: array([ 1., 1., 1., 1., 1.])
要确保它也适用于Python 2.x的int
数组,请使用from __future__ import division
或使用
To make sure it works on int
arrays as well for Python 2.x, use from __future__ import division
or use np.true_divide
.
对于添加到0
For columns adding upto 0
对于加到0
的列,假设我们可以保留它们原样,我们可以将总和设置为1
,而不用像这样除以0
-
For columns that add upto 0
, assuming that we are okay with keeping them as they are, we can set the summations to 1
, rather than divide by 0
, like so -
sums = a.sum(axis=0,keepdims=1);
sums[sums==0] = 1
out = a/sums
这篇关于如何使numpy数组列的总和为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!