简体   繁体   English

numpy矩阵除法返回全零

[英]Numpy matrix division returning all zeros

I have a division error with the matrix below. 我下面的矩阵有一个除法错误。 I want to divide the 10 x 10 matrix , with a 10 x 1 vector of the sum of the rows. 我想将10 x 10 matrix除以行总和的10 x 1向量。

[[5731,    3,   20,    8,   12,   54,   46,    8,   39,    2],
 [   2, 6472,   47,   24,    7,   44,    7,   11,  116,   12],
 [  55,   36, 5296,  104,   84,   27,  106,   53,  183,   14],
 [  50,   49,  132, 5312,    2,  253,   36,   58,  142,   97],
 [  16,   28,   36,    9, 5381,   11,   55,   24,   85,  197],
 [  62,   45,   30,  181,   77, 4631,  117,   28,  161,   89],
 [  33,   23,   37,    1,   43,   92, 5642,    4,   42,    1],
 [  20,   20,   74,   23,   55,   14,    4, 5788,   18,  249],
 [  52,  155,   74,  143,   14,  170,   50,   30, 5036,  127],
 [  43,   32,   32,   85,  174,   40,    2,  197,   79, 5265]]

The sum_vector of the rows: 行的sum_vector

[[5923],
 [6742],
 [5958],
 [6131],
 [5842],
 [5421],
 [5918],
 [6265],
 [5851],
 [5949]]

However, I keep getting this matrix below when the division occurs matrix / sum_vector 但是,当除法发生matrix / sum_vector时,我一直在下面获取此矩阵

[[0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0]]

I've tried this post and this post , but still getting all zeros. 我已经尝试了这篇文章和这篇文章 ,但仍然得到全零。

I can divide the matrix by a scalar and it will return correctly. 我可以将矩阵除以标量,然后正确返回。 However it is something with my division that is returning all zeros. 但是,我的部门正在返回全零。

I've also tried np.divide(matrix, sum_vector.reshape((10,1))) and matrix / matrix.sum(axis=1)[:,None] . 我还尝试了np.divide(matrix, sum_vector.reshape((10,1)))matrix / matrix.sum(axis=1)[:,None]

I feel like I'm missing something with the dimensions of the matrices, but I can't figure it out. 我觉得我在矩阵的维度上缺少了一些东西,但是我无法弄清楚。

Suggestions? 有什么建议吗?

Integer division is somewhat unintuitive before one gets used to it. 在习惯之前,整数除法有点不直观。 The result should stay an integer, thus having no decimals. 结果应保留为整数,因此没有小数。 And the way Python handles that is to round down to the nearest smaller integer. Python处理的方法是向下舍入到最接近的较小整数。 And when dividing integers with / in Python 2, integer division is performed. 并且在Python 2中用/除整数时,将执行整数除法。

In Python 3, the behavior is changed, so that even if both the operands are integers, the / operator will generate a floating point answer. 在Python 3中,该行为已更改,因此,即使两个操作数都是整数, /运算符也会生成浮点答案。 Instead // must be used to specify that integer division is what is actually wanted. 相反,必须使用//来指定整数除法是实际需要的。

In Python2, dividing an integer by another integer yields an integer result, which will be rounded down to the nearest integer value. 在Python2中,将一个整数除以另一个整数会产生一个整数结果,该结果将四舍五入为最接近的整数值。 Numpy follows the same convention. numpy遵循相同的约定。 Since all the values in your sum_vector are larger than all the values in matrix then the result will be an array of zeros. 由于sum_vector中的所有值都大于matrix所有值,因此结果将是零数组。

To perform float division instead, you need to cast one or both of your input arrays to a floating point dtype , eg result = matrix.astype(np.double) / sum_vector . 为了执行除法浮动代替,则需要投你输入阵列中的一个或两个为浮点dtype ,例如result = matrix.astype(np.double) / sum_vector

The situation is different in Python3 , where / performs float division by default, and // (the floor division operator ) is used if you want an integral result. 情况与Python3不同,在Python3中/默认执行浮点除法,如果需要积分结果,则使用//底数除法运算符 )。 You can also get the new-style division behaviour in Python2 by importing division from __future__ : 您还可以通过从__future__导入division获得__future__的新型除法行为:

In [1]: 5 / 2
Out[1]: 2

In [2]: from __future__ import division

In [3]: 5 / 2
Out[3]: 2.5

In [4]: 5 // 2  # floor division operator
Out[4]: 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM