0

I've a matrix with this shape:

>>> A = [ [12.11432, 10.00211, 9.44100],[0.12361, 5511.13478, 189.79823] ]

A is sparse matrix, I used exactly lil_matrix. I want to divide each element of A by the sum of the all elements. The result of this division must be a matrix B with the same precision as A.

>>> from scipy.sparse import lil_matrix
>>> import numpy as np
>>> A = np.array([ [12.11432, 10.00211, 9.44100],[0.12361, 5511.13478, 189.79823] ])
>>> A = lil_matrix(A)
>>> B = A / A.sum()
>>> B.toarray()
array([[  2.11322791e-03,   1.74477296e-03,   1.64689266e-03],
        [  2.15625889e-05,   9.61365048e-01,   3.31084961e-02]])

As you can see, the precision between A and B isn't the same.
So, How can I keep only 5 digit after the decimal point in the B matrix?

Hocine Ben
  • 2,079
  • 2
  • 14
  • 20
  • 3
    1) That's not how floating point numbers work, and 2) You're just looking at the printed representation of the array object – YXD Feb 27 '13 at 10:58
  • 3) This seems to be about scipy, not PIL. – Junuxx Feb 27 '13 at 11:03
  • 3
    @HocineBen: See [this question](http://stackoverflow.com/questions/2891790/pretty-printing-of-numpy-array) to see how to change the way Numpy prints arrays, including precision and scientific notation. Does `np.set_printoptions(precision=5, suppress=True); B.toarray()` do what you want? – Junuxx Feb 27 '13 at 11:06
  • @Junuxx: my problem is not in how display my matrix, but I want that my elements in my matrix have exactly the precision = 5 – Hocine Ben Feb 27 '13 at 11:12
  • Maybe you just want `.round(5)` then. – nneonneo Feb 27 '13 at 11:17
  • @nneonneo: How I can do it? can you give a simple code? – Hocine Ben Feb 27 '13 at 11:23
  • I get this error: AttributeError: round not found – Hocine Ben Feb 27 '13 at 11:28
  • It's `np.round(B,5)`, but I'm not convinced it's very useful to do this. Why do you want this rounding? – Junuxx Feb 27 '13 at 11:32
  • my data is very sensitive, they are cancerous image data. – Hocine Ben Feb 27 '13 at 11:36
  • I get this error with 'np.round(B,5)': Traceback (most recent call last): File "", line 1, in File "/usr/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 2295, in round_ return _wrapit(a, 'round', decimals, out) File "/usr/lib/python2.7/site-packages/numpy/core/fromnumeric.py", line 37, in _wrapit result = getattr(asarray(obj),method)(*args, **kwds) AttributeError: rint – Hocine Ben Feb 27 '13 at 11:39
  • 1
    Strange reason to *reduce* the precision. I think you're on the wrong track. The error is because you try to round a sparse matrix. `np.round(B.toarray(),5)`. – Junuxx Feb 27 '13 at 11:45
  • 1
    Let me put it this way: You shouldn't be bothered by the internal representation of a float, *unless* it's **not precise enough** OR because you have so many floats that you would **run out of memory** at the current precision. *Too precise because your data is very sensitive* makes little sense. You can always present rounded end results by using `np.set_print_options` as I mentioned several comments back. – Junuxx Feb 27 '13 at 11:53
  • 2
    @Junuxx: thank you, maybe you're right, I'll keep the precions and when viewing, I'll use np.set_print_options – Hocine Ben Feb 27 '13 at 12:04

0 Answers0