如何在python中从布尔数组转换为int数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16869990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert from boolean array to int array in python
提问by Akashdeep Saluja
I have a Numpy 2-D array in which one column has Boolean values i.e. True/False. I want to convert it to integer 1and 0respectively, how can I do it?
我有一个 Numpy 二维数组,其中一列具有布尔值,即True/ False。我想将它分别转换为整数1,0我该怎么做?
E.g. my data[0::,2]is boolean, I tried
例如我data[0::,2]是布尔值,我试过
data[0::,2]=int(data[0::,2])
, but it is giving me error:
,但它给了我错误:
TypeError: only length-1 arrays can be converted to Python scalars
TypeError: only length-1 arrays can be converted to Python scalars
My first 5 rows of array are:
我的前 5 行数组是:
[['0', '3', 'True', '22', '1', '0', '7.25', '0'],
['1', '1', 'False', '38', '1', '0', '71.2833', '1'],
['1', '3', 'False', '26', '0', '0', '7.925', '0'],
['1', '1', 'False', '35', '1', '0', '53.1', '0'],
['0', '3', 'True', '35', '0', '0', '8.05', '0']]
采纳答案by kirelagin
Ok, the easiest way to change a type of any array to float is doing:
好的,将任何数组的类型更改为浮动的最简单方法是:
data.astype(float)
data.astype(float)
The issue with your array is that float('True')is an error, because 'True'can't be parsed as a float number. So, the best thing to do is fixing your array generation code to produce floats (or, at least, strings with valid float literals) instead of bools.
您的数组的问题是这float('True')是一个错误,因为'True'无法解析为浮点数。因此,最好的办法是修复您的数组生成代码以生成浮点数(或者,至少,具有有效浮点文字的字符串)而不是布尔值。
In the meantime you can use this function to fix your array:
同时,您可以使用此函数来修复您的数组:
def boolstr_to_floatstr(v):
if v == 'True':
return '1'
elif v == 'False':
return '0'
else:
return v
And finally you convert your array like this:
最后你像这样转换你的数组:
new_data = np.vectorize(boolstr_to_floatstr)(data).astype(float)
回答by Mr. B
If I do this on your raw data source, which is strings:
如果我在您的原始数据源(字符串)上执行此操作:
data = [['0', '3', 'True', '22', '1', '0', '7.25', '0'],
['1', '1', 'False', '38', '1', '0', '71.2833', '1'],
['1', '3', 'False', '26', '0', '0', '7.925', '0'],
['1', '1', 'False', '35', '1', '0', '53.1', '0'],
['0', '3', 'True', '35', '0', '0', '8.05', '0']]
data = [[eval(x) for x in y] for y in data]
..and then follow that with:
..然后遵循:
data = [[float(x) for x in y] for y in data]
# or this if you prefer:
arr = numpy.array(data)
..then the problem is solved. ..you can even do it as a one-liner (I think this makes ints, though, and floats are probably needed): numpy.array([[eval(x) for x in y] for y in data])
..那么问题就解决了。..你甚至可以将它作为单行(我认为这会产生整数,但可能需要浮点数): numpy.array([[eval(x) for x in y] for y in data])
..I think the problem is that numpy is keeping your numeric strings as strings, and since not all of your strings are numeric, you can't do a type conversion on the whole array. Also, if you try to do a type conversion just on the parts of the array with "True" and "False", you're not really working with booleans, but with strings. ..and the only ways I know of to change that are to do the eval statement. ..well, you could do this, too:
..我认为问题在于 numpy 将您的数字字符串保留为字符串,并且由于并非所有字符串都是数字,因此您无法对整个数组进行类型转换。此外,如果您尝试仅对具有“True”和“False”的数组部分进行类型转换,那么您实际上并不是在使用布尔值,而是在使用字符串。..我知道的唯一改变方法是执行 eval 语句。..好吧,你也可以这样做:
booltext_int = {'True': 1, 'False': 2}
clean = [[float(x) if x[-1].isdigit() else booltext_int[x]
for x in y] for y in data]
..this way you avoid evals, which are inherently insecure. ..but that may not matter, since you may be using a trusted data source.
..这样你就可以避免 evals,它本质上是不安全的。..但这可能无关紧要,因为您可能正在使用受信任的数据源。
回答by jamylak
Using @kirelagin's idea with ast.literal_eval
使用@kirelagin 的想法 ast.literal_eval
>>> import ast
>>> import numpy as np
>>> arr = np.array(
[['0', '3', 'True', '22', '1', '0', '7.25', '0'],
['1', '1', 'False', '38', '1', '0', '71.2833', '1'],
['1', '3', 'False', '26', '0', '0', '7.925', '0'],
['1', '1', 'False', '35', '1', '0', '53.1', '0'],
['0', '3', 'True', '35', '0', '0', '8.05', '0']])
>>> np.vectorize(ast.literal_eval, otypes=[np.float])(arr)
array([[ 0. , 3. , 1. , 22. , 1. , 0. ,
7.25 , 0. ],
[ 1. , 1. , 0. , 38. , 1. , 0. ,
71.2833, 1. ],
[ 1. , 3. , 0. , 26. , 0. , 0. ,
7.925 , 0. ],
[ 1. , 1. , 0. , 35. , 1. , 0. ,
53.1 , 0. ],
[ 0. , 3. , 1. , 35. , 0. , 0. ,
8.05 , 0. ]])
回答by aslan
boolarrayvariable.astype(int) works:
boolarrayvariable.astype(int) 作品:
data = np.random.normal(0,1,(1,5))
threshold = 0
test1 = (data>threshold)
test2 = test1.astype(int)
Output:
输出:
data = array([[ 1.766, -1.765, 2.576, -1.469, 1.69]])
test1 = array([[ True, False, True, False, True]], dtype=bool)
test2 = array([[1, 0, 1, 0, 1]])
回答by Eusebio Rufian-Zilbermann
Old Q but, for reference - a bool can be converted to an int and an int to a float
旧 Q 但是,作为参考 - bool 可以转换为 int,int 可以转换为 float
data[0::,2]=data[0::,2].astype(int).astype(float)
data[0::,2]=data[0::,2].astype(int).astype(float)

