numpy.astype returns a copy of the array. It does not manipulate your array in-place. So you have to assign the value to a variable:
Y = Y.astype(int)
Answer from Joe on Stack OverflowHello so I want my input to be an integer of 6 numbers โ123456โ but then print them out as 3 arrays with two integer each f.e. โ12โ, โ34โ, โ56โ. Any idea which way is the easiest?
python - converting the values in my array into INT - Stack Overflow
How to convert a float array into integer array? - Python - Data Science Dojo Discussions
python 3.x - How to convert a numpy integer array to an integer - Stack Overflow
python - Convert Numpy array with 'n' and 'y' into integer array of 0 and 1 - Data Science Stack Exchange
Videos
numpy.astype returns a copy of the array. It does not manipulate your array in-place. So you have to assign the value to a variable:
Y = Y.astype(int)
Ok, if we have numpy here and something like that:
Y = np.array([5., 0., 4., 1., 9.])
We can convert each element of array to int like this:
map(int, Y)
To get this result:
[5, 0, 4, 1, 9]
It is quite trivial
(X=='y').astype(int)
Should do the trick. It simply converts your array to True or False according to your requirements and then astype will impose the required datatype. By default int will give you 1 for True and 0 for False.
You could use the following code:
X[X=='y'] = 1
X[X=='n'] = 0
This replaces the indexes of 'y' with 1 and of 'n' with 0.
Generally the X=='y' returns a Boolean array which contains True where the 'y' and False everywhere else and so on.
In [8]: import numpy as np
In [9]: a = np.array([1,2,3,4,5])
In [10]: int("".join(map(str, a)))
Out[10]: 12345
Just use the sum() function with a generator:
>>> sum(e * 10 ** i for i, e in enumerate(x[::-1]))
12345
or, alternatively:
>>> sum(e * 10 ** (len(x) - i-1) for i, e in enumerate(x))
12345
or you could use str.join, again with a generator:
>>> int(''.join(str(i) for i in x))
12345
why?
In the first example, we use a generator to yield each number multiplied by 10 to the power of its position in the array, we then add these together to get our final result. This may be easier to see if we use a list-comprehension, rather than a generator to see what is going on:
>>> [e * 10 ** i for i, e in enumerate(x[::-1])]
[5, 40, 300, 2000, 10000]
and then it is clear from here that, through summing these numbers together, we will get the result of 12345. Note, however, that we had to reverse x before using this, as otherwise the numbers would be multiplied by the wrong powers of 10 (i.e. the first number would be multiplied by 1 rather than 10000).
In the second snippet, the code simply uses a different method to get the right power of 10 for each index. Rather than reversing the array, we simply subtract the index from the length of x (len(x)), and minus one more so that the first element is 10000, not 100000. This is simply an alternative.
Finally, the last method should be fairly self-explanatory. We are merely joining together the stringified versions of each number in the array, and then converting back to an int to get our result.
If you are not satisfied with lists (because they can contain anything and take up too much memory) you can use efficient array of integers:
import array
array.array('i')
See here
If you need to initialize it,
a = array.array('i',(0 for i in range(0,10)))
two ways:
x = [0] * 10
x = [0 for i in xrange(10)]
Edit: replaced range by xrange to avoid creating another list.
Also: as many others have noted including Pi and Ben James, this creates a list, not a Python array. While a list is in many cases sufficient and easy enough, for performance critical uses (e.g. when duplicated in thousands of objects) you could look into python arrays. Look up the array module, as explained in the other answers in this thread.