這篇文章主要講解了jupyter notebook中美觀顯示矩陣的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
我就廢話不多說了,還是直接看代碼吧!
from IPython.display import display,Latex,Math
%matplotlib inline
from IPython.core.interactiveshell import InteractiveShell
sh = InteractiveShell.instance()
def number_to_str(n,cut=5):
ns=str(n)
format_='{0:.'+str(cut)+'f}'
if 'e' in ns or ('.' in ns and len(ns)>cut+1):
return format_.format(n)
else:
return str(n)
def matrix_to_latex(mat,style='bmatrix'):
if type(mat)==np.matrixlib.defmatrix.matrix:
mat=mat.A
head=r'\begin{'+style+'}'
tail=r'\end{'+style+'}'
if len(mat.shape)==1:
body=r'\\'.join([str(el) for el in mat])
return head+body+tail
elif len(mat.shape)==2:
lines=[]
for row in mat:
lines.append('&'.join([number_to_str(el) for el in row])+r'\\')
s=head+' '.join(lines)+tail
return s
return None
sh.display_formatter.formatters['text/latex'].type_printers[np.ndarray]=matrix_to_latex輸入后運行即可
我們在進行矩陣打印的時候就相當美觀咯?。?!

補充知識:解決python numpy 大數組顯示不全的問題
import numpy as np
np.set_printoptions(threshold=np.inf)
或者
np.set_printoptions(threshold='nan')
其中threshold表示:
Total number of array elements to be print(輸出數組的元素數目)
看完上述內容,是不是對jupyter notebook中美觀顯示矩陣的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。