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.

6
  • 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? Commented Apr 26, 2018 at 2: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 Commented Apr 26, 2018 at 2:44
  • Great. If you search on those keywords, you'll probably find a duplicate. Commented Apr 26, 2018 at 2:46
  • 1
    this one could help. stackoverflow.com/questions/48422762/… I will give an answer for my own question if is allowed as this is a duplicate Commented Apr 26, 2018 at 2: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. Commented Apr 26, 2018 at 2:59

1 Answer 1

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⎦⎦
Sign up to request clarification or add additional context in comments.

1 Comment

This solution perfectly fulfill my need. Thank you so much!

Your Answer

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