How to pretty-print a numpy.array without scientific notation and with given precision?

后端 未结 14 2184
臣服心动
臣服心动 2020-11-22 04:28

I\'m curious, whether there is any way to print formatted numpy.arrays, e.g., in a way similar to this:

x = 1.23456
print \'%.3f\' % x
         


        
14条回答
  •  花落未央
    2020-11-22 04:48

    I often want different columns to have different formats. Here is how I print a simple 2D array using some variety in the formatting by converting (slices of) my NumPy array to a tuple:

    import numpy as np
    dat = np.random.random((10,11))*100  # Array of random values between 0 and 100
    print(dat)                           # Lines get truncated and are hard to read
    for i in range(10):
        print((4*"%6.2f"+7*"%9.4f") % tuple(dat[i,:]))
    

提交回复
热议问题