Re: [Numpy-discussion] Printing formatted numerical values

2010-11-15 Thread Jonathan Hilmer
Is there a convention for dealing with NaN and Inf?  I've found that
trusting the default behavior is a very bad idea:

---
from numpy import *
x = zeros((5,7))
x[:,3:] = nan
x[:,-1] = inf
savetxt('problem_array.txt',x,delimiter='\t')
x2 = loadtxt('problem_array.txt')
print(x.shape)  # (5, 7)
print(x2.shape) # (5, 4)
print(x2[0,:])
print(open('problem_array.txt').readline().strip().split('\t'))
---

On my system the loaded array is reduced in size relative to the saved
array.  Does savetxt need some conditionals to deal with special
values like these?

Jonathan



On Mon, Nov 15, 2010 at 8:44 AM, Dave Hirschfeld
 wrote:
>   math.duke.edu> writes:
>
>>
>> Hi, what is the best way to print (to a file or to stdout) formatted
>> numerical values? Analogously to C's printf("%d %g",x,y) etc?
>>
>
> For stdout you can simply do:
>
> In [26]: w, x, y, z = np.randint(0,100,4)
>
> In [27]: type(w)
> Out[27]: 
>
> In [28]: print("%f %g %e %d" % (w,x,y,z,))
> 68.00 64 1.30e+01 57
>
> In [29]: w, x, y, z
> Out[29]: (68, 64, 13, 57)
>
> For a file I would use np.savetxt
>
> HTH,
> Dave
>
>
>
> ___
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Printing formatted numerical values

2010-11-15 Thread Dave Hirschfeld
  math.duke.edu> writes:

> 
> Hi, what is the best way to print (to a file or to stdout) formatted 
> numerical values? Analogously to C's printf("%d %g",x,y) etc?
> 

For stdout you can simply do:

In [26]: w, x, y, z = np.randint(0,100,4)

In [27]: type(w)
Out[27]: 

In [28]: print("%f %g %e %d" % (w,x,y,z,))
68.00 64 1.30e+01 57

In [29]: w, x, y, z
Out[29]: (68, 64, 13, 57)

For a file I would use np.savetxt

HTH,
Dave



___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Printing formatted numerical values

2010-11-15 Thread Robert Kern
On Mon, Nov 15, 2010 at 08:32,   wrote:
> Hi, what is the best way to print (to a file or to stdout) formatted
> numerical values? Analogously to C's printf("%d %g",x,y) etc?
>
> Numpy Documentation only discusses input *from* a file, or output of
> entire arrays. (np.savetxt()) I just want tab or space-delimited output of
> selected formatted values.
>
> In the absence of numpy documentation on this matter, I tried to follow
> python documentation and find errors. Below is ipython
> -pylab transcript, which apparently complains that an int32 variable is
> an object of type 'str'. How should I understand this? Does python not
> understand that numpy.int32 is an integer?

Correct. On a 64-bit system, numpy.int32 does not subtype from int.
The format codes do strict type-checking.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] Printing formatted numerical values

2010-11-15 Thread Gerrit Holl
On 15 November 2010 15:32,   wrote:
> Hi, what is the best way to print (to a file or to stdout) formatted
> numerical values? Analogously to C's printf("%d %g",x,y) etc?

Use the .tofile() method:

numpy.random.random(5).tofile(sys.stdout, ' ', '%s')
0.230466435867 0.609443784908 0.353855676828 0.552641723317 0.186418931597

(works only on "real files", afaik, not on StringIO or similar)

cheers,
Gerrit.

-- 
Exploring space at http://gerrit-explores.blogspot.com/
Personal homepage at http://www.topjaklont.org/
Asperger Syndroom: http://www.topjaklont.org/nl/asperger.html
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] Printing formatted numerical values

2010-11-15 Thread pv+numpy
Hi, what is the best way to print (to a file or to stdout) formatted 
numerical values? Analogously to C's printf("%d %g",x,y) etc?

Numpy Documentation only discusses input *from* a file, or output of 
entire arrays. (np.savetxt()) I just want tab or space-delimited output of 
selected formatted values.

In the absence of numpy documentation on this matter, I tried to follow 
python documentation and find errors. Below is ipython 
-pylab transcript, which apparently complains that an int32 variable is 
an object of type 'str'. How should I understand this? Does python not 
understand that numpy.int32 is an integer?

Thank you!


In [2] import numpy as np
In [3]: w = np.arange (1,5,dtype=np.int32).reshape((2,2))
In [4]: w Out[4]: array([[1, 2],

 [3, 4]], dtype=int32)

In [5]: w[0,0]
Out[5]: 1

In [6]: w[0,0].class Out[6]: 

In [7]: print('{0:2d}'.format(w[0,0]))

ValueError? Traceback (most recent call last)

/home/p/o/dev/thesis/python/wavelet/ in ()

ValueError?: Unknown format code 'd' for object of type 'str'

  --
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion