2

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.

[[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:

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

However, I keep getting this matrix below when the division occurs 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].

I feel like I'm missing something with the dimensions of the matrices, but I can't figure it out.

Suggestions?

4
  • 1
    Is this python-2.x? Commented May 21, 2017 at 20:09
  • Yes. Python 2.7.12 Commented May 21, 2017 at 20:12
  • 1
    In Python-2.x / is integer division for integers. You can use matrix.astype(np.float)/sum_vector to get floating point results. Commented May 21, 2017 at 20:14
  • Ah ... I missed that. Thanks! Commented May 21, 2017 at 20:16

2 Answers 2

3

In Python2, dividing an integer by another integer yields an integer result, which will be rounded down to the nearest integer value. Numpy follows the same convention. 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.

To perform float division instead, you need to cast one or both of your input arrays to a floating point dtype, e.g. 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. You can also get the new-style division behaviour in Python2 by importing division from __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
Sign up to request clarification or add additional context in comments.

Comments

2

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. And when dividing integers with / in Python 2, integer division is performed.

In Python 3, the behavior is changed, so that even if both the operands are integers, the / operator will generate a floating point answer. Instead // must be used to specify that integer division is what is actually wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.