np.zeros(2, 'S') returns empty strings.
This sort of makes sense, but is it the 'correct' behavior? In [20]: zeros(2, 'S') Out[20]: array(['', ''], dtype='|S1') It might be more consistent to return '0' instead, as in In [3]: zeros(2, int).astype('S') Out[3]: array(['0', '0'], dtype='|S24') Chuck
On Sat, Jan 14, 2012 at 4:12 PM, Charles R Harris wrote: This sort of makes sense, but is it the 'correct' behavior? In [20]: zeros(2, 'S')
Out[20]:
array(['', ''],
dtype='|S1') It might be more consistent to return '0' instead, as in In [3]: zeros(2, int).astype('S')
Out[3]:
array(['0', '0'],
dtype='|S24') Chuck Whatever it should be, numpy is currently inconsistent: np.empty(2, 'S')
array(['0', '\xd4'],
dtype='|S1')
np.zeros(2, 'S')
array(['', ''],
dtype='|S1')
np.ones(2, 'S')
array(['1', '1'],
dtype='|S1') I would expect '0''s for the call to zeros() and empty strings for the call
to empty().
Ben Root
On Sat, Jan 14, 2012 at 4:16 PM, Benjamin Root
On Sat, Jan 14, 2012 at 4:12 PM, Charles R Harris < charlesr.harris@gmail.com> wrote:
This sort of makes sense, but is it the 'correct' behavior?
In [20]: zeros(2, 'S') Out[20]: array(['', ''], dtype='|S1')
It might be more consistent to return '0' instead, as in
In [3]: zeros(2, int).astype('S') Out[3]: array(['0', '0'], dtype='|S24')
Chuck
Whatever it should be, numpy is currently inconsistent:
np.empty(2, 'S') array(['0', '\xd4'], dtype='|S1') np.zeros(2, 'S')
array(['', ''], dtype='|S1')
np.ones(2, 'S') array(['1', '1'], dtype='|S1')
I would expect '0''s for the call to zeros() and empty strings for the call to empty().
Ben Root
On the other hand, it is fairly standard to assume that the values in the array returned by empty() to be random, uninitialized junk. So, maybe empty()'s current behavior is ok, but certainly zeros()'s and ones()'s behaviors need to be looked at. Ben Root
On Sat, Jan 14, 2012 at 5:25 PM, Benjamin Root
On Sat, Jan 14, 2012 at 4:16 PM, Benjamin Root
wrote: On Sat, Jan 14, 2012 at 4:12 PM, Charles R Harris
wrote: This sort of makes sense, but is it the 'correct' behavior?
In [20]: zeros(2, 'S') Out[20]: array(['', ''], dtype='|S1')
It might be more consistent to return '0' instead, as in
In [3]: zeros(2, int).astype('S') Out[3]: array(['0', '0'], dtype='|S24')
I would be surprised if zeros is not an empty string, since an empty string is the "zero" for string addition. multiplication for strings doesn't exist, so ones can be anything even literally '1'
a = np.zeros(5,'S4') a[:] = 'b' reduce(lambda x,y: x+y, a) 'bbbbb'
a = np.zeros(1,'S100') for i in range(5): a[:] = a.item() + 'a' ... a array(['aaaaa'], dtype='|S100')
just as a logical argument, I have no idea what's practical since last time I tried to use numpy strings, I didn't find string addition and went back to double and triple list comprehension. Josef
Chuck
Whatever it should be, numpy is currently inconsistent:
np.empty(2, 'S') array(['0', '\xd4'], dtype='|S1') np.zeros(2, 'S')
array(['', ''], dtype='|S1')
np.ones(2, 'S') array(['1', '1'], dtype='|S1')
I would expect '0''s for the call to zeros() and empty strings for the call to empty().
Ben Root
On the other hand, it is fairly standard to assume that the values in the array returned by empty() to be random, uninitialized junk. So, maybe empty()'s current behavior is ok, but certainly zeros()'s and ones()'s behaviors need to be looked at.
Ben Root
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Sat, Jan 14, 2012 at 5:21 PM,
On Sat, Jan 14, 2012 at 5:25 PM, Benjamin Root
wrote: On Sat, Jan 14, 2012 at 4:16 PM, Benjamin Root
wrote: On Sat, Jan 14, 2012 at 4:12 PM, Charles R Harris
wrote: This sort of makes sense, but is it the 'correct' behavior?
In [20]: zeros(2, 'S') Out[20]: array(['', ''], dtype='|S1')
It might be more consistent to return '0' instead, as in
In [3]: zeros(2, int).astype('S') Out[3]: array(['0', '0'], dtype='|S24')
I would be surprised if zeros is not an empty string, since an empty string is the "zero" for string addition. multiplication for strings doesn't exist, so ones can be anything even literally '1'
a = np.zeros(5,'S4') a[:] = 'b' reduce(lambda x,y: x+y, a) 'bbbbb'
a = np.zeros(1,'S100') for i in range(5): a[:] = a.item() + 'a' ... a array(['aaaaa'], dtype='|S100')
just as a logical argument, I have no idea what's practical since last time I tried to use numpy strings, I didn't find string addition and went back to double and triple list comprehension.
I don't think it was quite so cleverly reasoned out ;) The functions works as expected for object arrays, but that is the only exception. For all other types the allocated space is simply filled with zero bytes. Too bad this isn't done in python like ones, it would be easier to fix. <snip> Chuck
On 15. jan. 2012, at 01:21, josef.pktd@gmail.com wrote:
On Sat, Jan 14, 2012 at 5:25 PM, Benjamin Root
wrote: On Sat, Jan 14, 2012 at 4:16 PM, Benjamin Root
wrote: On Sat, Jan 14, 2012 at 4:12 PM, Charles R Harris
wrote: This sort of makes sense, but is it the 'correct' behavior?
In [20]: zeros(2, 'S') Out[20]: array(['', ''], dtype='|S1')
It might be more consistent to return '0' instead, as in
In [3]: zeros(2, int).astype('S') Out[3]: array(['0', '0'], dtype='|S24')
I would be surprised if zeros is not an empty string, since an empty string is the "zero" for string addition. multiplication for strings doesn't exist, so ones can be anything even literally '1'
My python disagrees. In [1]: 2 * 'spam ham ' Out[1]: 'spam ham spam ham ' Not sure what the element-wise numpy array equivalent would be, though. Paul
On Sat, Jan 14, 2012 at 2:12 PM, Charles R Harris
This sort of makes sense, but is it the 'correct' behavior?
In [20]: zeros(2, 'S') Out[20]: array(['', ''], dtype='|S1')
I think of numpy strings as raw fixed-length byte arrays (since, well, that's what they are), so I would expect np.zeros to return all-NUL strings, like it does. (Not just 'empty' strings, which just means the first byte is NUL -- I expect all-NUL.) Maybe I've spent too much time working with C data structures, but that's my $0.02 :-) -- Nathaniel
On Sun, Jan 15, 2012 at 3:15 AM, Nathaniel Smith
On Sat, Jan 14, 2012 at 2:12 PM, Charles R Harris
wrote: This sort of makes sense, but is it the 'correct' behavior?
In [20]: zeros(2, 'S') Out[20]: array(['', ''], dtype='|S1')
I think of numpy strings as raw fixed-length byte arrays (since, well, that's what they are), so I would expect np.zeros to return all-NUL strings, like it does. (Not just 'empty' strings, which just means the first byte is NUL -- I expect all-NUL.) Maybe I've spent too much time working with C data structures, but that's my $0.02 :-)
Since I'm not coding in C: can a fixed-length empty string, '', be represented as only first byte is NUL? The following with the current behavior looks all reasonable to me
np.zeros(2).view('S4') array(['', '', '', ''], dtype='|S4') np.zeros(4, 'S4').view(float) array([ 0., 0.]) np.zeros(4, 'S4').view(int) array([0, 0, 0, 0]) np.zeros(4, 'S4').view('S16') array([''], dtype='|S16')
np.zeros(2, float).view('S4') array(['', '', '', ''], dtype='|S4') instead of astype
np.zeros(2, float).astype('S4') array(['0.0', '0.0'], dtype='|S4')
my 2c (with trying to understand what's the question) Josef
-- Nathaniel _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (5)
-
Benjamin Root
-
Charles R Harris
-
josef.pktd@gmail.com
-
Nathaniel Smith
-
Paul Anton Letnes