python 不省略打印tensor、numpy、panda,在终端中打印numpy数组时避免行的换行显示

1.python 不省略打印tensor、numpy、panda

1.1 tensor

Torch 在输出tensor的时候,如何指定输出全部,不要省略号略过中间值:

import torch
torch.set_printoptions(threshold=np.inf)

1.2 numpy

import numpy as np
np.set_printoptions(threshold=np.inf)

1.3 panda

import pandas as pd
import numpy as np
# 读入数据
# file=pd.read_csv('xx/xx.csv')
# 显示所有列
pd.set_option('display.max_columns',None)
# 显示所有行
pd.set_option('display.max_rows',None)
# print(file)

2. 在终端中打印numpy数组时避免行的换行显示

如果想要在终端中打印numpy数组时避免行的换行显示,可以通过numpy库中set_printoptions()函数来设置打印选项。具体而言,可以通过以下代码将行的长度设置为无限长:

import numpy as np

np.set_printoptions(linewidth=np.inf)

这样就可以在终端中完整地打印出每一行的数组元素,而不会被强制换行。需要注意的是,在行的长度非常长的情况下,仍然可能会因为终端的限制而出现换行的情况。

示例代码

import numpy as np

# 生成一个5*10的随机numpy数组
arr = np.random.rand(5, 10)

# 根据第7列的元素进行排序
sorted_indices = np.argsort(arr[:, 6])
sorted_arr = arr[sorted_indices]

np.set_printoptions(linewidth=np.inf)
print(arr)
print("--------------------------------------------------")
print(sorted_arr)

终端显示

[[0.53314666 0.35818599 0.59959925 0.88474035 0.95127956 0.97831137 0.46238493 0.78752953 0.83468549 0.67344474]
 [0.87146505 0.48066711 0.48043843 0.73656315 0.5832972  0.60851666 0.83613473 0.20333798 0.5285668  0.91508689]
 [0.44630399 0.36273763 0.74271366 0.0736185  0.15834205 0.21510501 0.32342147 0.32050069 0.59226777 0.24213576]
 [0.66495676 0.1029413  0.95818064 0.20183019 0.73862478 0.59040146 0.914301   0.0908743  0.94314664 0.72390731]
 [0.28262758 0.21237755 0.49626219 0.60756675 0.54575291 0.08104765 0.31249599 0.08567969 0.29187514 0.87043817]]
--------------------------------------------------
[[0.28262758 0.21237755 0.49626219 0.60756675 0.54575291 0.08104765 0.31249599 0.08567969 0.29187514 0.87043817]
 [0.44630399 0.36273763 0.74271366 0.0736185  0.15834205 0.21510501 0.32342147 0.32050069 0.59226777 0.24213576]
 [0.53314666 0.35818599 0.59959925 0.88474035 0.95127956 0.97831137 0.46238493 0.78752953 0.83468549 0.67344474]
 [0.87146505 0.48066711 0.48043843 0.73656315 0.5832972  0.60851666 0.83613473 0.20333798 0.5285668  0.91508689]
 [0.66495676 0.1029413  0.95818064 0.20183019 0.73862478 0.59040146 0.914301   0.0908743  0.94314664 0.72390731]]
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值