views:

93

answers:

3

In numpy there is a function that makes arrays print prettier.

set_printoptions(suppress = True)

In other words, instead of this:

array([[  0.00000000e+00,  -3.55271368e-16,   0.00000000e+00,
          1.74443793e-16,   9.68149172e-17],
       [  5.08273978e-17,  -4.42527959e-16,   1.57859836e-17,
          1.35982590e-16,   5.59918137e-17],
       [  3.00000000e+00,   6.00000000e+00,   9.00000000e+00,
          2.73835608e-16,   7.37061982e-17],
       [  2.00000000e+00,   4.00000000e+00,   6.00000000e+00,
          4.50218574e-16,   2.87467529e-16],
       [  1.00000000e+00,   2.00000000e+00,   3.00000000e+00,
          2.75582605e-16,   1.88929494e-16]])

You get this:

array([[ 0., -0.,  0.,  0.,  0.],
       [ 0., -0.,  0.,  0.,  0.],
       [ 3.,  6.,  9.,  0.,  0.],
       [ 2.,  4.,  6.,  0.,  0.],
       [ 1.,  2.,  3.,  0.,  0.]])

How do I make this setting permanent so it does this whenever I'm using IPython?

A: 

Well, one way to streamline it would be to create a wee module somewhere on your $PYTHONPATH, printopts say, containing:

import numpy
numpy.set_printoptions(suppress = True)

And then import that whenever you want to change the printing. You could also import numpy in your code as from printopts import numpy. That way you'd only need one import statement.

Cheers.

ADDENDUM: A solution for interactive use only is to set the $PYTHONSTARTUP environment variable to the path to the printopts.py file. The interpreter executes that file before anything else, when in interactive mode. Of course, then python will always load numpy, hurting start times.

Having considered a little more, what I would do is create a module, say np.py containing

from numpy import *
set_printoptions(supress=True)

Then always import np to get your modified version, for the reason in my comment below.

If you don't mind a hack, just add the set_printoptions() call to the numpy/__init__.py file, but you have to have write access to the numpy installation and remember to repeat the hack when you update python or numpy. I don't think this is a good idea.

Rupert Nash
That's not any simpler than, in an ipython -pylab session, typing "set" and pressing the Up arrow to get the command I typed last time. I want this to be in place *always*.
endolith
Well, if you always import numpy as with `from printopts import numpy` that will work. But ok - you made me google with that -1!!! See edit to answer.
Rupert Nash
Actually, I think this is simpler that doing the ipython thing and better than what I was saying in the `<strike>`-ed para above. Anyway, I think you should be careful about changing the default behaviour of a widely used package: other modules or scripts you use may assume the default behaviour and not give expected results. In your own code, that won't be released to others go ahead but remind yourself that you're doing something non-standard with the different import statement.
Rupert Nash
+2  A: 

You can add those to your ipythonrc file (located in ~/.ipython on Unix). You'd need the lines:

import_mod numpy
execute numpy.set_printoptinos(suppress = True)

You can also add it to a custom profile or use another configuration method:

http://ipython.scipy.org/doc/stable/html/config/customization.html

DopplerShift
I think ipythornc is deprecated, ipy_user_conf.py is preferred?
dwf
Dwf, add that as a separate answer?
endolith
I had heard that it was deprecated, but decided to suggest it anyways because:1) The 0.10 docs for IPython (last stable) don't mention it being deprecated2) It seemed slightly more straightforward than ipy_user_conf.py
DopplerShift
Beware of http://ipython.scipy.org/doc/manual/html/config/ipython.htmlwhich, a level up, says "Starting with version 0.11, IPython has a completely new configuration system ..." arrrrgh
Denis
A: 

I added this to the main() function in ~/.ipython/ipy_user_conf.py:

from numpy import set_printoptions
set_printoptions(suppress = True)

and it seems to work. It might not be the best place for it, but I'll update this answer later if I find out that it's wrong.

endolith