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中美观显示矩阵实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
全网最简约的Anaconda+Python3.7安装教程Win10
这篇文章主要介绍了全网最简约的Anaconda+Python3.7安装教程Win10,图文讲解全流程安装方法,还不会的小伙伴快来看看吧2023-03-03
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
这篇文章主要介绍了tensorflow 2.1.0 安装与实战(CASIA FACE v5),本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-06-06
Pycharm运行程序时,控制台输出PyDev console:starting问题
Pycharm运行程序时,控制台输出PyDev console:starting问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-05-05
python绘制子图技巧之plt.subplot、plt.subplots及坐标轴修改
一个图片里边绘制多个图像是绘图中的常见需求,下面这篇文章主要给大家介绍了关于python绘制子图技巧之plt.subplot、plt.subplots及坐标轴修改的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下2022-05-05


最新评论