Casting a float array into a string array
Hi, I'm trying to cast a float array into a string array (for instance transforming [[2., 3.], [4., 5.]] into [['2.', '3.'], ['4.', '5.']]), I tried with astype(str) and every variation (str_, string, string_, string0), but not luck. Is there a function or a method of the array class that can fulfill my needs ? And if there is a way to add a formatting option ('1.1f' for instance), it would be even better. Matthieu
what's wrong with astype? In [3]: x = numpy.array([[2.,3.],[4.,5.]]) In [4]: x.astype(str) Out[4]: array([['2', '3'], ['4', '5']], dtype='|S1') and if you want a list: In [5]: x.astype(str).tolist() Out[5]: [['2', '3'], ['4', '5']] L. On 10/5/07, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
Hi,
I'm trying to cast a float array into a string array (for instance transforming [[2., 3.], [4., 5.]] into [['2.', '3.'], ['4.', '5.']]), I tried with astype(str) and every variation (str_, string, string_, string0), but not luck. Is there a function or a method of the array class that can fulfill my needs ? And if there is a way to add a formatting option ('1.1f' for instance), it would be even better.
Matthieu
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
I'd like to have the '2.', because if the number is negative, only '-' is returned, not the real value. Matthieu 2007/10/5, lorenzo bolla <lbolla@gmail.com>:
what's wrong with astype?
In [3]: x = numpy.array([[2.,3.],[4.,5.]])
In [4]: x.astype(str) Out[4]: array([['2', '3'], ['4', '5']], dtype='|S1')
and if you want a list:
In [5]: x.astype(str).tolist() Out[5]: [['2', '3'], ['4', '5']]
L.
On 10/5/07, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
Hi,
I'm trying to cast a float array into a string array (for instance transforming [[2., 3.], [4., 5.]] into [['2.', '3.'], ['4.', '5.']]), I tried with astype(str) and every variation (str_, string, string_, string0), but not luck. Is there a function or a method of the array class that can fulfill my needs ? And if there is a way to add a formatting option ('1.1f' for instance), it would be even better.
Matthieu
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
On 05/10/2007, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
I'd like to have the '2.', because if the number is negative, only '-' is returned, not the real value.
For string arrays you need to specify the length of the string as part of the data type (and it defaults to length 1): In [11]: random.normal(size=(2,3)).astype("|S20") Out[11]: array([['-0.223407464804', '1.1952765837', '-2.24073805089'], ['1.36604738338', '-0.197059880309', '2.15648625075']], dtype='|S20') In [12]: random.normal(size=(2,3)).astype("|S2") Out[12]: array([['-0', '1.', '-0'], ['0.', '0.', '1.']], dtype='|S2') In [13]: random.normal(size=(2,3)).astype("str") Out[13]: array([['-', '1', '1'], ['-', '-', '0']], dtype='|S1') Anne
gotcha. specify the number of bytes, then. In [20]: x Out[20]: array([[-2., 3.], [ 4., 5.]]) In [21]: x.astype(numpy.dtype('S10')) Out[21]: array([['-2.0', '3.0'], ['4.0', '5.0']], dtype='|S10') L. On 10/5/07, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
I'd like to have the '2.', because if the number is negative, only '-' is returned, not the real value.
Matthieu
2007/10/5, lorenzo bolla < lbolla@gmail.com>:
what's wrong with astype?
In [3]: x = numpy.array([[2.,3.],[4.,5.]])
In [4]: x.astype(str) Out[4]: array([['2', '3'], ['4', '5']], dtype='|S1')
and if you want a list:
In [5]: x.astype(str).tolist() Out[5]: [['2', '3'], ['4', '5']]
L.
On 10/5/07, Matthieu Brucher < matthieu.brucher@gmail.com> wrote:
Hi,
I'm trying to cast a float array into a string array (for instance transforming [[2., 3.], [4., 5.]] into [['2.', '3.'], ['4.', '5.']]), I tried with astype(str) and every variation (str_, string, string_, string0), but not luck. Is there a function or a method of the array class that can fulfill my needs ? And if there is a way to add a formatting option ('1.1f' for instance), it would be even better.
Matthieu
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Thank you for the precision, I didn't thought of using 'Sxx' directly :( Matthieu 2007/10/5, lorenzo bolla <lbolla@gmail.com>:
gotcha. specify the number of bytes, then.
In [20]: x Out[20]: array([[-2., 3.], [ 4., 5.]])
In [21]: x.astype(numpy.dtype('S10')) Out[21]: array([['-2.0', '3.0'], ['4.0', '5.0']], dtype='|S10')
L.
On 10/5/07, Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
I'd like to have the '2.', because if the number is negative, only '-' is returned, not the real value.
Matthieu
2007/10/5, lorenzo bolla < lbolla@gmail.com>:
what's wrong with astype?
In [3]: x = numpy.array([[2.,3.],[4.,5.]])
In [4]: x.astype(str) Out[4]: array([['2', '3'], ['4', '5']], dtype='|S1')
and if you want a list:
In [5]: x.astype(str).tolist() Out[5]: [['2', '3'], ['4', '5']]
L.
On 10/5/07, Matthieu Brucher < matthieu.brucher@gmail.com > wrote:
Hi,
I'm trying to cast a float array into a string array (for instance transforming [[2., 3.], [4., 5.]] into [['2.', '3.'], ['4.', '5.']]), I tried with astype(str) and every variation (str_, string, string_, string0), but not luck. Is there a function or a method of the array class that can fulfill my needs ? And if there is a way to add a formatting option ('1.1f' for instance), it would be even better.
Matthieu
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Matthieu Brucher wrote:
And if there is a way to add a formatting option ('1.1f' for instance), it would be even better.
For full control of the formatting, you can use python's string formatting, and a nested list comprehension: [ ["%.3f"%i for i in r] for r in x] I don't know how to generalize this to n-d though -- maybe numpy.vectorize? -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
Guys -- I'm a little puzzled by a NumPy behavior. Perhaps the gurus on this list can enlighten me, please! I am working with numpy.histogram. I have a decent understanding of how it works when given an ascending range to bin into. However, when I give it a *decending* range, I can't figure out what the results mean. Here's an example: ------------------------ <session log> --------------------
A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1]) (x, y) = numpy.histogram(A, range=(0, 7)) x array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0])
(x, y) = numpy.histogram(A, range=(7, 0)) x array([ 0, -1, -3, 0, -3, -2, 0, -2, -2, 13])
-------------------- </session log> ------------------------
Please set aside the natural response "the user shouldn't bin into a decending range!" since I am trying to figure out what computation NumPy actually does in this case and I don't want a work-around. And yes, I have looked at the source. It's nicely vectorized, so I find the source rather opaque. Therefore, I would appreciate it if if some kind soul could answer a couple of questions: * What does the return mean for range=(7, 0)? * Why should the return histogram have negative elements? * If it truely isn't meaningful, why not catch the case and reject input? Maybe this is a bug.... ??? Thanks! Stuart
Stuart Brorson wrote:
Guys --
I'm a little puzzled by a NumPy behavior. Perhaps the gurus on this list can enlighten me, please!
I am working with numpy.histogram. I have a decent understanding of how it works when given an ascending range to bin into. However, when I give it a *decending* range, I can't figure out what the results mean. Here's an example:
------------------------ <session log> --------------------
A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1]) (x, y) = numpy.histogram(A, range=(0, 7)) x array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0]) (x, y) = numpy.histogram(A, range=(7, 0)) x array([ 0, -1, -3, 0, -3, -2, 0, -2, -2, 13]) -------------------- </session log> ------------------------
Please set aside the natural response "the user shouldn't bin into a decending range!" since I am trying to figure out what computation NumPy actually does in this case and I don't want a work-around. And yes, I have looked at the source. It's nicely vectorized, so I find the source rather opaque.
Therefore, I would appreciate it if if some kind soul could answer a couple of questions:
* What does the return mean for range=(7, 0)?
Nothing.
* Why should the return histogram have negative elements?
Because there are subtractions involved that depend on the bins being increasing which they are not if the given range is incorrect.
* If it truely isn't meaningful, why not catch the case and reject input? Maybe this is a bug.... ???
Patches are welcome. -- 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
Robert, Thanks for your answers about histogram's meaning for range=(7, 0)!
* If it truely isn't meaningful, why not catch the case and reject input? Maybe this is a bug.... ???
Patches are welcome.
OK. I don't know if you have a patch tracking system, so I'll just post it here. If you have a patch tracker, point me to it and I'll enter the patch there. ---------------------- <patch> ------------------------- Index: function_base.py =================================================================== --- function_base.py (revision 4155) +++ function_base.py (working copy) @@ -108,6 +108,12 @@ """ a = asarray(a).ravel() + + if (range is not None): + mn, mx = range + if (mn > mx): + raise AttributeError, 'max must be larger than min in range parameter.' + if not iterable(bins): if range is None: range = (a.min(), a.max()) @@ -116,6 +122,9 @@ mn -= 0.5 mx += 0.5 bins = linspace(mn, mx, bins, endpoint=False) + else: + if(any(bins[1:]-bins[:-1] < 0)): + raise AttributeError, 'bins must increase monotonically.' # best block size probably depends on processor cache size block = 65536 ----------------------- </patch> ------------------------- And here's what it does: ----------------------- <session log> ---------------------- /home/sdb> python Python 2.5 (r25:51908, Feb 19 2007, 04:41:03) [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import numpy A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1])
(x, y) = numpy.histogram(A, range=(0, 7)) x array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0])
(x, y) = numpy.histogram(A, range=(7, 0)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.5/site-packages/numpy/lib/function_base.py", line 115, in histogram raise AttributeError, 'max must be larger than min in range parameter.' AttributeError: max must be larger than min in range parameter.
bins = numpy.arange(0, 7) (x, y) = numpy.histogram(A, bins=bins)
bins = bins[::-1] bins array([6, 5, 4, 3, 2, 1, 0]) (x, y) = numpy.histogram(A, bins=bins) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.5/site-packages/numpy/lib/function_base.py", line 127, in histogram raise AttributeError, 'bins must increase monotonically.' AttributeError: bins must increase monotonically.
------------------------- </session log> --------------------------
Cheers, Stuart
Stuart Brorson wrote:
Robert,
Thanks for your answers about histogram's meaning for range=(7, 0)!
* If it truely isn't meaningful, why not catch the case and reject input? Maybe this is a bug.... ??? Patches are welcome.
OK. I don't know if you have a patch tracking system, so I'll just post it here. If you have a patch tracker, point me to it and I'll enter the patch there.
http://projects.scipy.org/scipy/numpy -- 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
OK. I don't know if you have a patch tracking system, so I'll just post it here. If you have a patch tracker, point me to it and I'll enter the patch there.
OK, entered as ticket #586. Cheers, Stuart
Check how you're implementing the histogram function with respect to that range statement. It seems to make a difference, desirable or not.
import numpy numpy.__version__ '1.0.4.dev3982' A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1]) (x, y) = numpy.histogram(A, range(0, 7)) x array([0, 2, 2, 2, 3, 3, 1]) y [0, 1, 2, 3, 4, 5, 6]
(x, y) = numpy.histogram(A, range=(0, 7)) x array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0]) y array([ 0. , 0.7, 1.4, 2.1, 2.8, 3.5, 4.2, 4.9, 5.6, 6.3])
(x, y) = numpy.histogram(A, range(7,0)) x array([], dtype=int32) y []
Note that in the last case, the histogram function isn't returning anything for a descending range. Also notice that you're overwriting a python function with the way you're assigning things....
range(0,7) [0, 1, 2, 3, 4, 5, 6] range=(0,7) range (0, 7)
I'll leave it to others to decide if this is problematic. -Mark Stuart Brorson wrote:
Guys --
I'm a little puzzled by a NumPy behavior. Perhaps the gurus on this list can enlighten me, please!
I am working with numpy.histogram. I have a decent understanding of how it works when given an ascending range to bin into. However, when I give it a *decending* range, I can't figure out what the results mean. Here's an example:
------------------------ <session log> --------------------
A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1]) (x, y) = numpy.histogram(A, range=(0, 7)) x array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0]) (x, y) = numpy.histogram(A, range=(7, 0)) x array([ 0, -1, -3, 0, -3, -2, 0, -2, -2, 13]) -------------------- </session log> ------------------------
Please set aside the natural response "the user shouldn't bin into a decending range!" since I am trying to figure out what computation NumPy actually does in this case and I don't want a work-around. And yes, I have looked at the source. It's nicely vectorized, so I find the source rather opaque.
Therefore, I would appreciate it if if some kind soul could answer a couple of questions:
* What does the return mean for range=(7, 0)?
* Why should the return histogram have negative elements?
* If it truely isn't meaningful, why not catch the case and reject input? Maybe this is a bug.... ???
Thanks!
Stuart _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Mark.Miller wrote:
Check how you're implementing the histogram function with respect to that range statement. It seems to make a difference, desirable or not.
import numpy numpy.__version__ '1.0.4.dev3982' A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1]) (x, y) = numpy.histogram(A, range(0, 7)) x array([0, 2, 2, 2, 3, 3, 1]) y [0, 1, 2, 3, 4, 5, 6]
(x, y) = numpy.histogram(A, range=(0, 7)) x array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0]) y array([ 0. , 0.7, 1.4, 2.1, 2.8, 3.5, 4.2, 4.9, 5.6, 6.3])
Please check the signature of numpy.histogram(). The two aren't intended to be the same. The range argument has nothing to do with the builtin range() function.
(x, y) = numpy.histogram(A, range(7,0)) x
array([], dtype=int32)
y []
Note that in the last case, the histogram function isn't returning anything for a descending range.
Also notice that you're overwriting a python function with the way you're assigning things....
No, he's not. "range" is a keyword argument to histogram(). He's using it correctly. -- 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
My bad...I also note that I forgot to decrement the descending list in my example. Ignore.... Robert Kern wrote:
Mark.Miller wrote:
Check how you're implementing the histogram function with respect to that range statement. It seems to make a difference, desirable or not.
import numpy numpy.__version__ '1.0.4.dev3982' A = numpy.array([1, 2, 3, 4, 5, 6, 5, 4, 5, 4, 3, 2, 1]) (x, y) = numpy.histogram(A, range(0, 7)) x array([0, 2, 2, 2, 3, 3, 1]) y [0, 1, 2, 3, 4, 5, 6]
(x, y) = numpy.histogram(A, range=(0, 7)) x array([0, 2, 2, 0, 2, 3, 0, 3, 1, 0]) y array([ 0. , 0.7, 1.4, 2.1, 2.8, 3.5, 4.2, 4.9, 5.6, 6.3])
Please check the signature of numpy.histogram(). The two aren't intended to be the same. The range argument has nothing to do with the builtin range() function.
(x, y) = numpy.histogram(A, range(7,0)) x
array([], dtype=int32)
y []
Note that in the last case, the histogram function isn't returning anything for a descending range.
Also notice that you're overwriting a python function with the way you're assigning things....
No, he's not. "range" is a keyword argument to histogram(). He's using it correctly.
participants (7)
-
Anne Archibald
-
Christopher Barker
-
lorenzo bolla
-
Mark.Miller
-
Matthieu Brucher
-
Robert Kern
-
Stuart Brorson