避免 scipy io.mmwrite 函数的科学记数法

Avoid scientific notation for scipy io.mmwrite function

我正在尝试导出类型为 ' 的稀疏矩阵,它以 csr 格式存储,使用 from scipy: io.mmwrite,其中擅长处理数据的稀疏性。 但是,此输出采用科学计数法:

%%MatrixMarket matrix coordinate real general
%
1719 2504 4304376
1 1 -9.0979224e-01
2 1 -1.6585451e-01

并且我想以正常浮点数的形式获取后处理脚本,该脚本期望 classmtx 格式的浮点值。

有没有办法轻松实现?

我目前的解决方案涉及再次读取写入的 mtx 文件并为所有行转换第 3 字段,这不是时间效率...

有没有办法指定 scipy 要使用的符号?像 :

np.set_printoptions(suppress=True)

我也试过强制转换为double/float,但找不到相关线程。

谢谢

这里有一个可能适合您的快速技巧。 (这是一个 hack,因为它覆盖了 class 的私有静态方法,该方法并未真正记录为 public API 的一部分。如果底层代码在SciPy,此 hack 可能不再有效。)

创建 scipy.io.mmfile.MMFile 的子class 覆盖 _field_template 方法,使其 returns 成为自定义格式字符串。例如

from scipy.io.mmio import MMFile


class MMFileFixedFormat(MMFile):

    def _field_template(self, field, precision):
        # Override MMFile._field_template.
        return f'%.{precision}f\n'

要使用此 class 编写矩阵市场文件,请将您对函数 scipy.io.mmwrite 的使用替换为 MMFileFixedFormat().write

这是 ipython 会话中的示例。稀疏矩阵在a.

In [77]: a
Out[77]: 
<5x5 sparse matrix of type '<class 'numpy.float32'>'
    with 8 stored elements in Compressed Sparse Row format>

In [78]: a.A
Out[78]: 
array([[0.27621606, 0.        , 0.        , 0.7780487 , 0.        ],
       [0.7295764 , 0.        , 0.        , 0.        , 0.        ],
       [0.09457383, 0.        , 0.13346413, 0.        , 0.        ],
       [0.        , 0.        , 0.11267778, 0.        , 0.        ],
       [0.05113978, 0.        , 0.        , 0.9891698 , 0.        ]],
      dtype=float32)

这是将 a 写入文件 "a.mtx" 的行。

In [79]: MMFileFixedFormat().write('a.mtx', a, precision=9)                                                         

查看文件:

In [80]: !cat a.mtx                                                                                                 
%%MatrixMarket matrix coordinate real general
%
5 5 8
1 1 0.27621606
1 4 0.77804869
2 1 0.72957641
3 1 0.09457383
3 3 0.13346413
4 3 0.11267778
5 1 0.05113978
5 4 0.98916978

您可能想要调整在函数 _field_template() 中创建的格式字符串。具有固定小数位数的格式的一个潜在问题是,如果条目的值为 0.00000098765432,它将打印为 0.000000099,而 0.0000000000123 将打印为 0.00000000(假设您使用与上例相同的 precision)。


不过请注意,矩阵市场文件的适当 reader 应该能够处理以科学记数法书写的数字。