开发者

simple numpy example from manual does not work as expected

开发者 https://www.devze.com 2023-04-11 09:51 出处:网络
The following code is from the Numpy 1.4.1 manual, numpy.char import numpy as np charar = np.chararray((3, 3))

The following code is from the Numpy 1.4.1 manual, numpy.char

import numpy as np
charar = np.chararray((3, 3))
charar[:, :] = 'abc'
print charar

According to the manual, 开发者_运维知识库this is supposed to output

chararray([['a', 'a', 'a'],
       ['a', 'a', 'a'],
       ['a', 'a', 'a']],
      dtype='|S1')

Instead I get

[['\xd8' '3' 'U']
 ['\xb7' '\x18' '\xc2']
 ['\xbd' '' '\x10']]

This looks like uninitialized values. Does anyone have any ideas what is going on here? I'm using the numpy package for Debian squeeze, which I realise is out of date, but I'm having some problems backporting the 1.6 package from unstable.

UPDATE: I've now backported the 1.6 package from unstable, minus the docs, which won't build for some reason. I get exactly the same results. I guess I'll go ahead and report this to the numpy project.


I don't have numpy 1.4 installed on my machine, but certainly if I run the code you wrote, I get a similar answer that doesn't match the manual. It should be noted though that in the newest version of the numpy manual the example has changed:

>>> charar = np.chararray((3, 3))
>>> charar[:] = 'a'
>>> charar
chararray([['a', 'a', 'a'],
       ['a', 'a', 'a'],
       ['a', 'a', 'a']],
      dtype='|S1')

See http://docs.scipy.org/doc/numpy/reference/generated/numpy.chararray.html

Also, np.chararray post v1.4 is only included in numpy for backward compatibility with Numarray, and the manual states that it shouldn't be used for new development.

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消