[Python] How to pretty print an array with small number in numpy

Discussão em 'Python' iniciado por Stack, Junho 2, 2025.

  1. Stack

    Stack Membro Participativo

    I have an array that I would like to display

    import numpy as np

    # build a matrix with a few large values and noise
    matrix = np.diag([5.9, 0.0, 13.11])
    rng = np.random.default_rng(seed=20250602)
    matrix += rng.random(size=(3, 3)) * 10e-10

    with np.printoptions(precision=2):
    print(matrix)


    The code above produces

    [[5.90e+00 4.13e-10 5.66e-10]
    [2.30e-10 4.80e-10 9.56e-10]
    [2.48e-10 4.32e-10 1.31e+01]]


    I would like instead to see something like this

    [[5.90 0 0]
    [0 0 0]
    [0 0 13.1]]


    namely, I don't want to see numbers smaller than a threshold (say 1e-3) to appear as different values making it seems like there is some information there. I would like only the values above the threshold to stand out.

    Continue reading...

Compartilhe esta Página