0

I have a numpy array containing mostly whole numbers and floats. The way I understand it, arrays are always stored as floats, and so the integers are stored as <number>.0. I want to work with the entries of this array as whole floats, but after some testing, it seems the whole numbers are printed as <number>. excluding the .0 decimal. I will not be printing these, so np.set_printoptions won't help me. I've also tried adding '%.1f' % which works, but isn't a possible solution as it turns my float into a string.

verticies = np.array([
    [4.5, 2],
    [0, 1],
    [-1.5, 2], 
    [1.5, 2], 
    [1.5, 1.5], 
    [2, 1.5], 
    [2, -0.5], 
    [1.5, -0.5], 
    [1.5, -2], 
    [0.5, -2], 
    [0.5, -4.5], 
    [-0.5, -4.5], 
    [-0.5, -2], 
    [-1.5, -2], 
    [-1.5, -0.5], 
    [-2, -0.5], 
    [-2, 1.5], 
    [-1.5, 1.5]
])

origin = np.array([
    [verticies[0][0]],
    [verticies[0][1]]
])

print(origin)

I would expect this to print [[4.5], [2.0]], but instead it prints [[4.5] [2. ]]

In short: how can I include the decimal 0 after 2.?

Any help would be greatly appreaciated!

Nuddel69
  • 9
  • 2
  • If I understand it right, you need a function to modify the output string-cast of your NumPy array, am I right? – ARK1375 Jul 18 '21 at 10:13
  • 1
    Why can't you use `numpy.set_printoptions` exactly? – 9769953 Jul 18 '21 at 10:13
  • 3
    The `2.` is still a float, and it's still exactly `2.0`. Whether or not the 0 is shown in output or string format, doesn't matter for calculations; it only matters to the human eye/mind. – 9769953 Jul 18 '21 at 10:15
  • Does this answer your question? [How to pretty-print a numpy.array without scientific notation and with given precision?](https://stackoverflow.com/questions/2891790/how-to-pretty-print-a-numpy-array-without-scientific-notation-and-with-given-pre) – yann ziselman Jul 18 '21 at 13:38
  • Nuddel69, When value is 2, you want output of "2.0". If the value was 2.123, what output desired? – chux - Reinstate Monica Jul 18 '21 at 14:11

1 Answers1

2

use np.set_printoptions

float_formatter = "{:.1f}".format
np.set_printoptions(formatter={'float_kind':float_formatter})
print(origin)

output

[[4.5]
[2.0]]
Mutaz-MSFT
  • 756
  • 5
  • 20
  • 1
    I seem to have misinterpreted my problem. The traceback mentioned being unable to read the value of the array, which lead me to isolating the array in question. I noticed integers being printed on a peculiar form, and assumed this a problem on numpy's part. In the end my problem was a missing datatype identifier! Thank you still, this would've answered my question! – Nuddel69 Jul 18 '21 at 15:40