【问题标题】:How to change the format of values of a matrix in python?如何在python中更改矩阵值的格式?
【发布时间】:2020-04-23 05:23:35
【问题描述】:
[[0.0000000e+00 0.0000000e+00 1.0000000e+00 1.6534920e+05 1.3689780e+05
  4.7178410e+05]
 [1.0000000e+00 0.0000000e+00 0.0000000e+00 1.6259770e+05 1.5137759e+05
  4.4389853e+05]
 [0.0000000e+00 1.0000000e+00 0.0000000e+00 1.5344151e+05 1.0114555e+05
  4.0793454e+05]
 [0.0000000e+00 0.0000000e+00 1.0000000e+00 1.4437241e+05 1.1867185e+05
  3.8319962e+05]
 [1.0000000e+00 0.0000000e+00 0.0000000e+00 0.0000000e+00 1.1698380e+05
  4.5173060e+04]]

我应该如何更改矩阵的值,以便以 %0f 格式四舍五入?

【问题讨论】:

  • 你如何打印这个?

标签: python machine-learning jupyter-notebook


【解决方案1】:

您可以使用numpy's vectorize 函数并传递要应用于列表的函数:

import numpy as np
rfunc = np.vectorize(round)     # round() is a python function which rounds inputs to a precision set by number of decimals; e.g: np.round(2.3456, 1) returns 2.3 
rfunc(a, 2)     # 2 is the precision which needs to be adjusted

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 2020-02-28
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 2021-09-10
    • 2017-03-29
    相关资源
    最近更新 更多