这是一个使用 unicode 将矩阵显示为列的小函数。我不知道如何将其连接到 Jupyter:
>>> 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⎦⎦