jupyternotebook中美观显示矩阵的方法-创新互联

这篇文章主要讲解了jupyter notebook中美观显示矩阵的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

成都创新互联是一家专注于成都网站设计、成都网站建设与策划设计,昭阳网站建设哪家好?成都创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:昭阳等地区。昭阳做网站价格咨询:18982081108

我就废话不多说了,还是直接看代码吧!

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

分享题目:jupyternotebook中美观显示矩阵的方法-创新互联
转载来于:http://lszwz.com/article/jggss.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款