我想在IPython笔记本上写一篇论文,但是遇到了显示格式的一些问题.说我有以下的数据帧df,有没有什么办法来格式化var1和var2成2位小数和var3成比例.
var1 var2 var3
id
0 1.458315 1.500092 -0.005709
1 1.576704 1.608445 -0.005122
2 1.629253 1.652577 -0.004754
3 1.669331 1.685456 -0.003525
4 1.705139 1.712096 -0.003134
5 1.740447 1.741961 -0.001223
6 1.775980 1.770801 -0.001723
7 1.812037 1.799327 -0.002013
8 1.853130 1.822982 -0.001396
9 1.943985 1.868401 0.005732
Run Code Online (Sandbox Code Playgroud)
内部数字不会乘以100,例如-0.0057 = -0.57%.
如果我有这样的numpy数组:
[2.15295647e+01, 8.12531501e+00, 3.97113829e+00, 1.00777250e+01]
Run Code Online (Sandbox Code Playgroud)
如何移动小数点并格式化数字,所以我最终得到一个像这样的numpy数组:
[21.53, 8.13, 3.97, 10.08]
Run Code Online (Sandbox Code Playgroud)
np.around(a, decimals=2)只给了我[2.15300000e+01, 8.13000000e+00, 3.97000000e+00, 1.00800000e+01]我不想要的东西,我还没有找到另一种方法.
如何打印3位小数的numpy数组?我尝试了,array.round(3)但它继续像这样打印6.000e-01.是否可以选择让它像这样打印:6.000?
我有一个解决方案print ("%0.3f" % arr),但我想要一个全局解决方案,即每次我想检查数组内容时都不这样做.
我正在使用Jupyter笔记本.我有一个非常宽的屏幕,但显示的输出(比如,我打印一个numpy数组)的格式就好像屏幕很窄.
我发现了一种增加细胞宽度的方法
from IPython.core.display import HTML
HTML("<style>.container { width:95% !important; }</style>")
Run Code Online (Sandbox Code Playgroud)
但这似乎只影响输入,而不是输出(见截图):
我已尝试设置linewidth选项numpy.set_printoptions,我尝试过设置numpy.core.arrayprint._line_width,没有...
编辑:使用matplotlib我可以使用命令设置绘图的宽度(我用魔法绘制在笔记本中%matplotlib inline)plt.rcParams['figure.figsize']=[X,Y].事实证明,我可以增加X以使曲线一直水平填充输出单元格.这意味着(我认为)原始问题是一numpy件事.
我目前有以下代码段:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy
from numpy import linalg
A = [[1,2,47,11],[3,2,8,15],[0,0,3,1],[0,0,8,1]]
S = [[113,49,2,283],[-113,0,3,359],[0,5,0,6],[0,20,0,12]]
A = numpy.matrix(A)
S = numpy.matrix(S)
numpy.set_printoptions(precision=2, suppress=True, linewidth=120)
print("S^{-1} * A * S")
print(linalg.inv(S) * A * S)
Run Code Online (Sandbox Code Playgroud)
产生这个输出:

是否有标准的方法来生成类似于以下的输出?我怎样才能得到这个输出?
[[ -1 -0.33 0 0]
[ 0 1 0 0]
[ 0 -648 4 0]
[ 0 6.67 0 5]]
Run Code Online (Sandbox Code Playgroud)
有什么不同?
i的第一个字符之间至少有两个空格i+1,但如果需要更多,则可能更多(NumPy输出生成两个空格)BetterPythonConsole混乱它)-0但是00.但是0编辑 …
考虑以下三个DataFrame:
df1 = pd.DataFrame([[1,2],[4,3]])
df2 = pd.DataFrame([[1,.2],[4,3]])
df3 = pd.DataFrame([[1,'a'],[4,3]])
Run Code Online (Sandbox Code Playgroud)
以下是第二列的类型DataFrame:
In [56]: map(type,df1[1])
Out[56]: [numpy.int64, numpy.int64]
In [57]: map(type,df2[1])
Out[57]: [numpy.float64, numpy.float64]
In [58]: map(type,df3[1])
Out[58]: [str, int]
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,所有int的都是铸造的numpy.int64.精细.在第三种情况下,基本上没有铸造.但是,在第二种情况下,整数(3)被转换为numpy.float64; 可能因为其他数字是浮点数.
我怎样才能控制铸件?在第二种情况下,我希望有[float64, int64]或[float, int]作为类型.
使用可调用打印功能可以有一个替代方案来显示在这里.
def printFloat(x):
if np.modf(x)[0] == 0:
return str(int(x))
else:
return str(x)
pd.options.display.float_format = printFloat
Run Code Online (Sandbox Code Playgroud) np.set_printoptions允许自定义numpy 数组的漂亮打印。然而,对于不同的用例,我希望有不同的打印选项。
理想情况下,无需每次都重新定义整个选项即可完成此操作。我正在考虑使用本地范围,例如:
with np.set_printoptions(precision=3):
print my_numpy_array
Run Code Online (Sandbox Code Playgroud)
但是,set_printoptions似乎不支持with语句,因为会抛出错误(AttributeError: __exit__)。有没有什么方法可以在不创建自己的漂亮打印类的情况下完成这项工作?我知道我可以创建自己的上下文管理器:
class PrettyPrint():
def __init__(self, **options):
self.options = options
def __enter__(self):
self.back = np.get_printoptions()
np.set_printoptions(**self.options)
def __exit__(self, *args):
np.set_printoptions(**self.back)
Run Code Online (Sandbox Code Playgroud)
并将其用作:
>>> print A
[ 0.29276529 -0.01866612 0.89768998]
>>> with PrettyPrint(precision=3):
print A
[ 0.293 -0.019 0.898]
Run Code Online (Sandbox Code Playgroud)
然而,有没有比创建新类更直接的东西(最好已经内置)?
我正在尝试使用set_printoptions问题的答案如何在没有科学记数法和给定精度的情况下漂亮地打印 numpy.array?
但我收到这个错误:
Traceback (most recent call last):
File "neural_network.py", line 57, in <module>
output.set_printoptions(precision=3)
AttributeError: 'numpy.ndarray' object has no attribute 'set_printoptions'
Run Code Online (Sandbox Code Playgroud)
显然,并非所有numpy数组都是一样的,适用于常规数组的方法numpy.array不适用于numpy.ndarray.
如何格式化 anumpy.ndarray以进行打印,例如删除科学记数法?
更新
更改调用可numpy.set_printoptions()消除错误,但对 ndarray 内容的打印格式没有影响。
假设我有数组:
import numpy as np
x = np.array([1.2334, 2.3455, 3.456], dtype=np.float32)
并要打印:
print('{:.2f}%'.format(x))
它给了我:
unsupported format string passed to numpy.ndarray.__format__
我最近注意到,NumPy ndarays的Python打印功能并不一致.例如,它水平打印水平1D数组:
import numpy as np
A1=np.array([1,2,3])
print(A1)
#--> [1 2 3]
Run Code Online (Sandbox Code Playgroud)
但垂直冗余括号的一维水平阵列:
A2=np.array([[1],[2],[3]])
print(A2)
#--> [[1]
# [2]
# [3]]
Run Code Online (Sandbox Code Playgroud)
水平一维垂直阵列:
A3=np.array([[1,2,3]])
print(A3)
#--> [[1 2 3]]
Run Code Online (Sandbox Code Playgroud)
和一个2D数组:
B=np.array([[11,12,13],[21,22,23],[31,32,32]])
print(B)
# --> [[11 12 13]
# [21 22 23]
# [31 32 32]]
Run Code Online (Sandbox Code Playgroud)
第一个维度现在是垂直的.对于更高的尺寸,它会变得更糟,因为它们都是垂直打印的:
C=np.array([[[111,112],[121,122]],[[211,212],[221,222]]])
print(C)
#--> [[[111 112]
# [121 122]]
#
# [[211 212]
# [221 222]]]
Run Code Online (Sandbox Code Playgroud)
在我看来,一致的行为是水平打印偶数尺寸,垂直打印奇数尺寸.使用Unicode字符可以很好地格式化它.我想知道是否有可能创建一个函数来打印上面的数组:
A1 --> [1 2 3]
A2 --> ???????????
? 1 2 3 ?
???????????
A3 --> ????? # \u250c\u2500\u2510 …Run Code Online (Sandbox Code Playgroud) 我在Python中有一个numpy浮点数组.
当我打印数组时,第一个值是:
[7.14519700e+04, ....
Run Code Online (Sandbox Code Playgroud)
但是,如果我打印出它自己的第一个值,则打印输出为:
71451.9699799
Run Code Online (Sandbox Code Playgroud)
显然这些数字应该是相同的,所以我只是想知道,数组只是向我展示了该元素的圆形版本?这里的第二个数字有12个有效数字,第一个数字只有9个.
我想我只是想知道为什么这些数字不同?
我正在尝试从文件中生成一个数组numpy.genfromtxt。
文件是这样的:
16.37.235.200|59009|514|16.37.235.153|
17.37.235.200|59009|514|18.37.235.153|
Run Code Online (Sandbox Code Playgroud)
我得到一个像这样的数组:
['16.37.235.200' '17.37.235.200']
Run Code Online (Sandbox Code Playgroud)
但我希望数组是这样的:
[16.37.235.200,17.37.235.200]
Run Code Online (Sandbox Code Playgroud) 可能重复:
带有浮点数的Python舍入错误
我用numpy创建了一个数组a = numpy.arange(0,1e5,1,dtype=int).a[18645]是预期的18645.当我创建另一个数组时b=a*10e-15,b[18645]是186.4999999999e-12.b[18644]是186.44e-12.为什么Python会创建这些尾随9?
当我试图在数组中搜索元素时出现了这个问题numpy.where.使用尾随的9s,该numpy.where功能未能找到184.45e-12 in b.
numpy ×12
python ×12
arrays ×5
formatting ×2
pandas ×2
pretty-print ×2
css ×1
gedit ×1
genfromtxt ×1
ipython ×1
python-3.x ×1
unicode ×1