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)
Answer from Joe Kington on Stack OverflowWell, 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'>
[Python] | Working with float array
How to convert a float array into integer array? - Python - Data Science Dojo Discussions
How to mix strings and floats in a NumPy array?
I'd consider a pandas DataFrame instead - the actual data is stored as a numpy array and so supports everything it needs to, and the headers are separate so you don't need to do awkward slicing when working on the whole data set. You can also then index rows and columns by their index. And it supports going to and from CSVs easily.
As an aside, the way to get arrays to accept mixed types is
np.array([1, 'a'], dtype=object)
but obviously you lose practically all of the benefits of using numpy in the first place.
More on reddit.comSaving a numpy array exactly as is to an image
Why are you doing image processing without image processing libraries? Causing yourself some unneeded pain! Just use scikit-image or opencv! I'm normally not one to suggest using huge libraries for a simple purpose when others would work but...seems silly not to in this example.
With that said, PIL should work fine for this task. Works for me:
In [1]: from PIL import Image
In [2]: import numpy as np
In [3]: img = np.uint8(12 * np.random.rand(50, 50))
In [4]: I = Image.fromarray(img)
In [5]: I.save('asdf.png')
In [6]: I2 = Image.open('asdf.png')
In [7]: img2 = np.array(I2)
In [8]: img
Out[8]:
array([[ 8, 11, 2, ..., 4, 2, 6],
[ 1, 3, 8, ..., 0, 1, 11],
[ 2, 2, 0, ..., 11, 4, 6],
...,
[ 7, 5, 9, ..., 11, 11, 10],
[ 9, 5, 7, ..., 9, 7, 10],
[ 2, 9, 10, ..., 11, 3, 10]], dtype=uint8)
In [9]: img2
Out[9]:
array([[ 8, 11, 2, ..., 4, 2, 6],
[ 1, 3, 8, ..., 0, 1, 11],
[ 2, 2, 0, ..., 11, 4, 6],
...,
[ 7, 5, 9, ..., 11, 11, 10],
[ 9, 5, 7, ..., 9, 7, 10],
[ 2, 9, 10, ..., 11, 3, 10]], dtype=uint8)
In [10]: (img == img2).all()
Out[10]: TrueMy guess is that your image data type is floating point. Generally floating point images are in [0, 1] but it's not a strictly enforced thing--sometimes people keep the values [0, 255] for example, but want more precision. Sometimes the values are [-1, 1]. Etc. Anyways, the point is it isn't entirely consistent. Because of that almost all libraries will scale floating point images automatically. Solution in that case is to convert your images from floating point to uint8.
More on reddit.comVideos
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])I'd like to manage headers as strings in the first column and first row of a matrix. The rest being float values.
The following trick will get the job done, but I don't think this is the right method.
array = numpy.zeros((rows,cols)).astype(object)
The idea is for conveinience when moving the data into a csv file.
Thank you for any advise!
I'd consider a pandas DataFrame instead - the actual data is stored as a numpy array and so supports everything it needs to, and the headers are separate so you don't need to do awkward slicing when working on the whole data set. You can also then index rows and columns by their index. And it supports going to and from CSVs easily.
As an aside, the way to get arrays to accept mixed types is
np.array([1, 'a'], dtype=object)
but obviously you lose practically all of the benefits of using numpy in the first place.
There is a simpler way to do this. Numpy arrays are designed for numbers. You can put whatever you want into them, but why?
Just keep a list. e.g.
array= ... headers = ["Name", "Phone", "Address", "Email"]
Write the headers, then write the array.