Reputation: 21
I've written a script that gives me the result of dividing two variables ("A" and "B") -- and the output of each variable is a numpy array with 26 elements. Usually, with any two elements from "A" and "B," the result of the operation is a float, and the the element in the output array that corresponds to that operation shows up as a float. But strangely, even if the output is supposed to be an integer (almost always 0 or 1), the integer will show up as "0." or "1." in the output array. Is there any way to turn these specific elements of the array back into integers, rather than keep them as floats?
I'd like to write a simple if statement that will convert any output elements that are supposed to be integers back into integers (i.e., make "0." into "0"). But I'm having some trouble with that. Any ideas?
Upvotes: 2
Views: 96
Reputation: 7507
The strength of Numpy arrays is that many low-level operations can be quickly performed on the data because most (not all) types used by these arrays have a fixed-size in memory. For instance, the floats you are using probably require 8 bytes each. The most important thing in that case is that all datas share the same type and fit in the same amount of memory. You can play a little around that if you really want (and need) to, but I would not suggest you to start by such special cases. Try to learn the strength of these arrays when used with this requirement (but this involves accepting the fact that you can't mix integers and floats in the same array).
Upvotes: 1
Reputation: 9610
You will probably want to read about data types: http://docs.scipy.org/doc/numpy/user/basics.types.html
An entire numpy array has a datatype. For the operation you are doing, it would not make sense to ask that A/B sometimes be integer and sometime be float: the division of two float arrays is a float array.
Complication: it is possible to specify mixed-type arrays: http://docs.scipy.org/doc/numpy/user/basics.rec.html#structured-arrays
Upvotes: 2