When you are doing physics simulations you should definitely use floats for everything. 0 is an integer constant in Python, and thus np.tile creates integer arrays; use 0.0 as the argument to np.tile to do floating point arrays; or preferably use the np.zeros(N) instead:
You can check the datatype of any array variable from its dtype member:
>>> np.tile(0, 10).dtype
dtype('int64')
>>> np.tile(0.0, 10).dtype
dtype('float64')
>>> np.zeros(10)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.zeros(10).dtype
dtype('float64')
To get a zeroed array of float32 you'd need to give a float32 as the argument:
>>> np.tile(np.float32(0), 10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
or, preferably, use zeros with a defined dtype:
>>> np.zeros(10, dtype='float32')
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
Answer from Antti Haapala on Stack OverflowWhen you are doing physics simulations you should definitely use floats for everything. 0 is an integer constant in Python, and thus np.tile creates integer arrays; use 0.0 as the argument to np.tile to do floating point arrays; or preferably use the np.zeros(N) instead:
You can check the datatype of any array variable from its dtype member:
>>> np.tile(0, 10).dtype
dtype('int64')
>>> np.tile(0.0, 10).dtype
dtype('float64')
>>> np.zeros(10)
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> np.zeros(10).dtype
dtype('float64')
To get a zeroed array of float32 you'd need to give a float32 as the argument:
>>> np.tile(np.float32(0), 10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
or, preferably, use zeros with a defined dtype:
>>> np.zeros(10, dtype='float32')
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
You need x = np.zeros(N), etc.: this declares the arrays as float arrays.
This is the standard way of putting zeros in an array (np.tile() is convenient for creating a tiling with a fixed array).
[Python] | Working with float array
How to cast binary array to float array?
python - How to create a numpy array with float values - Stack Overflow
python - How to convert an array of strings to an array of floats in numpy? - Stack Overflow
Videos
Someone tell me wtf is going on with my output :(
Why doesn't it just show 1.1 in the array
What is the difference between float and double in simplest terms possible. Can you give an example to distinguish between them?
import array
a1 = array.array('f', [1.1])
a2 = array.array('d', [1.1])
print(a1)
print(a2)Output:
array('f', [1.100000023841858])
array('d', [1.1])Well, if you're reading the data in as a list, just do np.array(map(float, list_of_strings)) (or equivalently, use a list comprehension). (In Python 3, you'll need to call list on the map return value if you use map, since map returns an iterator now.)
However, if it's already a numpy array of strings, there's a better way. Use astype().
import numpy as np
x = np.array(['1.1', '2.2', '3.3'])
y = x.astype(np.float)
Another option might be numpy.asarray:
import numpy as np
a = ["1.1", "2.2", "3.2"]
b = np.asarray(a, dtype=float)
print(a, type(a), type(a[0]))
print(b, type(b), type(b[0]))
resulting in:
['1.1', '2.2', '3.2'] <class 'list'> <class 'str'>
[1.1 2.2 3.2] <class 'numpy.ndarray'> <class 'numpy.float64'>
Using struct
Base on the struct module documentation the struct.unpack() function must have defined exact format for unpacking multiple values at once. So if you need to use struct module, then you either have to define the format using format as '<ff' or iterate over the array using struct.iter_unpack(format, buffer).
Using array
Another option is to use array module, which allows you to decode the binary data at once. You can change endianness using array.byteswap() method. The only restriction is that the whole array must have same type.
import array
arr = array.array('f', b'\x00\x00\x80?\x00\x00\x00@')
arr.tolist()
# [1.0, 2.0]
# change endianness
arr.byteswap()
arr.tolist()
# [4.600602988224807e-41, 8.96831017167883e-44]
Python's strcut provide some essential tooling, but is a bit clumsy to work with. In particular, for this, it'd have to yield all your floats as a tuple of Python floats (which are "enormous" objetcs with 10s of bytes each), and then insert then into an array.
Fortunatelly, Python's array.array class itself can fill in its values given a bytes object, or even read then directly from a file.
import array
data = b'\x00\x00\x80?\x00\x00\x00@'
arr = array.array('f')
arr.frombytes(data)
print(arr)
Yields:
array('f', [1.0, 2.0])
(to read directly from a file, use the fromfile method)