1

I have a numpy.ndarray of the following structure:

   array([[ 7963.92759169, -2931.3518914 ,  3360.79428745],
   [ 7964.28495515, -2930.99452794,  3361.15165092],
   [ 7965.60367246, -2929.67581063,  3362.47036823]])

I am trying to limit the digits after decimal to 2 with the below code:

for (label, score) in zip(lables, scoring_fn):
   print("[INFO] {}: {:.2f}".format(label, float(score)))

With this, I get the error "TypeError: unsupported format string passed to numpy.ndarray.format". Could anyone suggest on how to correct this error?

2 Answers 2

1

Or use np.ndarray.round:

arr = arr.round(2)
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

np.set_printoptions(precision=2)
print(YourArray)

example:

import numpy as np
a = np.array([[ 7963.92759169, -2931.3518914 ,  3360.79428745],
   [ 7964.28495515, -2930.99452794,  3361.15165092],
   [ 7965.60367246, -2929.67581063,  3362.47036823]])
np.set_printoptions(precision=2)
print(a)

output:

[[ 7963.93 -2931.35  3360.79]
 [ 7964.28 -2930.99  3361.15]
 [ 7965.6  -2929.68  3362.47]]

2 Comments

I tried using printoptions, as you could see in your output, it is no longer an array of integers (comma is not there between the numbers). So I could not access the array using index going further
using index you can print like this: print(a[0][0].round(2))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.