jupyter notebook中美观显示矩阵实例

作者:卷曲的葡萄藤 时间:2023-06-06 18:13:35 

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


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

输入后运行即可

我们在进行矩阵打印的时候就相当美观咯!!!

jupyter notebook中美观显示矩阵实例

补充知识:解决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(输出数组的元素数目)

来源:https://blog.csdn.net/weixin_39043567/article/details/91979912

标签:jupyter,notebook,矩阵
0
投稿

猜你喜欢

  • Python闭包与装饰器原理及实例解析

    2023-04-08 19:44:30
  • Mysql服务器的安装配置与启动关闭方法详解

    2024-01-28 05:10:26
  • 浅谈python标准库--functools.partial

    2023-01-03 20:08:36
  • 详解OpenCV实现特征提取的方法

    2022-10-05 11:15:28
  • TensorFLow用Saver保存和恢复变量

    2021-09-18 12:17:17
  • eWebEditor不支持IE8的解决方法

    2009-11-02 10:59:00
  • Python爬虫scrapy框架Cookie池(微博Cookie池)的使用

    2023-03-25 00:27:03
  • ADO.NET实现对SQL Server数据库的增删改查示例

    2024-01-12 17:37:54
  • Python批量按比例缩小图片脚本分享

    2022-06-06 11:46:12
  • Python实现列表拼接和去重的三种方式

    2021-05-02 23:43:54
  • mySQL UNION运算符的默认规则研究

    2024-01-21 17:51:39
  • MySQL数据库Shell import_table数据导入

    2024-01-15 02:34:55
  • C#简单访问SQLite数据库的方法(安装,连接,查询等)

    2024-01-22 18:29:22
  • swfobject2.1居中问题

    2008-12-15 17:18:00
  • FckEditor配置手册中文教程详细说明

    2010-02-28 12:37:00
  • Go 热加载之fresh详解

    2024-03-23 14:27:26
  • 隐蔽的ASP后门 大家可以查看下

    2010-08-05 21:26:00
  • .NET Framework SQL Server 数据提供程序连接池

    2024-01-27 05:05:23
  • python 弹窗提示警告框MessageBox的实例

    2023-11-12 01:07:17
  • Python中的异常处理相关语句基础学习笔记

    2021-10-18 00:54:50
  • asp之家 网络编程 m.aspxhome.com