4

I'm trying to use set_printoptions from the answer to the question How to pretty-printing a numpy.array without scientific notation and with given precision?

But I get this error:

Traceback (most recent call last):
  File "neural_network.py", line 57, in <module>
    output.set_printoptions(precision=3)
AttributeError: 'numpy.ndarray' object has no attribute 'set_printoptions'

Apparently, not all numpy arrays are created equal, and what works for a regular numpy.array doesn't work for a numpy.ndarray.

How can I format a numpy.ndarray for priting such as to remove scientific notation?

UPDATE

Changing the call to numpy.set_printoptions() removes the error, but has no effect on the print format of the ndarray contents.

Alex R
  • 11,364
  • 15
  • 100
  • 180
  • `set_printoptions` is a module level function. It is not a method of an `ndarray`. What we casually call a `numpy` array is actually an object of class `np.ndarray`. `np` or `numpy` refers to the module that we import. There isn't such a thing as a 'regular nupy.array'. – hpaulj Jul 23 '18 at 18:34
  • perhaps my question should be re-worded: How should the module-level function `set_printoptions` be invoked in order to work with an `ndarray`? – Alex R Jul 23 '18 at 18:41
  • The accepted answer in your link shows how to use `np.set_printoptions`. Other answers show how to use it in a `context`. That link also has an answer that use `array2string`. You'll have to show more code if you want us to diagnose why `numpy.set_printoptions` isn't working for you. – hpaulj Jul 23 '18 at 19:00

2 Answers2

4

Try numpy.array2string which takes ndarray as input and you can set precision.

Scroll down in below documentation link for examples.

https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.array2string.html

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • 1
    while it would be nice to know why numpy.set_printoptions() doesn't work, this answer does solve my actual problem of printing the array with precision=3 – Alex R Jul 23 '18 at 18:47
  • May be the calling style is little different.... here I found a sample in below repository https://github.com/numpy/numpy/blob/master/numpy/core/arrayprint.py look for "np.set_printoptions" – Pavan Chandaka Jul 23 '18 at 19:03
  • you may try something same as said in link (above comment) ..first by setting precision and calling print function with specifying your array is a type of np.array "print(np.array(output))" – Pavan Chandaka Jul 23 '18 at 19:09
-2

Use suppress = True and may solve the issue:

np.set_printoptions(precision = 2, suppress= True)