jupyter notebook中美观显示矩阵实例


Posted in Python onApril 17, 2020

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

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(输出数组的元素数目)

以上这篇jupyter notebook中美观显示矩阵实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python网页请求urllib2模块简单封装代码
Feb 07 Python
Python中类型检查的详细介绍
Feb 13 Python
Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能示例
Mar 22 Python
利用Python代码实现数据可视化的5种方法详解
Mar 25 Python
python合并同类型excel表格的方法
Apr 01 Python
python批量查询、汉字去重处理CSV文件
May 31 Python
kali中python版本的切换方法
Jul 11 Python
django项目中使用手机号登录的实例代码
Aug 15 Python
Django配置MySQL数据库的完整步骤
Sep 07 Python
python+OpenCV实现车牌号码识别
Nov 08 Python
Python中openpyxl实现vlookup函数的实例
Oct 28 Python
浅谈Python响应式类库RxPy
Jun 14 Python
Python3将ipa包中的文件按大小排序
Apr 17 #Python
利用pyecharts读取csv并进行数据统计可视化的实现
Apr 17 #Python
pyecharts动态轨迹图的实现示例
Apr 17 #Python
Windows下Anaconda安装、换源与更新的方法
Apr 17 #Python
Python openpyxl 插入折线图实例
Apr 17 #Python
python 画图 图例自由定义方式
Apr 17 #Python
关于python 的legend图例,参数使用说明
Apr 17 #Python
You might like
附件名前加网站名
2008/03/23 PHP
php循环输出数据库内容的代码
2008/05/24 PHP
PHP 网页过期时间的控制代码
2009/06/29 PHP
浅析PHP Socket技术
2013/08/02 PHP
zend framework框架中url大小写问题解决方法
2014/08/19 PHP
php批量添加数据与批量更新数据的实现方法
2014/12/16 PHP
codeigniter发送邮件并打印调试信息的方法
2015/03/21 PHP
JS 按钮点击触发(兼容IE、火狐)
2013/08/07 Javascript
常用的JavaScript验证正则表达式汇总
2013/11/26 Javascript
Node.js文件操作详解
2014/08/16 Javascript
jquery动态分页效果堪比时光网
2014/09/25 Javascript
angularJS结合canvas画图例子
2015/02/09 Javascript
自定义jQuery插件方式实现强制对象重绘的方法
2015/03/23 Javascript
关于在Servelet中如何获取当前时间的操作方法
2016/06/28 Javascript
jQuery实现复制到粘贴板功能
2017/02/11 Javascript
详解js的作用域、预解析机制
2018/02/05 Javascript
深入浅析Vue.js计算属性和侦听器
2018/05/05 Javascript
JavaScript获取时区实现过程解析
2020/09/24 Javascript
openlayers实现图标拖动获取坐标
2020/09/25 Javascript
Python中你应该知道的一些内置函数
2017/03/31 Python
Tensorflow 利用tf.contrib.learn建立输入函数的方法
2018/02/08 Python
python制作简单五子棋游戏
2019/06/18 Python
Pandas透视表(pivot_table)详解
2019/07/22 Python
Python实现图像的垂直投影示例
2020/01/17 Python
python实现ftp文件传输系统(案例分析)
2020/03/20 Python
python爬取代理ip的示例
2020/12/18 Python
使用CSS3实现圆角,阴影,透明
2014/12/23 HTML / CSS
计算机专业毕业生求职信分享
2013/12/24 职场文书
餐厅楼面部长岗位职责范文
2014/02/16 职场文书
护士毕业实习感言
2014/03/05 职场文书
助学感谢信范文
2015/01/21 职场文书
接待员岗位职责
2015/02/13 职场文书
护理自荐信
2019/05/14 职场文书
Go语言 go程释放操作(退出/销毁)
2021/04/30 Golang
mysql 带多个条件的查询方式
2021/06/05 MySQL
浅谈音视频 pts dts基本概念及理解
2022/08/05 数码科技