Python:美化数字和字典输出
Python: beautify numerical and dict outputs
我正在循环输出并比较 gof_train
和 gof_test
:
pp.pprint(gof_train)
pp.pprint(gof_test)
这会给我这样的结果(在 IPython 笔记本中):
# gof_train
{'Chi_square': 2835.3674597856002,
'K_S': 0.05029482196934898,
'MSE': 7.3741561732037447e-08,
'RMSE / Mean': 0.46193590138914759,
'RMSE / Mode': 0.050926962892235687,
'R_square': 0.88494738072721291}
# gof_test
{'Chi_square': 708.90289308802267,
'K_S': 0.05029482196934898,
'MSE': 7.3741561732037447e-08,
'RMSE / Mean': 0.46193590138914759,
'RMSE / Mode': 0.050926962892235687,
'R_square': 0.88494738072721291}
而且它们很难看。请问有没有办法美化输出?
具体来说,我想缩短数字,让这两个字典的属性相互比较。像这样:
'Chi_square': 2835.3674, 'Chi_square': 708.902,
'K_S': 0.050294, 'K_S': 0.0502,
'R_square': 0.8849, 'R_square': 0.8849
我的想法
数值输出,我觉得可以试试%precision
,http://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.basic.html#IPython.core.magics.basic.BasicMagics.precision
但我不知道有什么比较结果的好方法。如果我可以设置gof_train
和gof_test
float: left
的css
会很有趣,但我认为这是不可能的。
事实上,您无法使用 CSS 影响 Python 输出的显示,但您可以将结果提供给格式化函数,该函数会负责将结果设为 "beautiful"。在你的情况下,你可以使用这样的东西:
def compare_dicts(dict1, dict2, col_width):
print('{' + ' ' * (col_width-1) + '{')
for k1, v1 in dict1.items():
col1 = u" %s: %.3f," % (k1, v1)
padding = u' ' * (col_width - len(col1))
line = col1 + padding
if k1 in dict2:
line = u"%s %s: %.3f," % (line, k1, dict2[k1])
print(line)
print('}' + ' ' * (col_width-1) + '}')
dict1 = {
'foo': 0.43657,
'foobar': 1e6,
'bar': 0,
}
dict2 = {
'bar': 1,
'foo': 0.43657,
}
compare_dicts(dict1, dict2, 25)
这给出:
{ {
foobar: 1000000.000,
foo: 0.437, foo: 0.437,
bar: 0.000, bar: 1.000,
} }
只需使用pandas:
In [1]: import pandas as pd
更改小数位数:
In [2]: pd.options.display.float_format = '{:.3f}'.format
制作数据框:
In [3]: df = pd.DataFrame({'gof_test': gof_test, 'gof_train': gof_train})
并显示:
In [4]: df
Out [4]:
另一种选择是使用 engineering prefix:
In [5]: pd.set_eng_float_format(use_eng_prefix=True)
df
Out [5]:
In [6]: pd.set_eng_float_format()
df
Out [6]:
我正在循环输出并比较 gof_train
和 gof_test
:
pp.pprint(gof_train)
pp.pprint(gof_test)
这会给我这样的结果(在 IPython 笔记本中):
# gof_train
{'Chi_square': 2835.3674597856002,
'K_S': 0.05029482196934898,
'MSE': 7.3741561732037447e-08,
'RMSE / Mean': 0.46193590138914759,
'RMSE / Mode': 0.050926962892235687,
'R_square': 0.88494738072721291}
# gof_test
{'Chi_square': 708.90289308802267,
'K_S': 0.05029482196934898,
'MSE': 7.3741561732037447e-08,
'RMSE / Mean': 0.46193590138914759,
'RMSE / Mode': 0.050926962892235687,
'R_square': 0.88494738072721291}
而且它们很难看。请问有没有办法美化输出?
具体来说,我想缩短数字,让这两个字典的属性相互比较。像这样:
'Chi_square': 2835.3674, 'Chi_square': 708.902,
'K_S': 0.050294, 'K_S': 0.0502,
'R_square': 0.8849, 'R_square': 0.8849
我的想法
数值输出,我觉得可以试试
%precision
,http://ipython.org/ipython-doc/2/api/generated/IPython.core.magics.basic.html#IPython.core.magics.basic.BasicMagics.precision但我不知道有什么比较结果的好方法。如果我可以设置
gof_train
和gof_test
float: left
的css
会很有趣,但我认为这是不可能的。
事实上,您无法使用 CSS 影响 Python 输出的显示,但您可以将结果提供给格式化函数,该函数会负责将结果设为 "beautiful"。在你的情况下,你可以使用这样的东西:
def compare_dicts(dict1, dict2, col_width):
print('{' + ' ' * (col_width-1) + '{')
for k1, v1 in dict1.items():
col1 = u" %s: %.3f," % (k1, v1)
padding = u' ' * (col_width - len(col1))
line = col1 + padding
if k1 in dict2:
line = u"%s %s: %.3f," % (line, k1, dict2[k1])
print(line)
print('}' + ' ' * (col_width-1) + '}')
dict1 = {
'foo': 0.43657,
'foobar': 1e6,
'bar': 0,
}
dict2 = {
'bar': 1,
'foo': 0.43657,
}
compare_dicts(dict1, dict2, 25)
这给出:
{ {
foobar: 1000000.000,
foo: 0.437, foo: 0.437,
bar: 0.000, bar: 1.000,
} }
只需使用pandas:
In [1]: import pandas as pd
更改小数位数:
In [2]: pd.options.display.float_format = '{:.3f}'.format
制作数据框:
In [3]: df = pd.DataFrame({'gof_test': gof_test, 'gof_train': gof_train})
并显示:
In [4]: df
Out [4]:
另一种选择是使用 engineering prefix:
In [5]: pd.set_eng_float_format(use_eng_prefix=True)
df
Out [5]:
In [6]: pd.set_eng_float_format()
df
Out [6]: