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
Answer from Akavall on Stack OverflowIn [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.
python - Convert ndarray from float64 to integer - Stack Overflow
python - Convert Numpy array with 'n' and 'y' into integer array of 0 and 1 - Data Science Stack Exchange
python - convert array([[ number ]]) to 'number' - Stack Overflow
machine learning - Creating a single number from a numpy array - Python - Data Science Stack Exchange
Use .astype.
>>> a = numpy.array([1, 2, 3, 4], dtype=numpy.float64)
>>> a
array([ 1., 2., 3., 4.])
>>> a.astype(numpy.int64)
array([1, 2, 3, 4])
See the documentation for more options.
While astype is probably the "best" option there are several other ways to convert it to an integer array. I'm using this arr in the following examples:
>>> import numpy as np
>>> arr = np.array([1,2,3,4], dtype=float)
>>> arr
array([ 1., 2., 3., 4.])
The int* functions from NumPy
>>> np.int64(arr)
array([1, 2, 3, 4])
>>> np.int_(arr)
array([1, 2, 3, 4])
The NumPy *array functions themselves:
>>> np.array(arr, dtype=int)
array([1, 2, 3, 4])
>>> np.asarray(arr, dtype=int)
array([1, 2, 3, 4])
>>> np.asanyarray(arr, dtype=int)
array([1, 2, 3, 4])
The astype method (that was already mentioned but for completeness sake):
>>> arr.astype(int)
array([1, 2, 3, 4])
Note that passing int as dtype to astype or array will default to a default integer type that depends on your platform. For example on Windows it will be int32, on 64bit Linux with 64bit Python it's int64. If you need a specific integer type and want to avoid the platform "ambiguity" you should use the corresponding NumPy types like np.int32 or np.int64.
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 [24]: x=np.array([[0.04816103]])
In [25]: x
Out[25]: array([[0.04816103]])
In [26]: x.shape
Out[26]: (1, 1)
In [27]: x[0,0]
Out[27]: 0.04816103
In [28]: x[0][0]
Out[28]: 0.04816103
In [29]: x.squeeze()
Out[29]: array(0.04816103)
In [30]: _.shape
Out[30]: ()
In [34]: x.item()
Out[34]: 0.04816103
I used as this instruction, make two parameters referee one to another as below:
Rad[i] = {'j': str(JY[:, :, 0][0][0]), 'h': str(JY[:, :, 0][0][0] + 1j * JY[:, :,2][0][0])}
Output is:
{'j': '0.04816103', 'h': '(5.21626177-5.11993971j)'}
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]
Make a sample array
In [206]: x=np.array([1e9, 2e10, 1e6])
In [207]: x
Out[207]: array([ 1.00000000e+09, 2.00000000e+10, 1.00000000e+06])
We can convert to ints - except notice that the largest one is too large the default int32
In [208]: x.astype(int)
Out[208]: array([ 1000000000, -2147483648, 1000000])
In [212]: x.astype(np.int64)
Out[212]: array([ 1000000000, 20000000000, 1000000], dtype=int64)
Writing a csv with the default format (float) (this is the default format regardless of the array dtype):
In [213]: np.savetxt('text.txt',x)
In [214]: cat text.txt
1.000000000000000000e+09
2.000000000000000000e+10
1.000000000000000000e+06
We can specify a format:
In [215]: np.savetxt('text.txt',x, fmt='%d')
In [216]: cat text.txt
1000000000
20000000000
1000000
Potentially there are 3 issues:
- integer v float in the array itself, it's
dtype - display or print of the array
- writing the array to a csv file
It is a printing option, see the documentation: printing options. Briefly stated: you need to use the suppress option when printing:
np.set_printoptions(suppress=True) # for small floating point.
np.set_printoptions(suppress=True, formatter={'all':lambda x: str(x)})