Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set max output width in numpy?

I am using a Jupyter notebook. I have a pretty wide screen, but the displayed output (say, when I print a numpy array) is formatted as if the screen was narrow.

I found a way of increasing the width of the cells, with

from IPython.core.display import HTML HTML("<style>.container { width:95% !important; }</style>") 

but this seems to influence the input only, not the output (see screenshots):

short input longer input

I've tried setting the linewidth option in numpy.set_printoptions, I've tried setting numpy.core.arrayprint._line_width, nothing...

EDIT: Using matplotlib I can set the width of plots (that I plot in the notebook with the magic %matplotlib inline) with the command plt.rcParams['figure.figsize']=[X,Y]. It turns out that I can increase X to have plots fill the output cell horizontally all the way. This means (I think) that the original problem it's a numpy thing.

like image 649
Ziofil Avatar asked May 10 '16 22:05

Ziofil


People also ask

What is set_ printoptions in NumPy?

numpy.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, suppress=None, nanstr=None, infstr=None, formatter=None)[source] Set printing options. These options determine the way floating point numbers, arrays and other NumPy objects are displayed.

How do you limit the number of items printed in output of a NumPy array?

To limit the values of the NumPy array ndarray to given range, use np. clip() or clip() method of ndarray . By specifying the minimum and maximum values in the argument, the out-of-range values are replaced with those values. This is useful when you want to limit the values to a range such as 0.0 ~ 1.0 or 0 ~ 255 .

How do I print a full NP array?

We can use the threshold parameter of the numpy. set_printoptions() function to sys. maxsize to print the complete NumPy array.


2 Answers

I found this answer helpful in creating my own:

import numpy as np np.set_printoptions(edgeitems=30, linewidth=100000,      formatter=dict(float=lambda x: "%.3g" % x)) 

The absurd linewidth means only edgeitems and the window's width will determine when newlines/wrapping occurs.

enter image description here

If I shrink the window a bit, it looks like this, so you may still need to play with the edgeitems or formatting:

enter image description here

Here are the docs for set_printoptions, of which the following are relevant:

  • edgeitems : Number of array items in summary at beginning and end of each dimension (default 3).

  • linewidth : The number of characters per line for the purpose of inserting line breaks (default 75).

like image 172
DharmaTurtle Avatar answered Sep 24 '22 23:09

DharmaTurtle


This is a year old now but maybe the answer will help someone else.

The way numpy-arrays are displayed depends on a number of things. With this code, you can show more items and use the full width of your screen:

This is the default

import numpy as np np.set_printoptions(edgeitems=3) np.core.arrayprint._line_width = 80  >>> array([[[0, 0, 0, ..., 0, 0, 0], >>>    [0, 0, 0, ..., 0, 0, 0], >>>    [0, 0, 0, ..., 0, 0, 0], >>>    ...,  

With the following code you increase the items shown at the edge of each array (start and end) as well as the line width:

import numpy as np np.set_printoptions(edgeitems=10) np.core.arrayprint._line_width = 180  >>> array([[[  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], 
like image 29
Frank Herfert Avatar answered Sep 22 '22 23:09

Frank Herfert