[Numpy-discussion] Type Inference
Charles R Harris
Wed Nov 25 23:51:29 CST 2009
On Wed, Nov 25, 2009 at 12:34 PM, Dan Yamins <firstname.lastname@example.org> wrote:
> Sometimes I need to convert object-type arrays to their "natural, real"
> type, without a priori knowing what that type is, e.g. the equivalent of:
> >>> Y = np.array(X.tolist())
> where X is the object array. If X is naturally an array of ints, Y will be
> an int array, if X is naturally strings, then Y will be '|Sn' where n is the
> right string length, the right casting will be done if there are mixed
> types, etc...
> My question is: is there a faster way to do this, given that X is already
> an object array.
> One possible (though maybe not the only) way this might work is if there is
> a faster lower-level numpy type-inference function that doesn't actually do
> the conversion, but just reports the "right" type, the type that np.array()
> *would* convert to. Does such a thing exist? And if so, (let's call it
> 'np.typeinferer', hypothetically), would it then be faster to do something
> >>> Y = np.array(X,np.typeinferer(X))
> by-passing the step of converting the list-ified version of X to the new
> type? Or is the bulk of the work being done by np.array() the type
> inference anyway, so that it just makes sense to use np.array() in the first
> Sorry if this question is unclear -- and thanks for all the help recently.
This seems to work:
In : x = ones(2, dtype=object)
In : x
Out: array([1, 1], dtype=object)
In : array(x, dtype=int)
Out: array([1, 1])
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion