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