如何将字符串数组转换为numpy中的浮点数组?

时间:2022-06-27 02:42:44

How to convert

如何转换

["1.1", "2.2", "3.2"]

to

[1.1, 2.2, 3.2]

in NumPy?

在NumPy?

4 个解决方案

#1


118  

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.)

好吧,如果您正在以列表形式读取数据,只需执行np.array(map(float,list_of_strings))(或等效地使用列表推导)。 (在Python 3中,如果使用map,则需要在map返回值上调用list,因为map现在返回一个迭代器。)

However, if it's already a numpy array of strings, there's a better way. Use astype().

但是,如果它已经是一个庞大的字符串数组,那么有一种更好的方法。使用astype()。

import numpy as np
x = np.array(['1.1', '2.2', '3.3'])
y = x.astype(np.float)

#2


2  

You can use this as well

你也可以使用它

import numpy as np
x=np.array(['1.1', '2.2', '3.3'])
x=np.asfarray(x,float)

#3


1  

If you have (or create) a single string, you can use np.fromstring:

如果您有(或创建)单个字符串,则可以使用np.fromstring:

import numpy as np
x = ["1.1", "2.2", "3.2"]
x = ','.join(x)
x = np.fromstring( x, dtype=np.float, sep=',' )

Note, x = ','.join(x) transforms the x array to string '1.1, 2.2, 3.2'. If you read a line from a txt file, each line will be already a string.

注意,x =','。join(x)将x数组转换为字符串'1.1,2.2,3.2'。如果从txt文件中读取一行,则每行都将是一个字符串。

#4


0  

Another option might be numpy.asarray:

另一种选择可能是numpy.asarray:

import numpy as np
a = ["1.1", "2.2", "3.2"]
b = np.asarray(a, dtype=np.float64, order='C')

For Python 2*:

对于Python 2 *:

print a, type(a), type(a[0])
print b, type(b), type(b[0])

resulting in:

导致:

['1.1', '2.2', '3.2'] <type 'list'> <type 'str'>
[1.1 2.2 3.2] <type 'numpy.ndarray'> <type 'numpy.float64'>

#1


118  

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.)

好吧,如果您正在以列表形式读取数据,只需执行np.array(map(float,list_of_strings))(或等效地使用列表推导)。 (在Python 3中,如果使用map,则需要在map返回值上调用list,因为map现在返回一个迭代器。)

However, if it's already a numpy array of strings, there's a better way. Use astype().

但是,如果它已经是一个庞大的字符串数组,那么有一种更好的方法。使用astype()。

import numpy as np
x = np.array(['1.1', '2.2', '3.3'])
y = x.astype(np.float)

#2


2  

You can use this as well

你也可以使用它

import numpy as np
x=np.array(['1.1', '2.2', '3.3'])
x=np.asfarray(x,float)

#3


1  

If you have (or create) a single string, you can use np.fromstring:

如果您有(或创建)单个字符串,则可以使用np.fromstring:

import numpy as np
x = ["1.1", "2.2", "3.2"]
x = ','.join(x)
x = np.fromstring( x, dtype=np.float, sep=',' )

Note, x = ','.join(x) transforms the x array to string '1.1, 2.2, 3.2'. If you read a line from a txt file, each line will be already a string.

注意,x =','。join(x)将x数组转换为字符串'1.1,2.2,3.2'。如果从txt文件中读取一行,则每行都将是一个字符串。

#4


0  

Another option might be numpy.asarray:

另一种选择可能是numpy.asarray:

import numpy as np
a = ["1.1", "2.2", "3.2"]
b = np.asarray(a, dtype=np.float64, order='C')

For Python 2*:

对于Python 2 *:

print a, type(a), type(a[0])
print b, type(b), type(b[0])

resulting in:

导致:

['1.1', '2.2', '3.2'] <type 'list'> <type 'str'>
[1.1 2.2 3.2] <type 'numpy.ndarray'> <type 'numpy.float64'>