2

I am studying DL on deeplearning.ai. In this course Andrew stacks training examples in columns like following.

enter image description here

where numpy actually print array like this.

enter image description here

My question is, how to customize the output of numpy as follow

enter image description here

that is, have the inner square brackets 'longer' to cover whole column.

any response would be appreciated.

Thanks to smci's inspiration, I got this.

from IPython.display import display, Math
display(Math(r'\begin{align}\quad\boldsymbol X=\begin{bmatrix}\begin{bmatrix}135 \\30 \\\end{bmatrix},\begin{bmatrix}57 \\15 \\\end{bmatrix},\begin{bmatrix}150 \\35 \\\end{bmatrix}\end{bmatrix}\end{align}'))

Another question shows up:

The latex notation such as {align} occupy the python format {}, how to solve this? Any response would be appreciated.

  • To be clear, you want to customize the display format (e.g. HTML/CSS, LateX), rather than the output (i.e. numerical values). Do you want to display this in a notebook, webpage, export to document? – smci Apr 26 '18 at 02:26
  • 1
    Both are great! Thank you! Your question inspired me. I will look for a way to print a LateX with python in jupyter notebook –  Apr 26 '18 at 02:44
  • Great. If you search on those keywords, you'll probably find a duplicate. – smci Apr 26 '18 at 02:46
  • 1
    this one could help. https://stackoverflow.com/questions/48422762/is-it-possible-to-show-print-output-as-latex-in-jupyter-notebook I will give an answer for my own question if is allowed as this is a duplicate –  Apr 26 '18 at 02:53
  • Sure, that's a direct duplicate. Please post your answer there. We can close this question as duplicate, so it redirects to that one. – smci Apr 26 '18 at 02:59
  • Possible duplicate of [Is it possible to show \`print\` output as LaTeX in jupyter notebook?](https://stackoverflow.com/questions/48422762/is-it-possible-to-show-print-output-as-latex-in-jupyter-notebook) – smci Apr 26 '18 at 03:00

1 Answers1

1

Here is a small function that uses unicode to display a matrix as columns. How to hook this into Jupyter I don't know:

>>> def pretty_col(data):
...     assert data.ndim == 1
...     if data.size <= 1:
...         return format(data)
...     else:
...         return format(data[:, None])[1:-1].replace('[', '\u23A1', 1).replace(' [', '\u23A2', data.size-2).replace(' [', '\u23A3').replace(']', '\u23A4', 1).replace(']', '\u23A5', data.size-2).replace(']', '\u23A6')
...
>>> def pretty_cols(data, comma=False):
...     assert data.ndim == 2
...     if comma:
...         return '\n'.join(line[0] + line + line[-1] for line in map(str.join, data.shape[0] // 2 * ('  ',) + (', ',) + (data.shape[0] - 1) // 2 * ('  ',), zip(*map(str.split, map(pretty_col, data.T), data.shape[1]*('\n',)))))
...     else:
...         return '\n'.join(line[0] + line + line[-1] for line in map(''.join, zip(*map(str.split, map(pretty_col, data.T), data.shape[1]*('\n',)))))

...
>>> print(pretty_cols(np.arange(-1, 2, 0.25).reshape(4, 3)))
⎡⎡-1.  ⎤⎡-0.75⎤⎡-0.5 ⎤⎤
⎢⎢-0.25⎥⎢ 0.  ⎥⎢ 0.25⎥⎥
⎢⎢ 0.5 ⎥⎢ 0.75⎥⎢ 1.  ⎥⎥
⎣⎣ 1.25⎦⎣ 1.5 ⎦⎣ 1.75⎦⎦
>>> 
>>> print(pretty_cols(np.arange(-1, 2, 0.25).reshape(4, 3), True))
⎡⎡-1.  ⎤  ⎡-0.75⎤  ⎡-0.5 ⎤⎤
⎢⎢-0.25⎥  ⎢ 0.  ⎥  ⎢ 0.25⎥⎥
⎢⎢ 0.5 ⎥, ⎢ 0.75⎥, ⎢ 1.  ⎥⎥
⎣⎣ 1.25⎦  ⎣ 1.5 ⎦  ⎣ 1.75⎦⎦
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99