Python does not omit printing tensor, numpy, and panda, and avoids line breaks when printing numpy arrays in the terminal

1.python does not omit printing tensor, numpy, panda

1.1 tensor

When Torch outputs tensor, how to specify to output all, do not skip the intermediate value with ellipsis:

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. Avoid line breaks when printing numpy arrays in the terminal

numpyIf you want to avoid line wrapping when printing an array in the terminal , you can set the printing options through the functions numpyin the library . set_printoptions()Specifically, the length of the line can be set to infinite length by the following code:

import numpy as np

np.set_printoptions(linewidth=np.inf)

In this way, the array elements of each line can be completely printed in the terminal without being forced to wrap. It should be noted that when the length of the line is very long, line breaks may still occur due to the limitation of the terminal.

sample code

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)

terminal display

[[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]]

Guess you like

Origin blog.csdn.net/BIT_HXZ/article/details/129476871